|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Classifier; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\NodeFinder; |
|
9
|
|
|
use PhpParser\NodeTraverser; |
|
10
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
|
11
|
|
|
use PhpParser\ParserFactory; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
|
|
14
|
|
|
final class PhpParserClassifier |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private array $interfaces = []; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private array $attributes = []; |
|
24
|
|
|
private \PhpParser\Parser $parser; |
|
25
|
|
|
private NodeTraverser $traverser; |
|
26
|
|
|
private NodeFinder $nodeFinder; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(private string $directory) |
|
29
|
|
|
{ |
|
30
|
|
|
$traverser = new NodeTraverser(); |
|
31
|
|
|
$nameResolver = new NameResolver(); |
|
32
|
|
|
$traverser->addVisitor($nameResolver); |
|
33
|
|
|
$this->parser = (new ParserFactory()) |
|
34
|
|
|
->create(ParserFactory::PREFER_PHP7); |
|
35
|
|
|
$this->traverser = $traverser; |
|
36
|
|
|
$this->nodeFinder = new NodeFinder(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function withInterface(string|array $interfaces): self |
|
40
|
|
|
{ |
|
41
|
|
|
$new = clone $this; |
|
42
|
|
|
foreach ((array) $interfaces as $interface) { |
|
43
|
|
|
$new->interfaces[] = $interface; |
|
44
|
|
|
} |
|
45
|
|
|
return $new; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function withAttribute(string|array $attributes): self |
|
49
|
|
|
{ |
|
50
|
|
|
$new = clone $this; |
|
51
|
|
|
foreach ((array) $attributes as $attribute) { |
|
52
|
|
|
$new->attributes[] = $attribute; |
|
53
|
|
|
} |
|
54
|
|
|
return $new; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function find(): iterable |
|
58
|
|
|
{ |
|
59
|
|
|
$countInterfaces = count($this->interfaces); |
|
60
|
|
|
$countAttributes = count($this->attributes); |
|
61
|
|
|
|
|
62
|
|
|
if ($countInterfaces === 0 && $countAttributes === 0) { |
|
63
|
|
|
return []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$files = (new Finder()) |
|
67
|
|
|
->in($this->directory) |
|
68
|
|
|
->name('*.php') |
|
69
|
|
|
->sortByName() |
|
70
|
|
|
->files(); |
|
71
|
|
|
|
|
72
|
|
|
$interfaces = $this->interfaces; |
|
73
|
|
|
$attributes = $this->attributes; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($files as $file) { |
|
76
|
|
|
$nodes = $this->parser->parse(file_get_contents($file->getRealPath())); |
|
77
|
|
|
$this->traverser->traverse($nodes); |
|
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var $result Node\Stmt\Class_[] |
|
80
|
|
|
*/ |
|
81
|
|
|
$result = $this->nodeFinder->find( |
|
82
|
|
|
$nodes, |
|
83
|
|
|
function (Node $node) use ($interfaces, $countInterfaces, $attributes, $countAttributes) { |
|
84
|
|
|
if (!$node instanceof Node\Stmt\Class_) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
$interfacesNames = array_map(fn (Node\Name $name) => $name->toString(), $node->implements); |
|
88
|
|
|
if (count(array_intersect($interfaces, $interfacesNames)) !== $countInterfaces) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
$attributesNames = []; |
|
92
|
|
|
foreach ($node->attrGroups as $attrGroup) { |
|
93
|
|
|
foreach ($attrGroup->attrs as $attr) { |
|
94
|
|
|
$attributesNames[] = $attr->name->toString(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return !(count(array_intersect($attributes, $attributesNames)) !== $countAttributes) |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
); |
|
103
|
|
|
foreach ($result as $class) { |
|
104
|
|
|
yield $class->namespacedName->toString(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|