-
Notifications
You must be signed in to change notification settings - Fork 530
Report bool properties as too wide when only used with true/false #4243
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
Conversation
This pull request has been marked as ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This doesn't have to be limited to properties
- This doesn't have to be limited to PHP versions with native standalone
true
/false
types support. We have PHPDocs after all.
There are many TooWide*
rules with duplicated logic and maybe some variances in the logic. First we should refactor and deduplicate the logic where possible.
Then we could introduce a new bleeding edge toggle to enable reporting "too wide bool".
03b3159
to
59c672f
Compare
@@ -440,11 +440,13 @@ public function testBug4715(): void | |||
public function testBug4734(): void | |||
{ | |||
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4734.php'); | |||
$this->assertCount(3, $errors); | |||
$this->assertCount(5, $errors); // could be 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a topic for a followup: NodeScopeResolver
's ClassStatementsGatherer
works on a per class level, which means access to private properties from a closure-scope (from within a different class) are not seen when analyzing the class which declares the property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah of course, static analysis is not fit for these shenanigans 😊 Especially for private properties.
I think we are good to go |
tests/PHPStan/Rules/Comparison/LogicalXorConstantConditionRuleTest.php
Outdated
Show resolved
Hide resolved
c883045
to
a27a3f5
Compare
783390a
to
14d45f4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, you're going to kill me 😅
I'm a bit worried about the pedantry of this rule. I realized I don't want it to force people to be adding @return false
when they just have a : bool
return type above their functions.
So I think it should work like this:
- On all PHP versions: When the function has
@return bool
, the rule can enforce to change it to@return true
or@return false
(same for properties with@var
) - On PHP 8.2+: When the function does not have a PHPDoc
bool
type, only a nativebool
type, the rule can enforce it being changed totrue
orfalse
.
The places that call TooWideTypeCheck have easy access to getPhpDocReturnType / getNativeReturnType so it can be easily passed to check*
methods as arguments.
Please let me know if you want to change the logic, else I can tackle it myself.
Also: we should give these errors different error messages (that tell the user what they need to do) with differenr error identifiers. |
;)
should we keep the bleeding edge toggle then again? |
Yeah, all of this has to happen only with bleeding edge. The only exception would be if we went back in time and managed to implement the part with native types before 8.2 was released. (New rules for PHP 8.5 today are always reported, even without bleeding edge) |
ohh I missed this one:
|
here we go. I guess next step is a auto-fixer? |
I'm not sure about that. I'd like the auto-fixes to be a slam dunk, to be always correct. I'd say they are only suitable for opinionated "coding standard" rules. Like I'm doing for phpstan-src here: https://github.com/phpstan/phpstan-src/tree/2.1.x/build/PHPStan/Build Let's take your original issue (https://phpstan.org/r/5bbec0ea-63ee-46e0-8584-2f232c522df3) - the solution isn't to change |
I see. Makes sense. |
Perfect, thank you! |
closes phpstan/phpstan#13384