Total Complexity | 68 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Validator |
||
44 | { |
||
45 | /** @var array<int|string|Tree|UserInterface|array<int|string>> */ |
||
46 | private array $parameters; |
||
47 | |||
48 | private ServerRequestInterface $request; |
||
49 | |||
50 | /** @var array<Closure> */ |
||
51 | private array $rules = []; |
||
52 | |||
53 | /** |
||
54 | * @param array<int|string|Tree|UserInterface|array<int|string>> $parameters |
||
55 | * @param ServerRequestInterface $request |
||
56 | * @param string $encoding |
||
57 | */ |
||
58 | private function __construct(array $parameters, ServerRequestInterface $request, string $encoding) |
||
59 | { |
||
60 | if ($encoding === 'UTF-8') { |
||
61 | // All keys and values must be valid UTF-8 |
||
62 | $check_utf8 = static function ($value, $key): void { |
||
63 | if (is_string($key) && preg_match('//u', $key) !== 1) { |
||
64 | throw new HttpBadRequestException('Invalid UTF-8 characters in request'); |
||
65 | } |
||
66 | if (is_string($value) && preg_match('//u', $value) !== 1) { |
||
67 | throw new HttpBadRequestException('Invalid UTF-8 characters in request'); |
||
68 | } |
||
69 | }; |
||
70 | |||
71 | array_walk_recursive($parameters, $check_utf8); |
||
72 | } |
||
73 | |||
74 | $this->parameters = $parameters; |
||
75 | $this->request = $request; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param ServerRequestInterface $request |
||
80 | * |
||
81 | * @return self |
||
82 | */ |
||
83 | public static function attributes(ServerRequestInterface $request): self |
||
84 | { |
||
85 | return new self($request->getAttributes(), $request, 'UTF-8'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param ServerRequestInterface $request |
||
90 | * |
||
91 | * @return self |
||
92 | */ |
||
93 | public static function parsedBody(ServerRequestInterface $request): self |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param ServerRequestInterface $request |
||
100 | * |
||
101 | * @return self |
||
102 | */ |
||
103 | public static function queryParams(ServerRequestInterface $request): self |
||
104 | { |
||
105 | return new self($request->getQueryParams(), $request, 'UTF-8'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param ServerRequestInterface $request |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | public static function serverParams(ServerRequestInterface $request): self |
||
114 | { |
||
115 | // Headers should be ASCII. |
||
116 | // However, we cannot enforce this as some servers add GEOIP headers with non-ASCII placenames. |
||
117 | return new self($request->getServerParams(), $request, 'ASCII'); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param int $minimum |
||
122 | * @param int $maximum |
||
123 | * |
||
124 | * @return self |
||
125 | */ |
||
126 | public function isBetween(int $minimum, int $maximum): self |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param array<int|string,int|string> $values |
||
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | public function isInArray(array $values): self |
||
145 | { |
||
146 | $this->rules[] = static fn (int|string|null $value): int|string|null => in_array($value, $values, true) ? $value : null; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param array<int|string,int|string> $values |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | public function isInArrayKeys(array $values): self |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return self |
||
163 | */ |
||
164 | public function isNotEmpty(): self |
||
165 | { |
||
166 | $this->rules[] = static fn (string|null $value): string|null => $value !== null && $value !== '' ? $value : null; |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return self |
||
173 | */ |
||
174 | public function isLocalUrl(): self |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return self |
||
204 | */ |
||
205 | public function isTag(): self |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return self |
||
220 | */ |
||
221 | public function isXref(): self |
||
222 | { |
||
223 | $this->rules[] = static function ($value) { |
||
224 | if (is_string($value) && preg_match('/^' . Gedcom::REGEX_XREF . '$/', $value) === 1) { |
||
225 | return $value; |
||
226 | } |
||
227 | |||
228 | if (is_array($value)) { |
||
229 | foreach ($value as $v) { |
||
230 | if (!is_string($v) || preg_match('/^' . Gedcom::REGEX_XREF . '$/', $v) !== 1) { |
||
231 | return null; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | return $value; |
||
236 | } |
||
237 | |||
238 | return null; |
||
239 | }; |
||
240 | |||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string $parameter |
||
246 | * @param bool|null $default |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function boolean(string $parameter, bool|null $default = null): bool |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $parameter |
||
271 | * |
||
272 | * @return array<string> |
||
273 | */ |
||
274 | public function array(string $parameter): array |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param string $parameter |
||
289 | * @param float|null $default |
||
290 | * |
||
291 | * @return float |
||
292 | */ |
||
293 | public function float(string $parameter, float|null $default = null): float |
||
294 | { |
||
295 | $value = $this->parameters[$parameter] ?? null; |
||
296 | |||
297 | if (is_numeric($value)) { |
||
298 | $value = (float) $value; |
||
299 | } else { |
||
300 | $value = null; |
||
301 | } |
||
302 | |||
303 | $callback = static fn (float|null $value, Closure $rule): float|null => $rule($value); |
||
304 | |||
305 | $value = array_reduce($this->rules, $callback, $value) ?? $default; |
||
306 | |||
307 | if ($value === null) { |
||
308 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
309 | } |
||
310 | |||
311 | return $value; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param string $parameter |
||
316 | * @param int|null $default |
||
317 | * |
||
318 | * @return int |
||
319 | */ |
||
320 | public function integer(string $parameter, int|null $default = null): int |
||
321 | { |
||
322 | $value = $this->parameters[$parameter] ?? null; |
||
323 | |||
324 | if (is_string($value)) { |
||
325 | if (ctype_digit($value)) { |
||
326 | $value = (int) $value; |
||
327 | } elseif (str_starts_with($value, '-') && ctype_digit(substr($value, 1))) { |
||
328 | $value = (int) $value; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | if (!is_int($value)) { |
||
333 | $value = null; |
||
334 | } |
||
335 | |||
336 | $callback = static fn (int|null $value, Closure $rule): int|null => $rule($value); |
||
337 | |||
338 | $value = array_reduce($this->rules, $callback, $value) ?? $default; |
||
339 | |||
340 | if ($value === null) { |
||
341 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
342 | } |
||
343 | |||
344 | return $value; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param string $parameter |
||
349 | * |
||
350 | * @return Route |
||
351 | */ |
||
352 | public function route(string $parameter = 'route'): Route |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $parameter |
||
365 | * @param string|null $default |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | public function string(string $parameter, string|null $default = null): string |
||
370 | { |
||
371 | $value = $this->parameters[$parameter] ?? null; |
||
372 | |||
373 | if (!is_string($value)) { |
||
374 | $value = null; |
||
375 | } |
||
376 | |||
377 | $callback = static fn (string|null $value, Closure $rule): string|null => $rule($value); |
||
378 | |||
379 | $value = array_reduce($this->rules, $callback, $value) ?? $default; |
||
380 | |||
381 | if ($value === null) { |
||
382 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
383 | } |
||
384 | |||
385 | return $value; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @param string $parameter |
||
390 | * |
||
391 | * @return Tree |
||
392 | */ |
||
393 | public function tree(string $parameter = 'tree'): Tree |
||
402 | } |
||
403 | |||
404 | public function treeOptional(string $parameter = 'tree'): Tree|null |
||
405 | { |
||
406 | $value = $this->parameters[$parameter] ?? null; |
||
407 | |||
408 | if ($value === null || $value instanceof Tree) { |
||
409 | return $value; |
||
410 | } |
||
411 | |||
412 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
413 | } |
||
414 | |||
415 | public function user(string $parameter = 'user'): UserInterface |
||
424 | } |
||
425 | } |
||
426 |
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