Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BlockInterpreter 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 BlockInterpreter, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Limoncello\Validation\Execution; |
||
31 | final class BlockInterpreter |
||
32 | { |
||
33 | /** |
||
34 | * @param mixed $input |
||
35 | * @param array $serializedBlocks |
||
36 | * @param ContextStorageInterface $context |
||
37 | * @param CaptureAggregatorInterface $captures |
||
38 | * @param ErrorAggregatorInterface $errors |
||
39 | * |
||
40 | * @return bool |
||
41 | * |
||
42 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
43 | */ |
||
44 | 9 | public static function execute( |
|
60 | |||
61 | /** |
||
62 | * @param int[] $indexes |
||
63 | * @param array $blocks |
||
64 | * @param ContextStorageInterface $context |
||
65 | * @param ErrorAggregatorInterface $errors |
||
66 | * |
||
67 | * @return bool |
||
68 | * |
||
69 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
70 | */ |
||
71 | 18 | View Code Duplication | public static function executeStarts( |
91 | |||
92 | /** |
||
93 | * @param int[] $indexes |
||
94 | * @param array $blocks |
||
95 | * @param ContextStorageInterface $context |
||
96 | * @param ErrorAggregatorInterface $errors |
||
97 | * |
||
98 | * @return bool |
||
99 | * |
||
100 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
101 | */ |
||
102 | 18 | View Code Duplication | public static function executeEnds( |
122 | |||
123 | /** |
||
124 | * @param mixed $input |
||
125 | * @param int $blockIndex |
||
126 | * @param array $blocks |
||
127 | * @param ContextStorageInterface $context |
||
128 | * @param CaptureAggregatorInterface $captures |
||
129 | * @param ErrorAggregatorInterface $errors |
||
130 | * |
||
131 | * @return bool |
||
132 | * |
||
133 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
134 | */ |
||
135 | 18 | public static function executeBlock( |
|
159 | |||
160 | /** |
||
161 | * @param mixed $input |
||
162 | * @param int $blockIndex |
||
163 | * @param array $blocks |
||
164 | * @param ContextStorageInterface $context |
||
165 | * @param CaptureAggregatorInterface $captures |
||
166 | * |
||
167 | * @return array |
||
168 | * |
||
169 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
170 | */ |
||
171 | 18 | private static function executeBlockImpl( |
|
201 | |||
202 | /** |
||
203 | * @param mixed $input |
||
204 | * @param int $blockIndex |
||
205 | * @param array $blocks |
||
206 | * @param ContextStorageInterface $context |
||
207 | * @param CaptureAggregatorInterface $captures |
||
208 | * |
||
209 | * @return array |
||
210 | * |
||
211 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
212 | */ |
||
213 | 18 | private static function executeProcedureBlock( |
|
231 | |||
232 | /** |
||
233 | * @param mixed $input |
||
234 | * @param int $blockIndex |
||
235 | * @param array $blocks |
||
236 | * @param ContextStorageInterface $context |
||
237 | * @param CaptureAggregatorInterface $captures |
||
238 | * |
||
239 | * @return array |
||
240 | * |
||
241 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
242 | */ |
||
243 | 6 | private static function executeIfBlock( |
|
267 | |||
268 | /** |
||
269 | * @param mixed $input |
||
270 | * @param int $blockIndex |
||
271 | * @param array $blocks |
||
272 | * @param ContextStorageInterface $context |
||
273 | * @param CaptureAggregatorInterface $captures |
||
274 | * |
||
275 | * @return array |
||
276 | * |
||
277 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
278 | */ |
||
279 | 10 | View Code Duplication | private static function executeAndBlock( |
301 | |||
302 | /** |
||
303 | * @param mixed $input |
||
304 | * @param int $blockIndex |
||
305 | * @param array $blocks |
||
306 | * @param ContextStorageInterface $context |
||
307 | * @param CaptureAggregatorInterface $captures |
||
308 | * |
||
309 | * @return array |
||
310 | * |
||
311 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
312 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
313 | */ |
||
314 | 4 | View Code Duplication | private static function executeOrBlock( |
337 | |||
338 | /** |
||
339 | * @param array $serializedBlocks |
||
340 | * |
||
341 | * @return array |
||
342 | * |
||
343 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
344 | */ |
||
345 | 9 | private static function getBlocks(array $serializedBlocks): array |
|
352 | |||
353 | /** |
||
354 | * @param array $serializedBlocks |
||
355 | * |
||
356 | * @return array |
||
357 | * |
||
358 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
359 | */ |
||
360 | 9 | View Code Duplication | private static function getBlocksWithStart(array $serializedBlocks): array |
372 | |||
373 | /** |
||
374 | * @param array $serializedBlocks |
||
375 | * |
||
376 | * @return array |
||
377 | * |
||
378 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
379 | */ |
||
380 | 9 | View Code Duplication | private static function getBlocksWithEnd(array $serializedBlocks): array |
392 | |||
393 | /** |
||
394 | * @param array $block |
||
395 | * |
||
396 | * @return int |
||
397 | * |
||
398 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
399 | */ |
||
400 | 18 | private static function getBlockType(array $block): int |
|
408 | |||
409 | /** |
||
410 | * @param array $result |
||
411 | * @param array $block |
||
412 | * @param CaptureAggregatorInterface $captures |
||
413 | * |
||
414 | * @return void |
||
415 | * |
||
416 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
417 | */ |
||
418 | 18 | private static function captureSuccessfulBlockResultIfEnabled( |
|
432 | |||
433 | /** |
||
434 | * @param array $procedureBlock |
||
435 | * @param ContextInterface $context |
||
436 | * |
||
437 | * @return array |
||
438 | * |
||
439 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
440 | */ |
||
441 | 5 | View Code Duplication | private static function executeProcedureStart(array $procedureBlock, ContextInterface $context): array |
451 | |||
452 | /** |
||
453 | * @param array $procedureBlock |
||
454 | * @param ContextInterface $context |
||
455 | * |
||
456 | * @return array |
||
457 | * |
||
458 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
459 | */ |
||
460 | 6 | View Code Duplication | private static function executeProcedureEnd(array $procedureBlock, ContextInterface $context): array |
470 | |||
471 | /** |
||
472 | * @param array $errorsInfo |
||
473 | * @param ContextStorageInterface $context |
||
474 | * @param ErrorAggregatorInterface $errors |
||
475 | * |
||
476 | * @return void |
||
477 | */ |
||
478 | 16 | private static function addBlockErrors( |
|
494 | |||
495 | /** |
||
496 | * @param array $blocks |
||
497 | * |
||
498 | * @return bool |
||
499 | * |
||
500 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
501 | */ |
||
502 | 9 | private static function debugCheckLooksLikeBlocksArray(array $blocks): bool |
|
515 | |||
516 | /** |
||
517 | * @param array $blockIndexes |
||
518 | * @param array $blockList |
||
519 | * @param int $blockType |
||
520 | * |
||
521 | * @return bool |
||
522 | * |
||
523 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
524 | */ |
||
525 | 9 | private static function debugCheckBlocksExist(array $blockIndexes, array $blockList, int $blockType): bool |
|
537 | |||
538 | /** |
||
539 | * @param array $block |
||
540 | * |
||
541 | * @return bool |
||
542 | */ |
||
543 | 18 | private static function debugHasKnownBlockType(array $block): bool |
|
559 | } |
||
560 |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.