Total Complexity | 60 |
Total Lines | 345 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
41 | class Validator |
||
42 | { |
||
43 | /** @var array<int|string|Tree|UserInterface|array<int|string>> */ |
||
44 | private array $parameters; |
||
45 | |||
46 | /** @var array<Closure> */ |
||
47 | private array $rules = []; |
||
48 | |||
49 | /** |
||
50 | * @param array<int|string|Tree|UserInterface|array<int|string>> $parameters |
||
51 | */ |
||
52 | public function __construct(array $parameters) |
||
53 | { |
||
54 | $this->parameters = $parameters; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param ServerRequestInterface $request |
||
59 | * |
||
60 | * @return self |
||
61 | */ |
||
62 | public static function attributes(ServerRequestInterface $request): self |
||
63 | { |
||
64 | return new self($request->getAttributes()); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param ServerRequestInterface $request |
||
69 | * |
||
70 | * @return self |
||
71 | */ |
||
72 | public static function parsedBody(ServerRequestInterface $request): self |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param ServerRequestInterface $request |
||
79 | * |
||
80 | * @return self |
||
81 | */ |
||
82 | public static function queryParams(ServerRequestInterface $request): self |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param ServerRequestInterface $request |
||
89 | * |
||
90 | * @return self |
||
91 | */ |
||
92 | public static function serverParams(ServerRequestInterface $request): self |
||
93 | { |
||
94 | return new self($request->getServerParams()); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param int $minimum |
||
99 | * @param int $maximum |
||
100 | * |
||
101 | * @return self |
||
102 | */ |
||
103 | public function isBetween(int $minimum, int $maximum): self |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param array<string> $values |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function isInArray(array $values): self |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param array<string> $values |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function isInArrayKeys(array $values): self |
||
134 | { |
||
135 | return $this->isInArray(array_keys($values)); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function isNotEmpty(): self |
||
142 | { |
||
143 | $this->rules[] = static fn (?string $value): ?string => $value !== null && $value !== '' ? $value : null; |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string $base_url |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function isLocalUrl(string $base_url): self |
||
154 | { |
||
155 | $this->rules[] = static function (?string $value) use ($base_url): ?string { |
||
156 | if ($value !== null) { |
||
157 | $value_info = parse_url($value); |
||
158 | $base_url_info = parse_url($base_url); |
||
159 | |||
160 | if (!is_array($base_url_info)) { |
||
161 | throw new LogicException(__METHOD__ . ' needs a valid URL'); |
||
162 | } |
||
163 | |||
164 | if (is_array($value_info)) { |
||
165 | $scheme_ok = ($value_info['scheme'] ?? 'http') === ($base_url_info['scheme'] ?? 'http'); |
||
166 | $host_ok = ($value_info['host'] ?? '') === ($base_url_info['host'] ?? ''); |
||
167 | $port_ok = ($value_info['port'] ?? '') === ($base_url_info['port'] ?? ''); |
||
168 | $user_ok = ($value_info['user'] ?? '') === ($base_url_info['user'] ?? ''); |
||
169 | $path_ok = str_starts_with($value_info['path'] ?? '/', $base_url_info['path'] ?? '/'); |
||
170 | |||
171 | if ($scheme_ok && $host_ok && $port_ok && $user_ok && $path_ok) { |
||
172 | return $value; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return null; |
||
178 | }; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function isTag(): self |
||
187 | { |
||
188 | $this->rules[] = static function (?string $value): ?string { |
||
189 | if ($value !== null && preg_match('/^' . Gedcom::REGEX_TAG . '$/', $value) === 1) { |
||
190 | return $value; |
||
191 | } |
||
192 | |||
193 | return null; |
||
194 | }; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function isXref(): self |
||
203 | { |
||
204 | $this->rules[] = static function (?string $value): ?string { |
||
205 | if ($value !== null && preg_match('/^' . Gedcom::REGEX_XREF . '$/', $value) === 1) { |
||
206 | return $value; |
||
207 | } |
||
208 | |||
209 | return null; |
||
210 | }; |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param string $parameter |
||
217 | * @param bool|null $default |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function boolean(string $parameter, bool $default = null): bool |
||
222 | { |
||
223 | $value = $this->parameters[$parameter] ?? null; |
||
224 | |||
225 | if (in_array($value, ['1', true], true)) { |
||
226 | return true; |
||
227 | } |
||
228 | |||
229 | if (in_array($value, ['0', '', false], true)) { |
||
230 | return false; |
||
231 | } |
||
232 | |||
233 | if ($default === null) { |
||
234 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
235 | } |
||
236 | |||
237 | return $default; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $parameter |
||
242 | * |
||
243 | * @return array<string> |
||
244 | */ |
||
245 | public function array(string $parameter): array |
||
246 | { |
||
247 | $value = $this->parameters[$parameter] ?? null; |
||
248 | |||
249 | if (!is_array($value) && $value !== null) { |
||
250 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
251 | } |
||
252 | |||
253 | $callback = static fn (?array $value, Closure $rule): ?array => $rule($value); |
||
254 | |||
255 | $value = array_reduce($this->rules, $callback, $value); |
||
256 | $value ??= []; |
||
257 | |||
258 | $check_utf8 = static function ($v, $k) use ($parameter) { |
||
259 | if (is_string($k) && !preg_match('//u', $k) || is_string($v) && !preg_match('//u', $v)) { |
||
260 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
261 | } |
||
262 | }; |
||
263 | |||
264 | array_walk_recursive($value, $check_utf8); |
||
265 | |||
266 | return $value; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $parameter |
||
271 | * @param int|null $default |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | public function integer(string $parameter, int $default = null): int |
||
276 | { |
||
277 | $value = $this->parameters[$parameter] ?? null; |
||
278 | |||
279 | if (is_string($value) && ctype_digit($value)) { |
||
280 | $value = (int) $value; |
||
281 | } elseif (!is_int($value)) { |
||
282 | $value = null; |
||
283 | } |
||
284 | |||
285 | $callback = static fn (?int $value, Closure $rule): ?int => $rule($value); |
||
286 | |||
287 | $value = array_reduce($this->rules, $callback, $value); |
||
288 | |||
289 | $value ??= $default; |
||
290 | |||
291 | if ($value === null) { |
||
292 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
293 | } |
||
294 | |||
295 | return $value; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param string $parameter |
||
300 | * |
||
301 | * @return Route |
||
302 | */ |
||
303 | public function route(string $parameter = 'route'): Route |
||
304 | { |
||
305 | $value = $this->parameters[$parameter] ?? null; |
||
306 | |||
307 | if ($value instanceof Route) { |
||
308 | return $value; |
||
309 | } |
||
310 | |||
311 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param string $parameter |
||
316 | * @param string|null $default |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function string(string $parameter, string $default = null): string |
||
321 | { |
||
322 | $value = $this->parameters[$parameter] ?? null; |
||
323 | |||
324 | if (!is_string($value)) { |
||
325 | $value = null; |
||
326 | } |
||
327 | |||
328 | $callback = static fn (?string $value, Closure $rule): ?string => $rule($value); |
||
329 | |||
330 | $value = array_reduce($this->rules, $callback, $value); |
||
331 | $value ??= $default; |
||
332 | |||
333 | if ($value === null || preg_match('//u', $value) !== 1) { |
||
334 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
335 | } |
||
336 | |||
337 | return $value; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param string $parameter |
||
342 | * |
||
343 | * @return Tree |
||
344 | */ |
||
345 | public function tree(string $parameter = 'tree'): Tree |
||
346 | { |
||
347 | $value = $this->parameters[$parameter] ?? null; |
||
348 | |||
349 | if ($value instanceof Tree) { |
||
350 | return $value; |
||
351 | } |
||
352 | |||
353 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param string $parameter |
||
358 | * |
||
359 | * @return Tree|null |
||
360 | */ |
||
361 | public function treeOptional(string $parameter = 'tree'): ?Tree |
||
362 | { |
||
363 | $value = $this->parameters[$parameter] ?? null; |
||
364 | |||
365 | if ($value === null || $value instanceof Tree) { |
||
366 | return $value; |
||
367 | } |
||
368 | |||
369 | throw new HttpBadRequestException(I18N::translate('The parameter “%s” is missing.', $parameter)); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @param string $parameter |
||
374 | * |
||
375 | * @return UserInterface |
||
376 | */ |
||
377 | public function user(string $parameter = 'user'): UserInterface |
||
386 | } |
||
387 | } |
||
388 |