Skip to content

Commit f05e8eb

Browse files
committed
Fix reporting nonexistent offset in isset()
1 parent a07dc81 commit f05e8eb

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,9 @@ private function lookForEnterVariableAssign(MutatingScope $scope, Expr $expr): M
15011501

15021502
private function lookForExitVariableAssign(MutatingScope $scope, Expr $expr): MutatingScope
15031503
{
1504-
$scope = $scope->exitExpressionAssign($expr);
1504+
if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) {
1505+
$scope = $scope->exitExpressionAssign($expr);
1506+
}
15051507
if (!$expr instanceof Variable) {
15061508
return $this->lookForVariableAssignCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->exitExpressionAssign($expr));
15071509
}
@@ -1517,11 +1519,11 @@ private function lookForVariableAssignCallback(MutatingScope $scope, Expr $expr,
15171519
if ($expr instanceof Variable) {
15181520
$scope = $callback($scope, $expr);
15191521
} elseif ($expr instanceof ArrayDimFetch) {
1520-
while ($expr instanceof ArrayDimFetch) {
1521-
$expr = $expr->var;
1522+
if ($expr->dim !== null) {
1523+
$scope = $callback($scope, $expr);
15221524
}
15231525

1524-
$scope = $this->lookForVariableAssignCallback($scope, $expr, $callback);
1526+
$scope = $this->lookForVariableAssignCallback($scope, $expr->var, $callback);
15251527
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
15261528
$scope = $this->lookForVariableAssignCallback($scope, $expr->var, $callback);
15271529
} elseif ($expr instanceof StaticPropertyFetch) {

tests/PHPStan/Rules/Arrays/data/nonexistent-offset.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,23 @@ public function foo(array $array): int
459459
return 0;
460460
}
461461
}
462+
463+
class MessageDescriptorTest
464+
{
465+
466+
public function testDefinitions(): void
467+
{
468+
try {
469+
doFoo();
470+
} catch (\TypeError $e) {
471+
$trace = $e->getTrace();
472+
if (isset($trace[1]['args'][0])) {
473+
$class = $trace[1]['args'][0];
474+
$this->fail(sprintf('Invalid phpDoc in class: %s', $class));
475+
}
476+
477+
throw $e;
478+
}
479+
}
480+
481+
}

0 commit comments

Comments
 (0)