-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathNodeClassFinder.php
More file actions
49 lines (37 loc) · 1.57 KB
/
NodeClassFinder.php
File metadata and controls
49 lines (37 loc) · 1.57 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
<?php
declare(strict_types=1);
namespace Rector\Bridge\PhpParser;
use PhpParser\Node;
use Nette\Loaders\RobotLoader;
use PHPStan\Node\AnonymousClassNode;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use ReflectionClass;
final class NodeClassFinder
{
/**
* @return array<class-string<Node>>
*/
public static function find(): array
{
$robotLoader = new RobotLoader();
$robotLoader->acceptFiles = ['*.php'];
$phpParserNodeDirectory = __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/';
$robotLoader->addDirectory($phpParserNodeDirectory);
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/node-classes');
$robotLoader->refresh();
/** @var array<class-string> $nodeClasses */
$nodeClasses = array_keys($robotLoader->getIndexedClasses());
$instantiableNodeClasses = array_filter($nodeClasses, function (string $nodeClass): bool {
$reflectionClass = new ReflectionClass($nodeClass);
if ($reflectionClass->isAbstract()) {
return false;
}
return ! $reflectionClass->isInterface();
});
// this is needed, as PHPStan replaces the Class_ node of anonymous class completely
// @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/AnonymousClassVisitor.php
$specialPHPStanNodes = [AnonymousClassNode::class];
$specialRectorNodes = [FileWithoutNamespace::class];
return array_merge($instantiableNodeClasses, $specialPHPStanNodes, $specialRectorNodes);
}
}