1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus; |
4
|
|
|
|
5
|
|
|
use Roave\BetterReflection\BetterReflection; |
6
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
7
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
8
|
|
|
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; |
9
|
|
|
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
10
|
|
|
use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator; |
11
|
|
|
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; |
12
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* get a list of all classes in the given directories. |
16
|
|
|
* |
17
|
|
|
* Based on: https://github.com/Roave/BetterReflection/blob/396a07c9d276cb9ffba581b24b2dadbb542d542e/demo/parsing-whole-directory/example2.php. |
18
|
|
|
* |
19
|
|
|
* @param string[] $directories |
20
|
|
|
* |
21
|
|
|
* @return iterable |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
function listClassesInDirectories(string ...$directories): iterable |
|
|
|
|
24
|
|
|
{ |
25
|
4 |
|
$sourceLocator = new AggregateSourceLocator([ |
26
|
4 |
|
new DirectoriesSourceLocator( |
27
|
4 |
|
$directories, |
|
|
|
|
28
|
4 |
|
(new BetterReflection())->astLocator() |
29
|
|
|
), |
30
|
|
|
// ↓ required to autoload parent classes/interface from another directory than /src (e.g. /vendor) |
31
|
4 |
|
new AutoloadSourceLocator((new BetterReflection())->astLocator()), |
32
|
|
|
]); |
33
|
|
|
|
34
|
4 |
|
foreach ((new ClassReflector($sourceLocator))->getAllClasses() as $class) { |
35
|
4 |
|
yield $class->getName(); |
36
|
|
|
} |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* get a list of all classes in the given directory. |
41
|
|
|
* |
42
|
|
|
* @param string $directory |
43
|
|
|
* |
44
|
|
|
* @return iterable |
45
|
|
|
*/ |
46
|
|
|
function listClassesInDirectory(string $directory): iterable |
47
|
|
|
{ |
48
|
1 |
|
yield from listClassesInDirectories($directory); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* get a list of all classes in the given file. |
53
|
|
|
* |
54
|
|
|
* @param string $file |
55
|
|
|
* |
56
|
|
|
* @return iterable |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
function listClassesInFile(string $file): iterable |
|
|
|
|
59
|
|
|
{ |
60
|
2 |
|
$sourceLocator = new AggregateSourceLocator([ |
61
|
2 |
|
new SingleFileSourceLocator( |
62
|
2 |
|
$file, |
63
|
2 |
|
(new BetterReflection())->astLocator() |
64
|
|
|
), |
65
|
|
|
// ↓ required to autoload parent classes/interface from another directory (e.g. /vendor) |
66
|
2 |
|
new AutoloadSourceLocator((new BetterReflection())->astLocator()), |
67
|
|
|
]); |
68
|
|
|
|
69
|
2 |
|
foreach ((new ClassReflector($sourceLocator))->getAllClasses() as $class) { |
70
|
2 |
|
yield $class->getName(); |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* get a list of all classes in the given files. |
76
|
|
|
* |
77
|
|
|
* @param string[] $files |
78
|
|
|
* |
79
|
|
|
* @return iterable |
80
|
|
|
*/ |
81
|
|
|
function listClassesInFiles(string ...$files): iterable |
82
|
|
|
{ |
83
|
1 |
|
foreach ($files as $file) { |
84
|
1 |
|
foreach (listClassesInFile($file) as $class) { |
85
|
1 |
|
yield $class; |
86
|
|
|
} |
87
|
|
|
} |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
function listInstantiatableClassesInDirectories(string ...$directories): iterable |
|
|
|
|
91
|
|
|
{ |
92
|
1 |
|
$iterator = listClassesInDirectories(...$directories); |
93
|
|
|
|
94
|
|
|
return new class($iterator) extends \FilterIterator { |
95
|
|
|
public function accept(): bool |
96
|
|
|
{ |
97
|
1 |
|
$className = $this->getInnerIterator()->current(); |
98
|
|
|
try { |
99
|
1 |
|
$reflectionClass = ReflectionClass::createFromName($className); |
100
|
|
|
|
101
|
1 |
|
return $reflectionClass->isInstantiable(); |
102
|
|
|
} catch (IdentifierNotFound $exception) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
}; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
function listInstantiatableClassesInDirectory(string $directory): iterable |
110
|
|
|
{ |
111
|
1 |
|
yield from listInstantiatableClassesInDirectories($directory); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
function listNonInstantiatableClassesInDirectories(string ...$directories): iterable |
|
|
|
|
115
|
|
|
{ |
116
|
1 |
|
$iterator = listClassesInDirectories(...$directories); |
117
|
|
|
|
118
|
|
|
return new class($iterator) extends \FilterIterator { |
119
|
|
|
public function accept(): bool |
120
|
|
|
{ |
121
|
1 |
|
$className = $this->getInnerIterator()->current(); |
122
|
|
|
try { |
123
|
1 |
|
$reflectionClass = ReflectionClass::createFromName($className); |
124
|
|
|
|
125
|
1 |
|
return $reflectionClass->isInstantiable() === false; |
126
|
|
|
} catch (IdentifierNotFound $exception) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
}; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
function listNonInstantiatableClassesInDirectory(string $directory): iterable |
134
|
|
|
{ |
135
|
1 |
|
yield from listNonInstantiatableClassesInDirectories($directory); |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.