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 declare(strict_types=1); |
||
| 41 | final class BlockInterpreter |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @param mixed $input |
||
| 45 | * @param array $serializedBlocks |
||
| 46 | * @param ContextStorageInterface $context |
||
| 47 | * @param CaptureAggregatorInterface $captures |
||
| 48 | * @param ErrorAggregatorInterface $errors |
||
| 49 | * |
||
| 50 | * @return bool |
||
| 51 | * |
||
| 52 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 53 | */ |
||
| 54 | 9 | public static function execute( |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param iterable|int[] $indexes |
||
| 73 | * @param array $blocks |
||
| 74 | * @param ContextStorageInterface $context |
||
| 75 | * @param ErrorAggregatorInterface $errors |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | * |
||
| 79 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 80 | */ |
||
| 81 | 21 | View Code Duplication | public static function executeStarts( |
| 101 | |||
| 102 | /** |
||
| 103 | * @param iterable|int[] $indexes |
||
| 104 | * @param array $blocks |
||
| 105 | * @param ContextStorageInterface $context |
||
| 106 | * @param ErrorAggregatorInterface $errors |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | * |
||
| 110 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 111 | */ |
||
| 112 | 21 | View Code Duplication | public static function executeEnds( |
| 132 | |||
| 133 | /** |
||
| 134 | * @param mixed $input |
||
| 135 | * @param int $blockIndex |
||
| 136 | * @param array $blocks |
||
| 137 | * @param ContextStorageInterface $context |
||
| 138 | * @param CaptureAggregatorInterface $captures |
||
| 139 | * @param ErrorAggregatorInterface $errors |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | * |
||
| 143 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 144 | */ |
||
| 145 | 21 | public static function executeBlock( |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param mixed $input |
||
| 172 | * @param int $blockIndex |
||
| 173 | * @param array $blocks |
||
| 174 | * @param ContextStorageInterface $context |
||
| 175 | * @param CaptureAggregatorInterface $captures |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | * |
||
| 179 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 180 | */ |
||
| 181 | 21 | private static function executeBlockImpl( |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param mixed $input |
||
| 214 | * @param int $blockIndex |
||
| 215 | * @param array $blocks |
||
| 216 | * @param ContextStorageInterface $context |
||
| 217 | * @param CaptureAggregatorInterface $captures |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | * |
||
| 221 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 222 | */ |
||
| 223 | 21 | private static function executeProcedureBlock( |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param mixed $input |
||
| 244 | * @param int $blockIndex |
||
| 245 | * @param array $blocks |
||
| 246 | * @param ContextStorageInterface $context |
||
| 247 | * @param CaptureAggregatorInterface $captures |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | * |
||
| 251 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 252 | */ |
||
| 253 | 7 | private static function executeIfBlock( |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param mixed $input |
||
| 280 | * @param int $blockIndex |
||
| 281 | * @param array $blocks |
||
| 282 | * @param ContextStorageInterface $context |
||
| 283 | * @param CaptureAggregatorInterface $captures |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | * |
||
| 287 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 288 | */ |
||
| 289 | 11 | View Code Duplication | private static function executeAndBlock( |
| 311 | |||
| 312 | /** |
||
| 313 | * @param mixed $input |
||
| 314 | * @param int $blockIndex |
||
| 315 | * @param array $blocks |
||
| 316 | * @param ContextStorageInterface $context |
||
| 317 | * @param CaptureAggregatorInterface $captures |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | * |
||
| 321 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 322 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 323 | */ |
||
| 324 | 4 | View Code Duplication | private static function executeOrBlock( |
| 347 | |||
| 348 | /** |
||
| 349 | * @param array $serializedBlocks |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | * |
||
| 353 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 354 | */ |
||
| 355 | 9 | private static function getBlocks(array $serializedBlocks): array |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param array $serializedBlocks |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | * |
||
| 368 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 369 | */ |
||
| 370 | 9 | View Code Duplication | private static function getBlocksWithStart(array $serializedBlocks): array |
| 382 | |||
| 383 | /** |
||
| 384 | * @param array $serializedBlocks |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | * |
||
| 388 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 389 | */ |
||
| 390 | 9 | View Code Duplication | private static function getBlocksWithEnd(array $serializedBlocks): array |
| 402 | |||
| 403 | /** |
||
| 404 | * @param array $block |
||
| 405 | * |
||
| 406 | * @return int |
||
| 407 | * |
||
| 408 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 409 | */ |
||
| 410 | 21 | private static function getBlockType(array $block): int |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param array $result |
||
| 421 | * @param array $block |
||
| 422 | * @param CaptureAggregatorInterface $captures |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | * |
||
| 426 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 427 | */ |
||
| 428 | 21 | private static function captureSuccessfulBlockResultIfEnabled( |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @param array $procedureBlock |
||
| 445 | * @param ContextInterface $context |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | * |
||
| 449 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 450 | */ |
||
| 451 | 5 | View Code Duplication | private static function executeProcedureStart(array $procedureBlock, ContextInterface $context): array |
| 461 | |||
| 462 | /** |
||
| 463 | * @param array $procedureBlock |
||
| 464 | * @param ContextInterface $context |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | * |
||
| 468 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 469 | */ |
||
| 470 | 6 | View Code Duplication | private static function executeProcedureEnd(array $procedureBlock, ContextInterface $context): iterable |
| 480 | |||
| 481 | /** |
||
| 482 | * @param iterable $errorsInfo |
||
| 483 | * @param ContextStorageInterface $context |
||
| 484 | * @param ErrorAggregatorInterface $errors |
||
| 485 | * |
||
| 486 | * @return void |
||
| 487 | */ |
||
| 488 | 17 | private static function addBlockErrors( |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param iterable $blocks |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | * |
||
| 511 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 512 | */ |
||
| 513 | 9 | private static function debugCheckLooksLikeBlocksArray(iterable $blocks): bool |
|
| 526 | |||
| 527 | /** |
||
| 528 | * @param iterable|int[] $blockIndexes |
||
| 529 | * @param array $blockList |
||
| 530 | * @param int $blockType |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | * |
||
| 534 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 535 | */ |
||
| 536 | 9 | private static function debugCheckBlocksExist(iterable $blockIndexes, array $blockList, int $blockType): bool |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @param array $block |
||
| 551 | * |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | 21 | private static function debugHasKnownBlockType(array $block): bool |
|
| 570 | } |
||
| 571 |
Late static binding only has effect in subclasses. A
finalclass 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.