phpstan: False positive: Unreachable statement
Bug report
phpstan 0.12 latest
Code snippet that reproduces the problem
protected function propertyInUse(array $tokens, int $closeTagIndex, string $variable): bool {
/** @var string $property */
$property = substr($variable, 1);
$i = $closeTagIndex + 1;
while (isset($tokens[$i])) {
if ($tokens[$i]['code'] !== T_VARIABLE || $tokens[$i]['content'] !== '$this') {
$i++;
continue;
}
$i++;
if ($tokens[$i]['code'] !== T_OBJECT_OPERATOR) {
$i++;
continue;
}
$i++;
$token = $tokens[$i];
if ($token['code'] !== T_STRING || $token['content'] !== $property) {
$i++;
continue;
}
$i++; // 520
if ($tokens[$i]['code'] !== T_OBJECT_OPERATOR) {
$i++;
continue;
}
return true;
}
return false;
}
Expected output
520 Unreachable statement - code above always terminates.
This is not true.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (14 by maintainers)
The problem was that when the
$iin$i++wasn’t constant string like1but a general string likeint, no expression including$iwas invalidated.Fixed by adding
invalidateExpressionto NodeScopeResolver in this situation, and changingMutatingScope::invalidateExpression()to have similar logic toassignVariable(): https://github.com/phpstan/phpstan-src/commit/9665e165d50737c53d68521936cf8ab2f2c07109Assigning from
$ito some new value$j(and asserting on that) fixes the issue: https://phpstan.org/r/0f0d9246-2603-441d-b079-e08c83bed59cOr rather, @dereuromark I think the example does reproduce the issue (and @ondrejmirtes was just pointing out where the bug takes effect)
Here’s a condensed example: https://phpstan.org/r/c65b52e1-1859-4073-9705-d838cadf59f8