Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: constant in abstract class #2959

Closed
tbmatuka opened this issue Feb 6, 2020 · 6 comments
Closed

False positive: constant in abstract class #2959

tbmatuka opened this issue Feb 6, 2020 · 6 comments
Labels
Milestone

Comments

@tbmatuka
Copy link

tbmatuka commented Feb 6, 2020

Bug report

phpstan considers $this::CONSTANT the same as self::CONSTANT, which it shouldn't.

Code snippet that reproduces the problem

https://phpstan.org/r/f9cd163f-bcf1-4547-b332-264c94aebb5c

Expected output

$this::CONSTANT in a non-final class (and especially an abstract one) should not be considered as the value that it is set to in the current class.

Usecase

I'm using doctrine and I have an abstract repository class that I extend for all of my repositories. To avoid duplicating the constructor, I set a constant to the entity class name and use it in the constructor. This gives me a single clean line of code at the beginning of the class to make it work.

Temporary workaround

I created a function in the abstract class that returns $this::CONSTANT and used the result of that in the constructor. It's ugly, but it avoids triggering the bug.

@ondrejmirtes
Copy link
Member

You can use static::CONSTANT today for the same effect.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 7, 2020
@tbmatuka
Copy link
Author

tbmatuka commented Feb 7, 2020

@ondrejmirtes you're right, that's a lot better solution for my code! I guess I should stop using $this:: anyway, so that will work for me in the future. Thanks for your help.

@phpstan-bot
Copy link
Contributor

@tbmatuka PHPStan now reports different result with your code snippet:

@@ @@
+No errors
+
+PHP 7.1 – 7.3 (1 error)
+==========
+
 12: Strict comparison using === between '' and '' will always evaluate to true.
Full report

PHP 7.4 – 8.0

No errors

PHP 7.1 – 7.3 (1 error)

Line Error
12 Strict comparison using === between '' and '' will always evaluate to true.

@phpstan-bot
Copy link
Contributor

@tbmatuka After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-12: Strict comparison using === between '' and '' will always evaluate to true.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@4cb02d1

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants