Skip to content

Commit

Permalink
Fix reporting nonexistent offset in isset()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 30, 2022
1 parent a07dc81 commit f05e8eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -1501,7 +1501,9 @@ private function lookForEnterVariableAssign(MutatingScope $scope, Expr $expr): M

private function lookForExitVariableAssign(MutatingScope $scope, Expr $expr): MutatingScope
{
$scope = $scope->exitExpressionAssign($expr);
if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) {
$scope = $scope->exitExpressionAssign($expr);
}
if (!$expr instanceof Variable) {
return $this->lookForVariableAssignCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->exitExpressionAssign($expr));
}
Expand All @@ -1517,11 +1519,11 @@ private function lookForVariableAssignCallback(MutatingScope $scope, Expr $expr,
if ($expr instanceof Variable) {
$scope = $callback($scope, $expr);
} elseif ($expr instanceof ArrayDimFetch) {
while ($expr instanceof ArrayDimFetch) {
$expr = $expr->var;
if ($expr->dim !== null) {
$scope = $callback($scope, $expr);
}

$scope = $this->lookForVariableAssignCallback($scope, $expr, $callback);
$scope = $this->lookForVariableAssignCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
$scope = $this->lookForVariableAssignCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof StaticPropertyFetch) {
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/nonexistent-offset.php
Expand Up @@ -459,3 +459,23 @@ public function foo(array $array): int
return 0;
}
}

class MessageDescriptorTest
{

public function testDefinitions(): void
{
try {
doFoo();
} catch (\TypeError $e) {
$trace = $e->getTrace();
if (isset($trace[1]['args'][0])) {
$class = $trace[1]['args'][0];
$this->fail(sprintf('Invalid phpDoc in class: %s', $class));
}

throw $e;
}
}

}

0 comments on commit f05e8eb

Please sign in to comment.