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 namespace SmoothPhp\EventSourcing; |
||
11 | abstract class Entity implements \SmoothPhp\Contracts\EventSourcing\Entity |
||
12 | { |
||
13 | /** @var AggregateRootContract */ |
||
14 | private $aggregateRoot; |
||
15 | |||
16 | /** |
||
17 | * @param Event $event |
||
18 | * @throws Exception\AggregateRootAlreadyRegistered |
||
19 | */ |
||
20 | 6 | public function handleRecursively(Event $event) |
|
29 | |||
30 | /** |
||
31 | * @param AggregateRootContract $aggregateRoot |
||
32 | * @throws Exception\AggregateRootAlreadyRegistered |
||
33 | */ |
||
34 | 9 | public function registerAggregateRoot(AggregateRootContract $aggregateRoot) |
|
42 | |||
43 | /** |
||
44 | * @return \SmoothPhp\Contracts\EventSourcing\Entity[] $entity |
||
45 | */ |
||
46 | public function getChildren() |
||
50 | |||
51 | /** |
||
52 | * @param Event $event |
||
53 | * @throws Exception\NoAggregateRootRegistered |
||
54 | */ |
||
55 | 3 | protected function apply(Event $event) |
|
62 | |||
63 | /** |
||
64 | * Handles event if capable. |
||
65 | * |
||
66 | * @param $event |
||
67 | */ |
||
68 | 6 | View Code Duplication | protected function handle(Event $event) |
76 | |||
77 | /** |
||
78 | * @param Event $event |
||
79 | * @return string |
||
80 | */ |
||
81 | 6 | private function getApplyMethod(Event $event) |
|
87 | } |
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.