-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathavoid-short-node-names-in-fixtures.php
More file actions
68 lines (51 loc) · 1.78 KB
/
avoid-short-node-names-in-fixtures.php
File metadata and controls
68 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
// as those classes can be used in IDE autocomplete and break node instanceof
declare(strict_types=1);
use Nette\Utils\Strings;
use Rector\Scripts\Finder\FixtureFinder;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
require __DIR__ . '/../vendor/autoload.php';
$fixtureFiles = FixtureFinder::find([__DIR__ . '/../tests', __DIR__ . '/../rules-tests']);
// get short node names
$nodeFileFinder = Finder::create()
->files()
->name('*.php')
->in(__DIR__ . '/../vendor/nikic/php-parser/lib/PhpParser/Node');
/** @var SplFileInfo[] $nodeFileInfos */
$nodeFileInfos = iterator_to_array($nodeFileFinder->getIterator());
$shortNodeClassNames = [];
foreach ($nodeFileInfos as $nodeFileInfo) {
$shortNodeClassNames[] = $nodeFileInfo->getBasename('.php');
}
$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput());
$hasErrors = false;
foreach ($fixtureFiles as $fixtureFile) {
$shortClassNameMatch = Strings::match(
$fixtureFile->getContents(),
'/\b(?:class|interface)\s+(?<name>[A-Z]\w*)/'
);
if ($shortClassNameMatch === null) {
continue;
}
$fixtureClassName = $shortClassNameMatch['name'];
if (! in_array($fixtureClassName, $shortNodeClassNames, true)) {
continue;
}
$symfonyStyle->writeln(sprintf(
'Fixture class name "%s" conflicts with native short node name.%sChange it to different to avoid IDE incorrect autocomplete:%s%s%s',
$fixtureClassName,
PHP_EOL,
PHP_EOL,
$fixtureFile->getRealPath(),
PHP_EOL
));
$hasErrors = true;
}
if ($hasErrors) {
exit(1);
}
exit(0);