Skip to content

Commit c7bab44

Browse files
authored
Prefix instanceof statements (#168)
Prefix the names existing in instanceof statements. Also added tests to cover the cast expressions.
1 parent 5e59d9a commit c7bab44

File tree

6 files changed

+223
-23
lines changed

6 files changed

+223
-23
lines changed

specs/exp/cast.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
'Cast variable' => <<<'PHP'
24+
<?php
25+
26+
$x = new stdClass();
27+
28+
(bool) $x;
29+
(int) $x;
30+
(float) $x;
31+
(array) $x;
32+
(object) $x;
33+
----
34+
<?php
35+
36+
namespace Humbug;
37+
38+
$x = new \stdClass();
39+
(bool) $x;
40+
(int) $x;
41+
(double) $x;
42+
(array) $x;
43+
(object) $x;
44+
45+
PHP
46+
,
47+
];

specs/catch.php renamed to specs/exp/catch.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
return [
1616
'meta' => [
17-
'title' => 'Miscellaneous',
17+
'title' => 'Catch expressions',
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
@@ -151,6 +151,28 @@
151151
} catch (\Humbug\X\FooException|\Throwable $t) {
152152
}
153153

154+
PHP
155+
,
156+
157+
'catch with special keywords' => <<<'PHP'
158+
<?php
159+
160+
namespace Acme;
161+
162+
try {
163+
echo "foo";
164+
} catch (self | parent $t) {
165+
}
166+
----
167+
<?php
168+
169+
namespace Humbug\Acme;
170+
171+
try {
172+
echo "foo";
173+
} catch (self|parent $t) {
174+
}
175+
154176
PHP
155177
,
156178
];

specs/exp/instanceof.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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' => 'Instanceof expressions',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
],
22+
23+
'Instance of an internal class' => <<<'PHP'
24+
<?php
25+
26+
$x = new stdClass();
27+
$x instanceof stdClass;
28+
----
29+
<?php
30+
31+
namespace Humbug;
32+
33+
$x = new \stdClass();
34+
$x instanceof \stdClass;
35+
36+
PHP
37+
,
38+
39+
'Instance of an internal class in a namespace' => <<<'PHP'
40+
<?php
41+
42+
namespace Acme;
43+
44+
use stdClass;
45+
46+
$x = new stdClass();
47+
$x instanceof stdClass;
48+
49+
----
50+
<?php
51+
52+
namespace Humbug\Acme;
53+
54+
use stdClass;
55+
$x = new \stdClass();
56+
$x instanceof \stdClass;
57+
58+
PHP
59+
,
60+
61+
'Instance of a custom exception class' => <<<'PHP'
62+
<?php
63+
64+
$x = new Foo();
65+
$x instanceof Foo;
66+
67+
----
68+
<?php
69+
70+
namespace Humbug;
71+
72+
$x = new \Humbug\Foo();
73+
$x instanceof \Humbug\Foo;
74+
75+
PHP
76+
,
77+
78+
'Instance of a custom exception class in a namespace' => <<<'PHP'
79+
<?php
80+
81+
namespace Acme;
82+
83+
$x = new Foo();
84+
$x instanceof Foo;
85+
86+
----
87+
<?php
88+
89+
namespace Humbug\Acme;
90+
91+
$x = new \Humbug\Acme\Foo();
92+
$x instanceof \Humbug\Acme\Foo;
93+
94+
PHP
95+
,
96+
97+
'Instance of with ternary' => <<<'PHP'
98+
<?php
99+
100+
namespace Acme;
101+
102+
$file = new \stdClass();
103+
104+
$file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
105+
106+
----
107+
<?php
108+
109+
namespace Humbug\Acme;
110+
111+
$file = new \stdClass();
112+
$file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
113+
114+
PHP
115+
,
116+
117+
'Instance of with special keyword' => <<<'PHP'
118+
<?php
119+
120+
namespace Acme;
121+
122+
$file instanceof static;
123+
$file instanceof self;
124+
$file instanceof parent;
125+
126+
----
127+
<?php
128+
129+
namespace Humbug\Acme;
130+
131+
$file instanceof static;
132+
$file instanceof self;
133+
$file instanceof parent;
134+
135+
PHP
136+
,
137+
];

src/Console/Configuration.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
use InvalidArgumentException;
1919
use Iterator;
2020
use RuntimeException;
21+
use SplFileInfo;
2122
use Symfony\Component\Finder\Finder;
22-
use function Humbug\PhpScoper\iterables_to_iterator;
23+
use function Humbug\PhpScoper\chain;
2324

