Complex classes like Extra 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Extra, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Extra extends Assert |
||
|
|||
10 | { |
||
11 | /** |
||
12 | * @psalm-assert null|string $value |
||
13 | * @param mixed $value |
||
14 | * @param string $message |
||
15 | */ |
||
16 | public static function nullOrString($value, $message = '') |
||
20 | |||
21 | /** |
||
22 | * @psalm-assert array<array-key,string> $value |
||
23 | * @param mixed $value |
||
24 | * @param string $message |
||
25 | */ |
||
26 | public static function allString($value, $message = '') |
||
30 | |||
31 | /** |
||
32 | * @psalm-assert null|string $value |
||
33 | * @param mixed $value |
||
34 | * @param string $message |
||
35 | */ |
||
36 | public static function nullOrStringNotEmpty($value, $message = '') |
||
40 | |||
41 | /** |
||
42 | * @psalm-assert array<array-key,string> $value |
||
43 | * @param mixed $value |
||
44 | * @param string $message |
||
45 | */ |
||
46 | public static function allStringNotEmpty($value, $message = '') |
||
50 | |||
51 | /** |
||
52 | * @psalm-assert null|int $value |
||
53 | * @param mixed $value |
||
54 | * @param string $message |
||
55 | */ |
||
56 | public static function nullOrInteger($value, $message = '') |
||
60 | |||
61 | /** |
||
62 | * @psalm-assert array<array-key,int> $value |
||
63 | * @param mixed $value |
||
64 | * @param string $message |
||
65 | */ |
||
66 | public static function allInteger($value, $message = '') |
||
70 | |||
71 | /** |
||
72 | * @psalm-assert null|numeric $value |
||
73 | * @param mixed $value |
||
74 | * @param string $message |
||
75 | */ |
||
76 | public static function nullOrIntegerish($value, $message = '') |
||
80 | |||
81 | /** |
||
82 | * @psalm-assert array<array-key,numeric> $value |
||
83 | * @param mixed $value |
||
84 | * @param string $message |
||
85 | */ |
||
86 | public static function allIntegerish($value, $message = '') |
||
90 | |||
91 | /** |
||
92 | * @psalm-assert null|float $value |
||
93 | * @param mixed $value |
||
94 | * @param string $message |
||
95 | */ |
||
96 | public static function nullOrFloat($value, $message = '') |
||
100 | |||
101 | /** |
||
102 | * @psalm-assert array<array-key,float> $value |
||
103 | * @param mixed $value |
||
104 | * @param string $message |
||
105 | */ |
||
106 | public static function allFloat($value, $message = '') |
||
110 | |||
111 | /** |
||
112 | * @psalm-assert null|numeric $value |
||
113 | * @param mixed $value |
||
114 | * @param string $message |
||
115 | */ |
||
116 | public static function nullOrNumeric($value, $message = '') |
||
120 | |||
121 | /** |
||
122 | * @psalm-assert array<array-key,numeric> $value |
||
123 | * @param mixed $value |
||
124 | * @param string $message |
||
125 | */ |
||
126 | public static function allNumeric($value, $message = '') |
||
130 | |||
131 | /** |
||
132 | * @psalm-assert null|int $value |
||
133 | * @param mixed $value |
||
134 | * @param string $message |
||
135 | */ |
||
136 | public static function nullOrNatural($value, $message = '') |
||
140 | |||
141 | /** |
||
142 | * @psalm-assert array<array-key,int> $value |
||
143 | * @param mixed $value |
||
144 | * @param string $message |
||
145 | */ |
||
146 | public static function allNatural($value, $message = '') |
||
150 | |||
151 | /** |
||
152 | * @psalm-assert null|bool $value |
||
153 | * @param mixed $value |
||
154 | * @param string $message |
||
155 | */ |
||
156 | public static function nullOrBoolean($value, $message = '') |
||
160 | |||
161 | /** |
||
162 | * @psalm-assert array<array-key,bool> $value |
||
163 | * @param mixed $value |
||
164 | * @param string $message |
||
165 | */ |
||
166 | public static function allBoolean($value, $message = '') |
||
170 | |||
171 | /** |
||
172 | * @psalm-assert null|scalar $value |
||
173 | * @param mixed $value |
||
174 | * @param string $message |
||
175 | */ |
||
176 | public static function nullOrScalar($value, $message = '') |
||
180 | |||
181 | /** |
||
182 | * @psalm-assert array<array-key,scalar> $value |
||
183 | * @param mixed $value |
||
184 | * @param string $message |
||
185 | */ |
||
186 | public static function allScalar($value, $message = '') |
||
190 | |||
191 | /** |
||
192 | * @psalm-assert null|object $value |
||
193 | * @param mixed $value |
||
194 | * @param string $message |
||
195 | */ |
||
196 | public static function nullOrObject($value, $message = '') |
||
200 | |||
201 | /** |
||
202 | * @psalm-assert array<array-key,object> $value |
||
203 | * @param mixed $value |
||
204 | * @param string $message |
||
205 | */ |
||
206 | public static function allObject($value, $message = '') |
||
210 | |||
211 | /** |
||
212 | * @psalm-assert null|resource $value |
||
213 | * @param mixed $value |
||
214 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
215 | * @param string $message |
||
216 | */ |
||
217 | public static function nullOrResource($value, $type = null, $message = '') |
||
221 | |||
222 | /** |
||
223 | * @psalm-assert array<array-key,resource> $value |
||
224 | * @param mixed $value |
||
225 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
226 | * @param string $message |
||
227 | */ |
||
228 | public static function allResource($value, $type = null, $message = '') |
||
232 | |||
233 | /** |
||
234 | * @psalm-assert null|callable $value |
||
235 | * @param mixed $value |
||
236 | * @param string $message |
||
237 | */ |
||
238 | public static function nullOrIsCallable($value, $message = '') |
||
242 | |||
243 | /** |
||
244 | * @psalm-assert array<array-key,callable> $value |
||
245 | * @param mixed $value |
||
246 | * @param string $message |
||
247 | */ |
||
248 | public static function allIsCallable($value, $message = '') |
||
252 | |||
253 | /** |
||
254 | * @psalm-assert null|array $value |
||
255 | * @param mixed $value |
||
256 | * @param string $message |
||
257 | */ |
||
258 | public static function nullOrIsArray($value, $message = '') |
||
262 | |||
263 | /** |
||
264 | * @psalm-assert array<array-key,array> $value |
||
265 | * @param mixed $value |
||
266 | * @param string $message |
||
267 | */ |
||
268 | public static function allIsArray($value, $message = '') |
||
272 | |||
273 | /** |
||
274 | * @psalm-assert null|iterable $value |
||
275 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
276 | * @param mixed $value |
||
277 | * @param string $message |
||
278 | */ |
||
279 | public static function nullOrIsTraversable($value, $message = '') |
||
283 | |||
284 | /** |
||
285 | * @psalm-assert array<array-key,iterable> $value |
||
286 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
287 | * @param mixed $value |
||
288 | * @param string $message |
||
289 | */ |
||
290 | public static function allIsTraversable($value, $message = '') |
||
294 | |||
295 | /** |
||
296 | * @psalm-assert null|countable $value |
||
297 | * @param mixed $value |
||
298 | * @param string $message |
||
299 | */ |
||
300 | public static function nullOrIsCountable($value, $message = '') |
||
304 | |||
305 | /** |
||
306 | * @psalm-assert array<array-key,countable> $value |
||
307 | * @param mixed $value |
||
308 | * @param string $message |
||
309 | */ |
||
310 | public static function allIsCountable($value, $message = '') |
||
314 | |||
315 | /** |
||
316 | * @psalm-assert null|iterable $value |
||
317 | * @param mixed $value |
||
318 | * @param string $message |
||
319 | */ |
||
320 | public static function nullOrIsIterable($value, $message = '') |
||
324 | |||
325 | /** |
||
326 | * @psalm-assert array<array-key,iterable> $value |
||
327 | * @param mixed $value |
||
328 | * @param string $message |
||
329 | */ |
||
330 | public static function allIsIterable($value, $message = '') |
||
334 | |||
335 | /** |
||
336 | * @psalm-template ExpectedType of object |
||
337 | * @psalm-param class-string<ExpectedType> $class |
||
338 | * @psalm-assert null|ExpectedType $value |
||
339 | * @param mixed $value |
||
340 | * @param string|object $class |
||
341 | * @param string $message |
||
342 | */ |
||
343 | public static function nullOrIsInstanceOf($value, $class, $message = '') |
||
347 | |||
348 | /** |
||
349 | * @psalm-template ExpectedType of object |
||
350 | * @psalm-param class-string<ExpectedType> $class |
||
351 | * @psalm-assert array<array-key,ExpectedType> $value |
||
352 | * @param mixed $value |
||
353 | * @param string|object $class |
||
354 | * @param string $message |
||
355 | */ |
||
356 | public static function allIsInstanceOf($value, $class, $message = '') |
||
360 | |||
361 | /** |
||
362 | * @psalm-assert null|empty $value |
||
363 | * @param mixed $value |
||
364 | * @param string $message |
||
365 | */ |
||
366 | public static function nullOrIsEmpty($value, $message = '') |
||
370 | |||
371 | /** |
||
372 | * @psalm-assert array<array-key,empty> $value |
||
373 | * @param mixed $value |
||
374 | * @param string $message |
||
375 | */ |
||
376 | public static function allIsEmpty($value, $message = '') |
||
380 | |||
381 | /** |
||
382 | * @psalm-assert array<array-key,null> $value |
||
383 | * @param mixed $value |
||
384 | * @param string $message |
||
385 | */ |
||
386 | public static function allNull($value, $message = '') |
||
390 | |||
391 | /** |
||
392 | * @psalm-assert null|true $value |
||
393 | * @param mixed $value |
||
394 | * @param string $message |
||
395 | */ |
||
396 | public static function nullOrTrue($value, $message = '') |
||
400 | |||
401 | /** |
||
402 | * @psalm-assert array<array-key,true> $value |
||
403 | * @param mixed $value |
||
404 | * @param string $message |
||
405 | */ |
||
406 | public static function allTrue($value, $message = '') |
||
410 | |||
411 | /** |
||
412 | * @psalm-assert null|false $value |
||
413 | * @param mixed $value |
||
414 | * @param string $message |
||
415 | */ |
||
416 | public static function nullOrFalse($value, $message = '') |
||
420 | |||
421 | /** |
||
422 | * @psalm-assert array<array-key,false> $value |
||
423 | * @param mixed $value |
||
424 | * @param string $message |
||
425 | */ |
||
426 | public static function allFalse($value, $message = '') |
||
430 | |||
431 | /** |
||
432 | * @psalm-template ExpectedType |
||
433 | * @psalm-param array<ExpectedType> $values |
||
434 | * @psalm-assert null|ExpectedType $value |
||
435 | * @param mixed $value |
||
436 | * @param array $values |
||
437 | * @param string $message |
||
438 | */ |
||
439 | public static function nullOrOneOf($value, $values, $message = '') |
||
443 | |||
444 | /** |
||
445 | * @psalm-template ExpectedType |
||
446 | * @psalm-param array<ExpectedType> $values |
||
447 | * @psalm-assert array<array-key,ExpectedType> $value |
||
448 | * @param mixed $value |
||
449 | * @param array $values |
||
450 | * @param string $message |
||
451 | */ |
||
452 | public static function allOneOf($value, $values, $message = '') |
||
456 | |||
457 | /** |
||
458 | * @psalm-assert null|class-string $value |
||
459 | * @param mixed $value |
||
460 | * @param string $message |
||
461 | */ |
||
462 | public static function nullOrClassExists($value, $message = '') |
||
466 | |||
467 | /** |
||
468 | * @psalm-assert array<array-key,class-string> $value |
||
469 | * @param mixed $value |
||
470 | * @param string $message |
||
471 | */ |
||
472 | public static function allClassExists($value, $message = '') |
||
476 | |||
477 | /** |
||
478 | * @psalm-assert null|class-string $value |
||
479 | * @param mixed $value |
||
480 | * @param string $message |
||
481 | */ |
||
482 | public static function nullOrInterfaceExists($value, $message = '') |
||
486 | |||
487 | /** |
||
488 | * @psalm-assert array<array-key,class-string> $value |
||
489 | * @param mixed $value |
||
490 | * @param string $message |
||
491 | */ |
||
492 | public static function allInterfaceExists($value, $message = '') |
||
496 | } |
||
497 |