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 RecursiveDirectoryIterator; |
||
10 | use RecursiveIteratorIterator; |
||
11 | use ReflectionClass; |
||
12 | use SplFileInfo; |
||
13 | |||
14 | use function array_diff_key; |
||
15 | use function array_key_exists; |
||
16 | use function array_keys; |
||
17 | use function array_values; |
||
18 | use function assert; |
||
19 | use function class_exists; |
||
20 | use function file_exists; |
||
21 | use function get_declared_classes; |
||
22 | use function str_contains; |
||
23 | |||
24 | /** @implements Iterator<string, Meta> */ |
||
25 | final class AppIterator implements Iterator |
||
26 | { |
||
27 | private int $i = 0; |
||
28 | |||
29 | /** @var array<string, Meta> */ |
||
30 | private array $metaCollection = []; |
||
31 | |||
32 | /** @var list<string> */ |
||
0 ignored issues
–
show
|
|||
33 | private array $keys = []; |
||
34 | |||
35 | /** @throws ResourceDirException */ |
||
36 | public function __construct(string $resourceDir) |
||
37 | { |
||
38 | if (! file_exists($resourceDir)) { |
||
39 | throw new ResourceDirException($resourceDir); |
||
40 | } |
||
41 | |||
42 | $iterator = new RecursiveIteratorIterator( |
||
43 | new RecursiveDirectoryIterator($resourceDir), |
||
44 | RecursiveIteratorIterator::SELF_FIRST, |
||
45 | ); |
||
46 | $this->metaCollection = $this->getMetaCollection($iterator); |
||
47 | $this->keys = array_keys($this->metaCollection); |
||
0 ignored issues
–
show
It seems like
array_keys($this->metaCollection) of type array is incompatible with the declared type BEAR\Resource\list of property $keys .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | public function current(): Meta |
||
54 | { |
||
55 | return $this->metaCollection[$this->keys[$this->i]]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritDoc} |
||
60 | */ |
||
61 | public function next(): void |
||
62 | { |
||
63 | ++$this->i; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | public function key(): string |
||
70 | { |
||
71 | return $this->keys[$this->i]; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | public function valid(): bool |
||
78 | { |
||
79 | return array_key_exists($this->i, $this->keys); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function rewind(): void |
||
86 | { |
||
87 | $this->i = 0; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param Iterator<SplFileInfo> $iterator |
||
92 | * |
||
93 | * @return array<string, Meta> |
||
94 | */ |
||
95 | private function getMetaCollection(Iterator $iterator): array |
||
96 | { |
||
97 | $metaCollection = []; |
||
98 | foreach ($iterator as $item) { |
||
99 | if ($this->isNotPhp($item)) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | $resourceClass = $this->getResourceClassName($item); |
||
104 | if ($resourceClass === '') { |
||
105 | continue; |
||
106 | } |
||
107 | |||
108 | assert(class_exists($resourceClass)); |
||
109 | $meta = new Meta($resourceClass); |
||
110 | $metaCollection[$meta->uri] = $meta; |
||
111 | } |
||
112 | |||
113 | return $metaCollection; |
||
114 | } |
||
115 | |||
116 | private function isNotPhp(SplFileInfo $item): bool |
||
117 | { |
||
118 | $isPhp = $item->isFile() |
||
119 | && $item->getExtension() === 'php' |
||
120 | && (! str_contains($item->getBasename('.php'), '.')); |
||
121 | |||
122 | return ! $isPhp; |
||
123 | } |
||
124 | |||
125 | private function getResourceClassName(SplFileInfo $file): string |
||
126 | { |
||
127 | $pathName = $file->getPathname(); |
||
128 | $declaredClasses = get_declared_classes(); |
||
129 | assert(file_exists($pathName)); |
||
130 | include_once $pathName; |
||
131 | $newClasses = array_values(array_diff_key(get_declared_classes(), $declaredClasses)); |
||
132 | |||
133 | return $this->getName($newClasses); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param array<class-string> $newClasses |
||
0 ignored issues
–
show
|
|||
138 | * |
||
139 | * @return class-string|string |
||
0 ignored issues
–
show
|
|||
140 | */ |
||
141 | private function getName(array $newClasses): string |
||
142 | { |
||
143 | foreach ($newClasses as $newClass) { |
||
144 | $parent = (new ReflectionClass($newClass))->getParentClass(); |
||
145 | if ($parent && $parent->name === ResourceObject::class) { |
||
146 | return $newClass; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return ''; |
||
151 | } |
||
152 | } |
||
153 |
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