2425
final class Configuration
2526
{
@@ -70,7 +71,7 @@ public static function load(string $path = null, array $paths = []): self
7071

7172
$finders = self::retrieveFinders($config);
7273
$filesFromPaths = self::retrieveFilesFromPaths($paths);
73-
$filesWithContents = self::retrieveFilesWithContents(iterables_to_iterator($filesFromPaths, ...$finders));
74+
$filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders));
7475

7576
return new self($path, $filesWithContents, $patchers, $whitelist);
7677
}
@@ -100,7 +101,7 @@ private function __construct(
100101
public function withPaths(array $paths): self
101102
{
102103
$filesWithContents = self::retrieveFilesWithContents(
103-
iterables_to_iterator(
104+
chain(
104105
self::retrieveFilesFromPaths(
105106
array_unique($paths)
106107
)
@@ -310,14 +311,14 @@ private static function retrieveFilesWithContents(Iterator $files): array
310311
{
311312
return array_reduce(
312313
iterator_to_array($files),
313-
function (array $files, $fileInfo): array {
314-
$file = (string) $fileInfo;
314+
function (array $files, SplFileInfo $fileInfo): array {
315+
$file = $fileInfo->getRealPath();
315316

316-
if (false === file_exists($file)) {
317+
if (false === $file) {
317318
throw new RuntimeException(
318319
sprintf(
319320
'Could not find the file "%s".',
320-
$file
321+
(string) $fileInfo
321322
)
322323
);
323324
}

src/NodeVisitor/NameStmtPrefixer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpParser\Node\Expr\ClassConstFetch;
2121
use PhpParser\Node\Expr\ConstFetch;
2222
use PhpParser\Node\Expr\FuncCall;
23+
use PhpParser\Node\Expr\Instanceof_;
2324
use PhpParser\Node\Expr\New_;
2425
use PhpParser\Node\Expr\StaticCall;
2526
use PhpParser\Node\Name;
@@ -32,6 +33,7 @@
3233
use PhpParser\Node\Stmt\Function_;
3334
use PhpParser\Node\Stmt\Interface_;
3435
use PhpParser\NodeVisitorAbstract;
36+
use function in_array;
3537

3638
/**
3739
* ```
@@ -108,6 +110,7 @@ private function prefixName(Name $name): Node
108110
|| $parentNode instanceof Class_
109111
|| $parentNode instanceof Interface_
110112
|| $parentNode instanceof Catch_
113+
|| $parentNode instanceof Instanceof_
111114
)
112115
) {
113116
return $name;
@@ -120,6 +123,8 @@ private function prefixName(Name $name): Node
120123
|| $parentNode instanceof ClassConstFetch
121124
|| $parentNode instanceof New_
122125
|| $parentNode instanceof Param
126+
|| $parentNode instanceof Catch_
127+
|| $parentNode instanceof Instanceof_
123128
)
124129
&& in_array((string) $name, self::PHP_FUNCTION_KEYWORDS, true)
125130
) {

src/functions.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
namespace Humbug\PhpScoper;
1616

17-
use AppendIterator;
18-
use ArrayIterator;
1917
use Humbug\PhpScoper\Console\Application;
2018
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
2119
use Humbug\PhpScoper\Console\Command\InitCommand;
@@ -29,7 +27,6 @@
2927
use Humbug\SelfUpdate\Exception\RuntimeException as SelfUpdateRuntimeException;
3028
use Humbug\SelfUpdate\Updater;
3129
use Iterator;
32-
use IteratorAggregate;
3330
use PackageVersions\Versions;
3431
use PhpParser\Node;
3532
use PhpParser\Parser;
@@ -211,20 +208,11 @@ function deep_clone($node)
211208
return unserialize(serialize($node));
212209
}
213210

214-
function iterables_to_iterator(iterable ...$iterables): Iterator
211+
function chain(iterable ...$iterables): Iterator
215212
{
216-
$iterator = new AppendIterator();
217-
218213
foreach ($iterables as $iterable) {
219-
if (is_array($iterable)) {
220-
$iterator->append(new ArrayIterator($iterable));
221-
} elseif ($iterable instanceof IteratorAggregate) {
222-
$iterator->append($iterable->getIterator());
223-
} else {
224-
/* @var Iterator $iterable */
225-
$iterator->append($iterable);
214+
foreach ($iterable as $key => $value) {
215+
yield $key => $value;
226216
}
227217
}
228-
229-
return $iterator;
230218
}

0 commit comments

Comments
 (0)