Total Complexity | 42 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Collection implements Contract |
||
40 | { |
||
41 | /** |
||
42 | * The routes. |
||
43 | * |
||
44 | * @var array<string, Route|string> |
||
45 | */ |
||
46 | protected array $routes = []; |
||
47 | |||
48 | /** |
||
49 | * The static routes. |
||
50 | * |
||
51 | * @var RequestMethodList |
||
52 | */ |
||
53 | protected array $static = []; |
||
54 | |||
55 | /** |
||
56 | * The dynamic routes. |
||
57 | * |
||
58 | * @var RequestMethodList |
||
59 | */ |
||
60 | protected array $dynamic = []; |
||
61 | |||
62 | /** |
||
63 | * The named routes. |
||
64 | * |
||
65 | * @var array<string, string> |
||
66 | */ |
||
67 | protected array $named = []; |
||
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | public function getData(): Data |
||
73 | { |
||
74 | $data = new Data(); |
||
75 | |||
76 | $data->routes = []; |
||
77 | $data->static = $this->static; |
||
78 | $data->dynamic = $this->dynamic; |
||
79 | $data->named = $this->named; |
||
80 | |||
81 | foreach ($this->routes as $id => $route) { |
||
82 | $data->routes[$id] = serialize($route); |
||
83 | } |
||
84 | |||
85 | return $data; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function setFromData(Data $data): void |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritDoc |
||
101 | */ |
||
102 | public function add(Route $route): void |
||
103 | { |
||
104 | assert($route->getPath()); |
||
105 | |||
106 | // Set the route to its request methods |
||
107 | $this->setRouteToRequestMethods($route); |
||
108 | // Set the route to the named |
||
109 | $this->setRouteToNamed($route); |
||
110 | |||
111 | $this->routes[$route->getName()] = $route; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @inheritDoc |
||
116 | * |
||
117 | * @throws JsonException |
||
118 | */ |
||
119 | public function get(string $path, RequestMethod|null $method = null): Route|null |
||
120 | { |
||
121 | return $this->getStatic($path, $method) ?? $this->getDynamic($path, $method); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | public function isset(string $path, RequestMethod|null $method = null): bool |
||
128 | { |
||
129 | return $this->hasStatic($path, $method) || $this->hasDynamic($path, $method); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | public function all(): array |
||
136 | { |
||
137 | return $this->ensureMethodRoutes(array_merge($this->static, $this->dynamic)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function allFlattened(): array |
||
144 | { |
||
145 | return $this->ensureRoutes($this->routes); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | * |
||
151 | * @throws JsonException |
||
152 | */ |
||
153 | public function getStatic(string $path, RequestMethod|null $method = null): Route|null |
||
154 | { |
||
155 | return $this->getOfType($this->static, $path, $method); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @inheritDoc |
||
160 | */ |
||
161 | public function hasStatic(string $path, RequestMethod|null $method = null): bool |
||
162 | { |
||
163 | return $this->hasOfType($this->static, $path, $method); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @inheritDoc |
||
168 | */ |
||
169 | public function allStatic(RequestMethod|null $method = null): array |
||
170 | { |
||
171 | return $this->allOfType($this->static, $method); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritDoc |
||
176 | * |
||
177 | * @throws JsonException |
||
178 | */ |
||
179 | public function getDynamic(string $regex, RequestMethod|null $method = null): Route|null |
||
180 | { |
||
181 | return $this->getOfType($this->dynamic, $regex, $method); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @inheritDoc |
||
186 | */ |
||
187 | public function hasDynamic(string $regex, RequestMethod|null $method = null): bool |
||
188 | { |
||
189 | return $this->hasOfType($this->dynamic, $regex, $method); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @inheritDoc |
||
194 | */ |
||
195 | public function allDynamic(RequestMethod|null $method = null): array |
||
196 | { |
||
197 | return $this->allOfType($this->dynamic, $method); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @inheritDoc |
||
202 | */ |
||
203 | public function getNamed(string $name): Route|null |
||
204 | { |
||
205 | $named = $this->named[$name] ?? null; |
||
206 | |||
207 | if ($named === null) { |
||
208 | return null; |
||
209 | } |
||
210 | |||
211 | return $this->ensureRoute($named); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @inheritDoc |
||
216 | */ |
||
217 | public function hasNamed(string $name): bool |
||
218 | { |
||
219 | return isset($this->named[$name]); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @inheritDoc |
||
224 | */ |
||
225 | public function allNamed(): array |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Set a route to its request methods. |
||
232 | * |
||
233 | * @param Route $route The route |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | protected function setRouteToRequestMethods(Route $route): void |
||
238 | { |
||
239 | foreach ($route->getRequestMethods() as $requestMethod) { |
||
240 | $this->setRouteToRequestMethod($route, $requestMethod); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Set the route to the request method. |
||
246 | * |
||
247 | * @param Route $route The route |
||
248 | * @param RequestMethod $requestMethod The request method |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | protected function setRouteToRequestMethod(Route $route, RequestMethod $requestMethod): void |
||
253 | { |
||
254 | $name = $route->getName(); |
||
255 | $regex = $route->getRegex(); |
||
256 | |||
257 | // If this is a dynamic route |
||
258 | if ($regex !== null) { |
||
259 | // Set the route in the dynamic routes list |
||
260 | $this->dynamic[$requestMethod->value][$regex] = $name; |
||
261 | } // Otherwise set it in the static routes array |
||
262 | else { |
||
263 | // Set the route in the static routes list |
||
264 | $this->static[$requestMethod->value][$route->getPath()] = $name; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Set the route to the named. |
||
270 | * |
||
271 | * @param Route $route The route |
||
272 | * |
||
273 | * @return void |
||
274 | */ |
||
275 | protected function setRouteToNamed(Route $route): void |
||
276 | { |
||
277 | $name = $route->getName(); |
||
278 | |||
279 | // Set the route in the named routes list |
||
280 | $this->named[$name] = $name; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Get a route of type (static|dynamic). |
||
285 | * |
||
286 | * @param RequestMethodList $type The type [static|dynamic] |
||
287 | * @param string $path The path |
||
288 | * @param RequestMethod|null $method [optional] The request method |
||
289 | * |
||
290 | * @throws JsonException |
||
291 | * |
||
292 | * @return Route|null |
||
293 | */ |
||
294 | protected function getOfType(array $type, string $path, RequestMethod|null $method = null): Route|null |
||
295 | { |
||
296 | if ($method === null) { |
||
297 | return $this->getAnyOfType($type, $path); |
||
298 | } |
||
299 | |||
300 | $route = $type[$method->value][$path] ?? null; |
||
301 | |||
302 | if ($route === null) { |
||
303 | return null; |
||
304 | } |
||
305 | |||
306 | return $this->ensureRoute($route); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Get a route of any type (static|dynamic). |
||
311 | * |
||
312 | * @param RequestMethodList $type The type [static|dynamic] |
||
313 | * @param string $path The path |
||
314 | * |
||
315 | * @throws JsonException |
||
316 | * |
||
317 | * @return Route|null |
||
318 | */ |
||
319 | protected function getAnyOfType(array $type, string $path): Route|null |
||
320 | { |
||
321 | return $this->getOfType($type, $path, RequestMethod::GET) |
||
322 | ?? $this->getOfType($type, $path, RequestMethod::HEAD) |
||
323 | ?? $this->getOfType($type, $path, RequestMethod::POST) |
||
324 | ?? $this->getOfType($type, $path, RequestMethod::PUT) |
||
325 | ?? $this->getOfType($type, $path, RequestMethod::PATCH) |
||
326 | ?? $this->getOfType($type, $path, RequestMethod::DELETE); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Has a path of type (static|dynamic). |
||
331 | * |
||
332 | * @param RequestMethodList $type The type [static|dynamic] |
||
333 | * @param string $path The path |
||
334 | * @param RequestMethod|null $method [optional] The request method |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | protected function hasOfType(array $type, string $path, RequestMethod|null $method = null): bool |
||
339 | { |
||
340 | if ($method === null) { |
||
341 | return $this->hasAnyOfType($type, $path); |
||
342 | } |
||
343 | |||
344 | return isset($type[$method->value][$path]); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Has a path of any type. |
||
349 | * |
||
350 | * @param RequestMethodList $type The type [static|dynamic] |
||
351 | * @param string $path The path |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | protected function hasAnyOfType(array $type, string $path): bool |
||
356 | { |
||
357 | foreach ($type as $requestMethod) { |
||
358 | if (isset($requestMethod[$path])) { |
||
359 | return true; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | return false; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Get all of type with optional by request method. |
||
368 | * |
||
369 | * @param RequestMethodList $type The type [static|dynamic] |
||
370 | * @param RequestMethod|null $method [optional] The request method |
||
371 | * |
||
372 | * @return RequestMethodRouteList|array<string, Route> |
||
373 | */ |
||
374 | protected function allOfType(array $type, RequestMethod|null $method = null): array |
||
375 | { |
||
376 | if ($method === null) { |
||
377 | return $this->ensureMethodRoutes($type); |
||
378 | } |
||
379 | |||
380 | return $this->ensureRoutes($type[$method->value] ?? []); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Ensure request methods are arrays of routes. |
||
385 | * |
||
386 | * @param RequestMethodList $methodsArray |
||
387 | * |
||
388 | * @return RequestMethodRouteList |
||
389 | */ |
||
390 | protected function ensureMethodRoutes(array $methodsArray): array |
||
391 | { |
||
392 | return array_map( |
||
393 | [$this, 'ensureRoutes'], |
||
394 | $methodsArray |
||
395 | ); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Ensure an array is an array of routes. |
||
400 | * |
||
401 | * @param array<string, string|Route> $routesArray The routes array |
||
402 | * |
||
403 | * @return array<string, Route> |
||
404 | */ |
||
405 | protected function ensureRoutes(array $routesArray): array |
||
410 | ); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Ensure a route, or null, is returned. |
||
415 | * |
||
416 | * @param Route|string $route The route |
||
417 | * |
||
418 | * @return Route |
||
419 | */ |
||
420 | protected function ensureRoute(Route|string $route): Route |
||
421 | { |
||
437 | } |
||
438 | } |
||
439 |
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