Skip to content

Commit 5e59d9a

Browse files
authored
Fix prefixing classes used in try/catch blocks (#167)
1 parent 243d41c commit 5e59d9a

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

specs/catch.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Miscellaneous',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
],
22+
23+
'Catch an internal class' => <<<'PHP'
24+
<?php
25+
26+
try {
27+
echo "foo";
28+
} catch (Throwable $t) {
29+
}
30+
----
31+
<?php
32+
33+
namespace Humbug;
34+
35+
try {
36+
echo "foo";
37+
} catch (\Throwable $t) {
38+
}
39+
40+
PHP
41+
,
42+
43+
'Catch an internal class in a namespace' => <<<'PHP'
44+
<?php
45+
46+
namespace Acme;
47+
48+
try {
49+
echo "foo";
50+
} catch (\Throwable $t) {
51+
}
52+
----
53+
<?php
54+
55+
namespace Humbug\Acme;
56+
57+
try {
58+
echo "foo";
59+
} catch (\Throwable $t) {
60+
}
61+
62+
PHP
63+
,
64+
65+
'Catch an custom exception class' => <<<'PHP'
66+
<?php
67+
68+
try {
69+
echo "foo";
70+
} catch (FooException $t) {
71+
}
72+
----
73+
<?php
74+
75+
namespace Humbug;
76+
77+
try {
78+
echo "foo";
79+
} catch (\Humbug\FooException $t) {
80+
}
81+
82+
PHP
83+
,
84+
85+
'Catch an custom exception class in a namespace' => <<<'PHP'
86+
<?php
87+
88+
namespace Acme;
89+
90+
try {
91+
echo "foo";
92+
} catch (FooException $t) {
93+
}
94+
----
95+
<?php
96+
97+
namespace Humbug\Acme;
98+
99+
try {
100+
echo "foo";
101+
} catch (\Humbug\Acme\FooException $t) {
102+
}
103+
104+
PHP
105+
,
106+
107+
'Catch an custom exception class in a namespace imported with a use statement' => <<<'PHP'
108+
<?php
109+
110+
namespace Acme;
111+
112+
use X\FooException;
113+
114+
try {
115+
echo "foo";
116+
} catch (FooException $t) {
117+
}
118+
----
119+
<?php
120+
121+
namespace Humbug\Acme;
122+
123+
use Humbug\X\FooException;
124+
try {
125+
echo "foo";
126+
} catch (\Humbug\X\FooException $t) {
127+
}
128+
129+
PHP
130+
,
131+
132+
'Multiple catch statement' => <<<'PHP'
133+
<?php
134+
135+
namespace Acme;
136+
137+
use X\FooException;
138+
139+
try {
140+
echo "foo";
141+
} catch (FooException | \Throwable $t) {
142+
}
143+
----
144+
<?php
145+
146+
namespace Humbug\Acme;
147+
148+
use Humbug\X\FooException;
149+
try {
150+
echo "foo";
151+
} catch (\Humbug\X\FooException|\Throwable $t) {
152+
}
153+
154+
PHP
155+
,
156+
];

src/NodeVisitor/NameStmtPrefixer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PhpParser\Node\Name\FullyQualified;
2727
use PhpParser\Node\NullableType;
2828
use PhpParser\Node\Param;
29+
use PhpParser\Node\Stmt\Catch_;
2930
use PhpParser\Node\Stmt\Class_;
3031
use PhpParser\Node\Stmt\ClassMethod;
3132
use PhpParser\Node\Stmt\Function_;
@@ -106,6 +107,7 @@ private function prefixName(Name $name): Node
106107
|| $parentNode instanceof New_
107108
|| $parentNode instanceof Class_
108109
|| $parentNode instanceof Interface_
110+
|| $parentNode instanceof Catch_
109111
)
110112
) {
111113
return $name;

0 commit comments

Comments
 (0)