Skip to content

Commit cf0c60c

Browse files
committed
Finally regression tests
1 parent 9878eef commit cf0c60c

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ public function dataFileAsserts(): iterable
908908
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7374.php');
909909
yield from $this->gatherAssertTypes(__DIR__ . '/data/template-constant-bound.php');
910910
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7391.php');
911+
yield from $this->gatherAssertTypes(__DIR__ . '/data/finally-scope.php');
911912
}
912913

913914
/**
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace FinallyBugs;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function mightThrowException(): void
11+
{
12+
13+
}
14+
15+
/** @throws \DomainException */
16+
public function throwsDomainException(): void
17+
{
18+
19+
}
20+
21+
/** @throws \LogicException */
22+
public function throwsLogicException(): void
23+
{
24+
25+
}
26+
27+
public function doFoo()
28+
{
29+
$s = 1;
30+
try {
31+
$this->mightThrowException();
32+
$s = 2;
33+
} catch (\Throwable $e) { // always catches
34+
assertType('1', $s);
35+
$s = 'str';
36+
} finally {
37+
assertType("2|'str'", $s);
38+
}
39+
}
40+
41+
public function doBar()
42+
{
43+
try {
44+
$s = 1;
45+
$this->mightThrowException();
46+
} catch (\InvalidArgumentException $e) { // might catch
47+
assertType('1', $s);
48+
$s = "bar";
49+
} catch (\Throwable $e) { // always catches what isn't InvalidArgumentException
50+
assertType('1', $s);
51+
$s = 'str';
52+
} finally {
53+
assertType("1|'bar'|'str'", $s);
54+
}
55+
}
56+
57+
public function doBar2()
58+
{
59+
try {
60+
$s = 1;
61+
$this->throwsDomainException();
62+
} catch (\DomainException $e) { // always catches
63+
assertType('1', $s);
64+
$s = "bar";
65+
} catch (\Throwable $e) { // dead catch
66+
assertType('1', $s);
67+
$s = 'str';
68+
} finally {
69+
assertType("1|'bar'|'str'", $s); // could be 1|'bar'
70+
}
71+
}
72+
73+
public function doBar3()
74+
{
75+
try {
76+
$s = 1;
77+
$this->throwsDomainException();
78+
} catch (\LogicException $e) { // always catches
79+
assertType('1', $s);
80+
$s = "bar";
81+
} catch (\Throwable $e) { // dead catch
82+
assertType('1', $s);
83+
$s = 'str';
84+
} finally {
85+
assertType("1|'bar'|'str'", $s); // could be 1|'bar'
86+
}
87+
}
88+
89+
public function doBar4()
90+
{
91+
try {
92+
$s = 1;
93+
$this->throwsLogicException();
94+
} catch (\DomainException $e) { // might catch
95+
assertType('1', $s);
96+
$s = "bar";
97+
} catch (\Throwable $e) { // always catches what isn't DomainException
98+
assertType('1', $s);
99+
$s = 'str';
100+
} finally {
101+
assertType("1|'bar'|'str'", $s);
102+
}
103+
}
104+
105+
public function doBar5()
106+
{
107+
try {
108+
$s = 1;
109+
$this->throwsLogicException();
110+
} catch (\DomainException $e) { // might catch
111+
assertType('1', $s);
112+
$s = "bar";
113+
} catch (\LogicException $e) { // always catches what isn't DomainException
114+
assertType('1', $s);
115+
$s = "str";
116+
} catch (\Throwable $e) { // dead catch
117+
assertType('1', $s);
118+
$s = 'foo';
119+
} finally {
120+
assertType("1|'bar'|'foo'|'str'", $s); // could be 1|'bar'|'str'
121+
}
122+
}
123+
124+
public function doBar6()
125+
{
126+
try {
127+
$s = 1;
128+
$this->throwsLogicException();
129+
} catch (\RuntimeException $e) { // dead catch
130+
assertType('1', $s);
131+
$s = "bar";
132+
} finally {
133+
assertType('1', $s);
134+
}
135+
}
136+
137+
}

0 commit comments

Comments
 (0)