|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Resource\Exception\ResourceDirException; |
|
8
|
|
|
use Iterator; |
|
9
|
|
|
use Override; |
|
10
|
|
|
use RecursiveDirectoryIterator; |
|
11
|
|
|
use RecursiveIteratorIterator; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
use SplFileInfo; |
|
14
|
|
|
|
|
15
|
|
|
use function array_diff_key; |
|
16
|
|
|
use function array_key_exists; |
|
17
|
|
|
use function array_keys; |
|
18
|
|
|
use function array_values; |
|
19
|
|
|
use function assert; |
|
20
|
|
|
use function class_exists; |
|
21
|
|
|
use function file_exists; |
|
22
|
|
|
use function get_declared_classes; |
|
23
|
|
|
use function str_contains; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @psalm-import-type ResourceClassName from Types |
|
27
|
|
|
* @psalm-import-type MetaMap from Types |
|
28
|
|
|
* @psalm-import-type ClassNameList from Types |
|
29
|
|
|
* @implements Iterator<string, Meta> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class AppIterator implements Iterator |
|
32
|
|
|
{ |
|
33
|
|
|
private int $i = 0; |
|
34
|
|
|
|
|
35
|
|
|
/** @var MetaMap */ |
|
|
|
|
|
|
36
|
|
|
private array $metaCollection = []; |
|
37
|
|
|
|
|
38
|
|
|
/** @var list<string> */ |
|
|
|
|
|
|
39
|
|
|
private array $keys = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @throws ResourceDirException */ |
|
42
|
|
|
public function __construct(string $resourceDir) |
|
43
|
|
|
{ |
|
44
|
|
|
if (! file_exists($resourceDir)) { |
|
45
|
|
|
throw new ResourceDirException($resourceDir); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
49
|
|
|
new RecursiveDirectoryIterator($resourceDir), |
|
50
|
|
|
RecursiveIteratorIterator::SELF_FIRST, |
|
51
|
|
|
); |
|
52
|
|
|
$this->metaCollection = $this->getMetaCollection($iterator); |
|
|
|
|
|
|
53
|
|
|
$this->keys = array_keys($this->metaCollection); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
#[Override] |
|
60
|
|
|
public function current(): Meta |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->metaCollection[$this->keys[$this->i]]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritDoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
#[Override] |
|
69
|
|
|
public function next(): void |
|
70
|
|
|
{ |
|
71
|
|
|
++$this->i; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
#[Override] |
|
78
|
|
|
public function key(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->keys[$this->i]; // @codeCoverageIgnore |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
#[Override] |
|
87
|
|
|
public function valid(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return array_key_exists($this->i, $this->keys); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
#[Override] |
|
96
|
|
|
public function rewind(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->i = 0; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Iterator<SplFileInfo> $iterator |
|
103
|
|
|
* |
|
104
|
|
|
* @return MetaMap |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getMetaCollection(Iterator $iterator): array |
|
107
|
|
|
{ |
|
108
|
|
|
$metaCollection = []; |
|
109
|
|
|
foreach ($iterator as $item) { |
|
110
|
|
|
if ($this->isNotPhp($item)) { |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$resourceClass = $this->getResourceClassName($item); |
|
115
|
|
|
if ($resourceClass === '' || ! class_exists($resourceClass)) { |
|
116
|
|
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$meta = new Meta($resourceClass); |
|
120
|
|
|
$metaCollection[$meta->uri] = $meta; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $metaCollection; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function isNotPhp(SplFileInfo $item): bool |
|
127
|
|
|
{ |
|
128
|
|
|
$isPhp = $item->isFile() |
|
129
|
|
|
&& $item->getExtension() === 'php' |
|
130
|
|
|
&& (! str_contains($item->getBasename('.php'), '.')); |
|
131
|
|
|
|
|
132
|
|
|
return ! $isPhp; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** @return ResourceClassName|'' */ |
|
|
|
|
|
|
136
|
|
|
private function getResourceClassName(SplFileInfo $file): string |
|
137
|
|
|
{ |
|
138
|
|
|
$pathName = $file->getPathname(); |
|
139
|
|
|
$declaredClasses = get_declared_classes(); |
|
140
|
|
|
assert(file_exists($pathName)); |
|
141
|
|
|
include_once $pathName; |
|
142
|
|
|
$newClasses = array_values(array_diff_key(get_declared_classes(), $declaredClasses)); |
|
143
|
|
|
|
|
144
|
|
|
return $this->getName($newClasses); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param ClassNameList $newClasses |
|
|
|
|
|
|
149
|
|
|
* |
|
150
|
|
|
* @return ResourceClassName|'' |
|
|
|
|
|
|
151
|
|
|
*/ |
|
152
|
|
|
private function getName(array $newClasses): string |
|
153
|
|
|
{ |
|
154
|
|
|
foreach ($newClasses as $newClass) { |
|
155
|
|
|
$parent = (new ReflectionClass($newClass))->getParentClass(); |
|
156
|
|
|
if ($parent && $parent->name === ResourceObject::class) { |
|
157
|
|
|
/** @var ResourceClassName $newClass */ |
|
158
|
|
|
return $newClass; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return ''; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths