| Total Complexity | 62 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 42 | class Validator |
||
| 43 | { |
||
| 44 | /** @var array<int|string|Tree|UserInterface|array<int|string>> */ |
||
| 45 | private array $parameters; |
||
| 46 | |||
| 47 | private ServerRequestInterface $request; |
||
| 48 | |||
| 49 | /** @var array<Closure> */ |
||
| 50 | private array $rules = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array<int|string|Tree|UserInterface|array<int|string>> $parameters |
||
| 54 | * @param ServerRequestInterface $request |
||
| 55 | */ |
||
| 56 | public function __construct(array $parameters, ServerRequestInterface $request) |
||
| 57 | { |
||
| 58 | $this->parameters = $parameters; |
||
| 59 | $this->request = $request; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param ServerRequestInterface $request |
||
| 64 | * |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | public static function attributes(ServerRequestInterface $request): self |
||
| 68 | { |
||
| 69 | return new self($request->getAttributes(), $request); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param ServerRequestInterface $request |
||
| 74 | * |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | public static function parsedBody(ServerRequestInterface $request): self |
||
| 78 | { |
||
| 79 | return new self((array) $request->getParsedBody(), $request); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param ServerRequestInterface $request |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public static function queryParams(ServerRequestInterface $request): self |
||
| 88 | { |
||
| 89 | return new self($request->getQueryParams(), $request); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param ServerRequestInterface $request |
||
| 94 | * |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | public static function serverParams(ServerRequestInterface $request): self |
||
| 98 | { |
||
| 99 | return new self($request->getServerParams(), $request); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $minimum |
||
| 104 | * @param int $maximum |
||
| 105 | * |
||
| 106 | * @return self |
||
| 107 | */ |
||
| 108 | public function isBetween(int $minimum, int $maximum): self |
||
| 109 | { |
||
| 110 | $this->rules[] = static function (?int $value) use ($minimum, $maximum): ?int { |
||
| 111 | if (is_int($value) && $value >= $minimum && $value <= $maximum) { |
||
| 112 | return $value; |
||
| 113 | } |
||
| 114 | |||
| 115 | return null; |
||
| 116 | }; |
||
| 117 | |||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array<string> $values |
||
| 123 | * |
||
| 124 | * @return self |
||
| 125 | */ |
||
| 126 | public function isInArray(array $values): self |
||
| 127 | { |
||
| 128 | $this->rules[] = static fn (/*int|string|null*/ $value)/*: int|string|null*/ => $value !== null && in_array($value, $values, true) ? $value : null; |
||
| 129 | |||
| 130 | return $this; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param array<string> $values |
||
| 135 | * |
||
| 136 | * @return self |
||
| 137 | */ |
||
| 138 | public function isInArrayKeys(array $values): self |
||
| 139 | { |
||
| 140 | return $this->isInArray(array_keys($values)); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | public function isNotEmpty(): self |
||
| 147 | { |
||
| 148 | $this->rules[] = static fn (?string $value): ?string => $value !== null && $value !== '' ? $value : null; |
||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return self |
||
| 155 | */ |
||
| 156 | public function isLocalUrl(): self |
||
| 157 | { |
||
| 158 | $base_url = $this->request->getAttribute('base_url', ''); |
||
| 159 | |||
| 160 | $this->rules[] = static function (?string $value) use ($base_url): ?string { |
||
| 161 | if ($value !== null) { |
||
| 162 | $value_info = parse_url($value); |
||
| 163 | $base_url_info = parse_url($base_url); |
||
| 164 | |||
| 165 | if (is_array($value_info) && is_array($base_url_info)) { |
||
|
|
|||
| 166 | $scheme_ok = ($value_info['scheme'] ?? 'http') === ($base_url_info['scheme'] ?? 'http'); |
||
| 167 | $host_ok = ($value_info['host'] ?? '') === ($base_url_info['host'] ?? ''); |
||
| 168 | $port_ok = ($value_info['port'] ?? '') === ($base_url_info['port'] ?? ''); |
||
| 169 | $user_ok = ($value_info['user'] ?? '') === ($base_url_info['user'] ?? ''); |
||
| 170 | $path_ok = str_starts_with($value_info['path'] ?? '/', $base_url_info['path'] ?? '/'); |
||
| 171 | |||
| 172 | if ($scheme_ok && $host_ok && $port_ok && $user_ok && $path_ok) { |
||
| 173 | return $value; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return null; |
||
| 179 | }; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return self |
||
| 186 | */ |
||
| 187 | public function isTag(): self |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return self |
||
| 202 | */ |
||
| 203 | public function isXref(): self |
||
| 204 | { |
||
| 205 | $this->rules[] = static function (?string $value): ?string { |
||
| 206 | if ($value !== null && preg_match('/^' . Gedcom::REGEX_XREF . '$/', $value) === 1) { |
||
| 207 | return $value; |
||
| 208 | } |
||
| 209 | |||
| 210 | return null; |
||
| 211 | }; |
||
| 212 | |||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $parameter |
||
| 218 | * @param bool|null $default |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function boolean(string $parameter, bool $default = null): bool |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $parameter |
||
| 243 | * |
||
| 244 | * @return array<string> |
||
| 245 | */ |
||
| 246 | public function array(string $parameter): array |
||
| 247 | { |
||
| 248 | $value = $this->parameters[$parameter] ?? null; |
||
| 249 | |||
| 250 | if (!is_array($value) && $value !== null) { |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $parameter |
||
| 272 | * @param int|null $default |
||
| 273 | * |
||
| 274 | * @return int |
||
| 275 | */ |
||
| 276 | public function integer(string $parameter, int $default = null): int |
||
| 277 | { |
||
| 278 | $value = $this->parameters[$parameter] ?? null; |
||
| 279 | |||
| 280 | if (is_string($value)) { |
||
| 281 | if (ctype_digit($value)) { |
||
| 282 | $value = (int) $value; |
||
| 283 | } elseif (str_starts_with($value, '-') && ctype_digit(substr($value, 1))) { |
||
| 284 | $value = (int) $value; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!is_int($value)) { |
||
| 289 | $value = null; |
||
| 290 | } |
||
| 291 | |||
| 292 | $callback = static fn (?int $value, Closure $rule): ?int => $rule($value); |
||
| 293 | |||
| 294 | $value = array_reduce($this->rules, $callback, $value); |
||
| 295 | |||
| 296 | $value ??= $default; |
||
| 297 | |||
| 298 | if ($value === null) { |
||
| 299 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $value; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $parameter |
||
| 307 | * |
||
| 308 | * @return Route |
||
| 309 | */ |
||
| 310 | public function route(string $parameter = 'route'): Route |
||
| 311 | { |
||
| 312 | $value = $this->parameters[$parameter] ?? null; |
||
| 313 | |||
| 314 | if ($value instanceof Route) { |
||
| 315 | return $value; |
||
| 316 | } |
||
| 317 | |||
| 318 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $parameter |
||
| 323 | * @param string|null $default |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function string(string $parameter, string $default = null): string |
||
| 328 | { |
||
| 329 | $value = $this->parameters[$parameter] ?? null; |
||
| 330 | |||
| 331 | if (!is_string($value)) { |
||
| 332 | $value = null; |
||
| 333 | } |
||
| 334 | |||
| 335 | $callback = static fn (?string $value, Closure $rule): ?string => $rule($value); |
||
| 336 | |||
| 337 | $value = array_reduce($this->rules, $callback, $value); |
||
| 338 | $value ??= $default; |
||
| 339 | |||
| 340 | if ($value === null || preg_match('//u', $value) !== 1) { |
||
| 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 Tree |
||
| 351 | */ |
||
| 352 | public function tree(string $parameter = 'tree'): Tree |
||
| 353 | { |
||
| 354 | $value = $this->parameters[$parameter] ?? null; |
||
| 355 | |||
| 356 | if ($value instanceof Tree) { |
||
| 357 | return $value; |
||
| 358 | } |
||
| 359 | |||
| 360 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $parameter |
||
| 365 | * |
||
| 366 | * @return Tree|null |
||
| 367 | */ |
||
| 368 | public function treeOptional(string $parameter = 'tree'): ?Tree |
||
| 369 | { |
||
| 370 | $value = $this->parameters[$parameter] ?? null; |
||
| 371 | |||
| 372 | if ($value === null || $value instanceof Tree) { |
||
| 373 | return $value; |
||
| 374 | } |
||
| 375 | |||
| 376 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $parameter |
||
| 381 | * |
||
| 382 | * @return UserInterface |
||
| 383 | */ |
||
| 384 | public function user(string $parameter = 'user'): UserInterface |
||
| 393 | } |
||
| 394 | } |
||
| 395 |