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 | * @implements Iterator<string, Meta> |
||
28 | */ |
||
29 | final class AppIterator implements Iterator |
||
30 | { |
||
31 | private int $i = 0; |
||
32 | |||
33 | /** @var array<string, Meta> */ |
||
34 | private array $metaCollection = []; |
||
35 | |||
36 | /** @var list<string> */ |
||
0 ignored issues
–
show
|
|||
37 | private array $keys = []; |
||
38 | |||
39 | /** @throws ResourceDirException */ |
||
40 | public function __construct(string $resourceDir) |
||
41 | { |
||
42 | if (! file_exists($resourceDir)) { |
||
43 | throw new ResourceDirException($resourceDir); |
||
44 | } |
||
45 | |||
46 | $iterator = new RecursiveIteratorIterator( |
||
47 | new RecursiveDirectoryIterator($resourceDir), |
||
48 | RecursiveIteratorIterator::SELF_FIRST, |
||
49 | ); |
||
50 | $this->metaCollection = $this->getMetaCollection($iterator); |
||
51 | $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.. ![]() |
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | #[Override] |
||
58 | public function current(): Meta |
||
59 | { |
||
60 | return $this->metaCollection[$this->keys[$this->i]]; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | #[Override] |
||
67 | public function next(): void |
||
68 | { |
||
69 | ++$this->i; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | #[Override] |
||
76 | public function key(): string |
||
77 | { |
||
78 | return $this->keys[$this->i]; // @codeCoverageIgnore |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | #[Override] |
||
85 | public function valid(): bool |
||
86 | { |
||
87 | return array_key_exists($this->i, $this->keys); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritDoc} |
||
92 | */ |
||
93 | #[Override] |
||
94 | public function rewind(): void |
||
95 | { |
||
96 | $this->i = 0; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param Iterator<SplFileInfo> $iterator |
||
101 | * |
||
102 | * @return array<string, Meta> |
||
103 | */ |
||
104 | private function getMetaCollection(Iterator $iterator): array |
||
105 | { |
||
106 | $metaCollection = []; |
||
107 | foreach ($iterator as $item) { |
||
108 | if ($this->isNotPhp($item)) { |
||
109 | continue; |
||
110 | } |
||
111 | |||
112 | $resourceClass = $this->getResourceClassName($item); |
||
113 | if ($resourceClass === '' || ! class_exists($resourceClass)) { |
||
114 | continue; |
||
115 | } |
||
116 | |||
117 | $meta = new Meta($resourceClass); |
||
118 | $metaCollection[$meta->uri] = $meta; |
||
119 | } |
||
120 | |||
121 | return $metaCollection; |
||
122 | } |
||
123 | |||
124 | private function isNotPhp(SplFileInfo $item): bool |
||
125 | { |
||
126 | $isPhp = $item->isFile() |
||
127 | && $item->getExtension() === 'php' |
||
128 | && (! str_contains($item->getBasename('.php'), '.')); |
||
129 | |||
130 | return ! $isPhp; |
||
131 | } |
||
132 | |||
133 | /** @return ResourceClassName|'' */ |
||
0 ignored issues
–
show
|
|||
134 | private function getResourceClassName(SplFileInfo $file): string |
||
135 | { |
||
136 | $pathName = $file->getPathname(); |
||
137 | $declaredClasses = get_declared_classes(); |
||
138 | assert(file_exists($pathName)); |
||
139 | include_once $pathName; |
||
140 | $newClasses = array_values(array_diff_key(get_declared_classes(), $declaredClasses)); |
||
141 | |||
142 | return $this->getName($newClasses); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param array<class-string> $newClasses |
||
0 ignored issues
–
show
|
|||
147 | * |
||
148 | * @return ResourceClassName|'' |
||
0 ignored issues
–
show
|
|||
149 | */ |
||
150 | private function getName(array $newClasses): string |
||
151 | { |
||
152 | foreach ($newClasses as $newClass) { |
||
153 | $parent = (new ReflectionClass($newClass))->getParentClass(); |
||
154 | if ($parent && $parent->name === ResourceObject::class) { |
||
155 | /** @var ResourceClassName $newClass */ |
||
156 | return $newClass; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return ''; |
||
161 | } |
||
162 | } |
||
163 |
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