-
Notifications
You must be signed in to change notification settings - Fork 516
Fix missing detection of dead code in expressions #4090
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
base: 2.1.x
Are you sure you want to change the base?
Conversation
hmm I wonder whether I should add this
etc...? edit: I added it in all places where I could think of a example which I was able to test |
I think the new behaviour makes sense but the errors are unfortunate
|
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.
There is StatementResultTest that tests "is always terminating". Please add a similar test for ExpressionResult. Thank you.
src/Analyser/ExpressionResult.php
Outdated
@@ -28,6 +28,7 @@ public function __construct( | |||
private array $impurePoints, | |||
?callable $truthyScopeCallback = null, | |||
?callable $falseyScopeCallback = null, | |||
private bool $isAlwaysTerminating = false, |
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.
Doesn't have to be optional and doesn't have to be at the end. I'd prefer it to be after hasYield
, for consistency with StatementResult.
src/Analyser/NodeScopeResolver.php
Outdated
@@ -2408,6 +2413,7 @@ public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scop | |||
return $this->processExprNode($stmt, $newExpr, $scope, $nodeCallback, $context); | |||
} | |||
|
|||
$isAlwaysTerminating = false; |
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.
Remove the variable assign from here. It should be set inside each if
about each node type.
src/Analyser/NodeScopeResolver.php
Outdated
} elseif ($expr instanceof PropertyFetch) { | ||
$scopeBeforeVar = $scope; | ||
$result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); | ||
$hasYield = $result->hasYield(); | ||
$throwPoints = $result->getThrowPoints(); | ||
$impurePoints = $result->getImpurePoints(); | ||
$isAlwaysTerminating = false; |
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.
should be taken from $result
and also updated in $expr->name instanceof Expr
branch
$scope = $result->getScope(); | ||
} | ||
} elseif ($expr instanceof ArrayDimFetch) { | ||
$hasYield = false; | ||
$throwPoints = []; | ||
$impurePoints = []; | ||
$isAlwaysTerminating = false; |
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.
Basically this should be updated every time $hasYield
is updated. Please review all branches in processExprNode
like that.
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.
I adjusted it in most cases.
I left out e.g. null-safe operations, as these would only terminate for always-non-nullables and similar stuff.
I think this should be ready to land. thanks for the feedback |
closes phpstan/phpstan#13232
closes phpstan/phpstan#11909