Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 14, 2025

@staabm staabm marked this pull request as ready for review August 14, 2025 20:16
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This doesn't have to be limited to properties
  2. 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".

@staabm staabm force-pushed the bug13384 branch 2 times, most recently from 03b3159 to 59c672f Compare August 30, 2025 10:07
@@ -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
Copy link
Contributor Author

@staabm staabm Aug 30, 2025

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

Copy link
Member

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.

@staabm
Copy link
Contributor Author

staabm commented Aug 30, 2025

I think we are good to go

@staabm staabm force-pushed the bug13384 branch 4 times, most recently from 783390a to 14d45f4 Compare September 5, 2025 06:02
Copy link
Member

@ondrejmirtes ondrejmirtes left a 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 native bool type, the rule can enforce it being changed to true or false.

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.

@ondrejmirtes
Copy link
Member

Also: we should give these errors different error messages (that tell the user what they need to do) with differenr error identifiers.

@staabm
Copy link
Contributor Author

staabm commented Sep 5, 2025

Hey, you're going to kill me 😅

;)

So I think it should work like this:

should we keep the bleeding edge toggle then again?

@ondrejmirtes
Copy link
Member

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)

@staabm
Copy link
Contributor Author

staabm commented Sep 5, 2025

adjusted per feedback

ohh I missed this one:

Also: we should give these errors different error messages (that tell the user what they need to do) with differenr error identifiers.

@staabm
Copy link
Contributor Author

staabm commented Sep 5, 2025

Also: we should give these errors different error messages (that tell the user what they need to do) with differenr error identifiers.

here we go. I guess next step is a auto-fixer?

@ondrejmirtes
Copy link
Member

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 private static bool $registered = false; to private static false $registered = false; but different.

@staabm
Copy link
Contributor Author

staabm commented Sep 5, 2025

I see. Makes sense.

@ondrejmirtes ondrejmirtes merged commit b62441d into phpstan:2.1.x Sep 6, 2025
438 of 453 checks passed
@ondrejmirtes
Copy link
Member

Perfect, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

never written static private property in final class not detected
4 participants