Skip to content

Commit 726dc11

Browse files
Improve
1 parent 2c0286f commit 726dc11

3 files changed

Lines changed: 83 additions & 6 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,7 @@ private function processStmtNode(
20942094
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
20952095
$throwPoints = array_merge($throwPoints, $this->processExprNode(
20962096
$stmt,
2097-
new MethodCall($this->deepNodeCloner->cloneNode($var->var), 'offsetUnset'),
2097+
new MethodCall($var->var, 'offsetUnset'),
20982098
$scope,
20992099
$storage,
21002100
new NoopNodeCallback(),
@@ -3629,11 +3629,11 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36293629
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
36303630
$throwPoints = array_merge($throwPoints, $this->processExprNode(
36313631
$stmt,
3632-
new MethodCall($this->deepNodeCloner->cloneNode($expr->var), 'offsetGet'),
3632+
new MethodCall($expr->var, 'offsetGet'),
36333633
$scope,
36343634
$storage,
36353635
new NoopNodeCallback(),
3636-
ExpressionContext::createDeep(),
3636+
$context,
36373637
)->getThrowPoints());
36383638
}
36393639
} elseif ($expr instanceof Array_) {
@@ -3945,11 +3945,11 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
39453945

39463946
$throwPoints = array_merge($throwPoints, $this->processExprNode(
39473947
$stmt,
3948-
new MethodCall($this->deepNodeCloner->cloneNode($var->var), 'offsetExists'),
3948+
new MethodCall($var->var, 'offsetExists'),
39493949
$scope,
39503950
$storage,
39513951
new NoopNodeCallback(),
3952-
ExpressionContext::createDeep(),
3952+
$context,
39533953
)->getThrowPoints());
39543954
}
39553955
foreach (array_reverse($expr->vars) as $var) {

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,24 @@ public function testBug9267(): void
616616

617617
public function testBug11427(): void
618618
{
619-
$this->analyse([__DIR__ . '/data/bug-11427.php'], []);
619+
$this->analyse([__DIR__ . '/data/bug-11427.php'], [
620+
[
621+
'Dead catch - Exception is never thrown in the try block.',
622+
124,
623+
],
624+
[
625+
'Dead catch - Exception is never thrown in the try block.',
626+
130,
627+
],
628+
[
629+
'Dead catch - Exception is never thrown in the try block.',
630+
136,
631+
],
632+
[
633+
'Dead catch - Exception is never thrown in the try block.',
634+
142,
635+
],
636+
]);
620637
}
621638

622639
#[RequiresPhp('>= 8.4')]

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,63 @@ function testArrayOrArrayAccess($c): void {
8383
// offsetUnset can throw when $c is C
8484
}
8585
}
86+
87+
class D implements \ArrayAccess {
88+
/**
89+
* @throws void
90+
*/
91+
#[\ReturnTypeWillChange]
92+
public function offsetExists($offset) {
93+
throw new \Exception("exists");
94+
}
95+
96+
/**
97+
* @throws void
98+
*/
99+
#[\ReturnTypeWillChange]
100+
public function offsetGet($offset) {
101+
throw new \Exception("get");
102+
}
103+
104+
/**
105+
* @throws void
106+
*/
107+
#[\ReturnTypeWillChange]
108+
public function offsetSet($offset, $value) {
109+
throw new \Exception("set");
110+
}
111+
112+
/**
113+
* @throws void
114+
*/
115+
#[\ReturnTypeWillChange]
116+
public function offsetUnset($offset) {
117+
throw new \Exception("unset");
118+
}
119+
}
120+
121+
function test2(D $c): void {
122+
try {
123+
$x = isset($c[1]);
124+
} catch (\Exception $e) {
125+
// offsetExists cannot throw
126+
}
127+
128+
try {
129+
$x = $c[1];
130+
} catch (\Exception $e) {
131+
// offsetGet cannot throw
132+
}
133+
134+
try {
135+
$c[1] = 1;
136+
} catch (\Exception $e) {
137+
// offsetSet cannot throw
138+
}
139+
140+
try {
141+
unset($c[1]);
142+
} catch (\Exception $e) {
143+
// offsetUnset cannot throw
144+
}
145+
}

0 commit comments

Comments
 (0)