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:
1 | <?php |
||
24 | class DefaultCommandTester implements CommandTester |
||
25 | { |
||
26 | /** |
||
27 | * @var CommandSubscriber |
||
28 | */ |
||
29 | private $commandSubscriber; |
||
30 | /** |
||
31 | * @var CommandApplier |
||
32 | */ |
||
33 | private $commandApplier; |
||
34 | /** |
||
35 | * @var AggregateRepository |
||
36 | */ |
||
37 | private $aggregateRepository; |
||
38 | /** |
||
39 | * @var EventsApplierOnAggregate |
||
40 | */ |
||
41 | private $eventsApplierOnAggregate; |
||
42 | /** |
||
43 | * @var EventMetadataFactory |
||
44 | */ |
||
45 | private $eventMetadataFactory; |
||
46 | /** |
||
47 | * @var CommandMetadataFactory |
||
48 | */ |
||
49 | private $commandMetadataFactory; |
||
50 | |||
51 | 2 | View Code Duplication | public function __construct( |
67 | |||
68 | 2 | public function canExecuteCommand(Command $command): bool |
|
78 | |||
79 | 2 | View Code Duplication | private function loadCommandHandlerAndAggregate(CommandWithMetadata $command): CommandHandlerAndAggregate |
86 | |||
87 | 1 | private function decorateEventWithMetaData($event, MetaData $metaData): EventWithMetaData |
|
91 | |||
92 | /** |
||
93 | * @param CommandWithMetadata $command |
||
94 | * @param CommandHandlerAndAggregate $handlerAndAggregate |
||
95 | * @return void |
||
96 | */ |
||
97 | 2 | View Code Duplication | private function applyCommand(CommandWithMetadata $command, CommandHandlerAndAggregate $handlerAndAggregate) |
115 | |||
116 | 1 | private function isScheduledEvent($event): bool |
|
120 | |||
121 | 1 | private function isScheduledCommand($message): bool |
|
125 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.