Skip to content

Include intializations from parent scope for anonymous functions #2607

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

Merged
merged 3 commits into from
Oct 13, 2023

Conversation

fabacino
Copy link
Contributor

@fabacino fabacino commented Sep 7, 2023

Fixes bug #9831

Comment on lines 222 to 224
if ($usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) {
$hasInitialization = $hasInitialization->or($usageScope->getParentScope()->hasExpressionType(new PropertyInitializationExpr($propertyName)));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe only enter this IF, if the outer !hasInitialization->yes() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks.

I also moved the initialization of $hasInitialization below the IF which ignores functions other than the constructor (https://github.com/phpstan/phpstan-src/pull/2607/files#diff-0e5b4f9339ad4cf44915b25c9ea53bf150de4b6fd060856247cc74b0fc0c54eaR225). Since the conditions in that IF do not depend on $hasInitialization, we can continue the loop earlier. I hope that's ok.

@fabacino fabacino requested a review from staabm September 12, 2023 13:57
@fabacino fabacino force-pushed the bugfix/9831-fix-false-positive branch from 9c6262b to 0a513d2 Compare October 12, 2023 15:15
@fabacino
Copy link
Contributor Author

Is there anything I need to do to move this forward? The failing tests do not seem to be caused by the changes introduced in this PR.

@ondrejmirtes
Copy link
Member

I've been thinking about this, and the main thing here is that I'm not sure that this is the right fix. I just thought about it and I couldn't find a way how to break this so we're probably good to go. Time and feedback will tell us if there's something wrong with it :) Thank you!

@fabacino
Copy link
Contributor Author

I see, thanks for explaining.

@fabacino fabacino deleted the bugfix/9831-fix-false-positive branch October 17, 2023 07:08
@ondrejmirtes
Copy link
Member

This looks pretty similar to the problem you fixed here, can you please look into it? Thank you: phpstan/phpstan#10048

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.

None yet

3 participants