Skip to content

Commit bca49c0

Browse files
Improve
1 parent 726dc11 commit bca49c0

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6414,10 +6414,11 @@ private function processAssignVar(
64146414
$scope = $scope->assignExpression($expr, $type, $nativeType);
64156415
}
64166416

6417-
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
6417+
$setVarType = $scope->getType($originalVar->var);
6418+
if (!$setVarType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($setVarType)->no()) {
64186419
$throwPoints = array_merge($throwPoints, $this->processExprNode(
64196420
$stmt,
6420-
new MethodCall($var, 'offsetSet'),
6421+
new MethodCall($originalVar->var, 'offsetSet'),
64216422
$scope,
64226423
$storage,
64236424
new NoopNodeCallback(),

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,22 @@ public function testBug11427(): void
633633
'Dead catch - Exception is never thrown in the try block.',
634634
142,
635635
],
636+
[
637+
'Dead catch - Exception is never thrown in the try block.',
638+
183,
639+
],
640+
[
641+
'Dead catch - Exception is never thrown in the try block.',
642+
189,
643+
],
644+
[
645+
'Dead catch - Exception is never thrown in the try block.',
646+
197,
647+
],
648+
[
649+
'Dead catch - Exception is never thrown in the try block.',
650+
203,
651+
],
636652
]);
637653
}
638654

tests/PHPStan/Rules/Exceptions/data/bug-11427.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,64 @@ function test2(D $c): void {
143143
// offsetUnset cannot throw
144144
}
145145
}
146+
147+
/**
148+
* @param array<C> $arrayOfC
149+
*/
150+
function test3(array $arrayOfC): void
151+
{
152+
try {
153+
$x = isset($arrayOfC[0][1]);
154+
} catch (\Exception $e) {
155+
// offsetExists can throw
156+
}
157+
158+
try {
159+
$x = $arrayOfC[0][1];
160+
} catch (\Exception $e) {
161+
// offsetGet can throw
162+
}
163+
164+
try {
165+
$arrayOfC[0][1] = 1;
166+
} catch (\Exception $e) {
167+
// offsetSet can throw
168+
}
169+
170+
try {
171+
unset($arrayOfC[0][1]);
172+
} catch (\Exception $e) {
173+
// offsetUnset can throw
174+
}
175+
}
176+
177+
/**
178+
* @param array<C> $arrayOfC
179+
*/
180+
function test4(array $arrayOfC): void {
181+
try {
182+
$x = isset($arrayOfC[0]);
183+
} catch (\Exception $e) {
184+
// offsetExists cannot throw
185+
}
186+
187+
try {
188+
$x = $arrayOfC[0];
189+
} catch (\Exception $e) {
190+
// offsetGet cannot throw
191+
}
192+
193+
/** @var C<int, int> $newC */
194+
$newC = new C();
195+
try {
196+
$arrayOfC[0] = $newC;
197+
} catch (\Exception $e) {
198+
// offsetSet cannot throw
199+
}
200+
201+
try {
202+
unset($arrayOfC[0]);
203+
} catch (\Exception $e) {
204+
// offsetUnset cannot throw
205+
}
206+
}

0 commit comments

Comments
 (0)