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 |
||
10 | final class SubEvents |
||
11 | { |
||
12 | /** |
||
13 | * @var SubEvent[] |
||
14 | */ |
||
15 | private $subEvents; |
||
16 | |||
17 | private function __construct(SubEvent ...$subEvents) |
||
21 | |||
22 | public static function createEmpty(): SubEvents |
||
26 | |||
27 | public static function createFromCalendar(Calendar $calendar): SubEvents |
||
41 | |||
42 | public function getSubEvents(): array |
||
46 | |||
47 | public function hasSubEvent(SubEvent $subEventToSearch): bool |
||
57 | |||
58 | public function hasSubEventWithTimestamp(Timestamp $timestampToSearch): bool |
||
68 | |||
69 | View Code Duplication | public function addSubEvent(SubEvent $subEvent): SubEvents |
|
79 | |||
80 | View Code Duplication | public function updateSubEvent(SubEvent $subEvent): SubEvents |
|
91 | |||
92 | private function removeSubEventWithTimestamp(Timestamp $timestamp): SubEvents |
||
105 | } |
||
106 |
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.