-
Notifications
You must be signed in to change notification settings - Fork 473
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
Fix statement analysis after early-terminating statements #3227
Conversation
@@ -82,4 +82,24 @@ public function testBug10377(): void | |||
]); | |||
} | |||
|
|||
public function testBug11179(): void | |||
{ | |||
$this->analyse([__DIR__ . '/../DeadCode/data/bug-11179.php'], [ |
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.
Please also add a test into the "Unreachable statement - code above always terminates." rule.
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.
It's in UnreachableStatementRuleTest
} | ||
break; | ||
$nodeCallback(new UnreachableStatementNode($nextStmt), $scope); |
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.
Sorry, I don't understand these changes. So here we're going to invoke UnreachableStatementNode
but then we're still going to analyse the next statement if it's not Function_ or ClassLike? I don't get it.
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.
Given this code:
<?php
namespace Foo;
doSomething();
exit();
foo();
bar();
function doSomething(): void
{
// ...
}
Here foo();
will be reported as an unreachable statement.
Before this PR, PHPStan stops analyzing statements after foo()
because analyzing bar()
is no longer useful.
However, we still need to analyze the body of doSomething()
actually.
So PHPStan should continue analysis after an unreachable statement, but the target is different;
- Before unreachable statements (
$alreadyTerminated = false
)- analyze all statements
- After unreachable statements (
$alreadyTerminated = true
)- analyze only functions and class-likes
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.
what about methods, closures, arrow-functions?
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.
@staabm 1) I don't think you can put exit
in-between methods in a class
2) I don't think closures after exit
are executed.
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.
@takaram Thank you, makes total sense!
Thank you! |
Closes phpstan/phpstan#11179