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 |
||
21 | class EventStream |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $streamName; |
||
27 | |||
28 | /** |
||
29 | * @var IdInterface |
||
30 | */ |
||
31 | protected $aggregateId; |
||
32 | |||
33 | /** |
||
34 | * @var DomainEventInterface[] |
||
35 | */ |
||
36 | protected $events = []; |
||
37 | |||
38 | /** |
||
39 | * EntityDomainEvent constructor. |
||
40 | * |
||
41 | * @param string $streamName |
||
42 | * @param IdInterface $aggregateId |
||
43 | * @param DomainEventInterface[] $events |
||
44 | */ |
||
45 | public function __construct($streamName, IdInterface $aggregateId, array $events) |
||
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | public function streamName() |
||
72 | |||
73 | /** |
||
74 | * @return IdInterface |
||
75 | */ |
||
76 | public function aggregateId() |
||
80 | |||
81 | /** |
||
82 | * @return DomainEventInterface[] |
||
83 | */ |
||
84 | public function events() |
||
88 | } |
||
89 |
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.