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 |
||
19 | class Projector implements EventListenerInterface |
||
20 | { |
||
21 | use DelegateEventHandlingToSpecificMethodTrait; |
||
22 | |||
23 | /** |
||
24 | * @var RepositoryInterface |
||
25 | */ |
||
26 | private $repository; |
||
27 | |||
28 | /** |
||
29 | * @var CreatedByToUserIdResolverInterface |
||
30 | */ |
||
31 | private $userIdResolver; |
||
32 | |||
33 | public function __construct( |
||
40 | |||
41 | public function applyOrganizerProjectedToJSONLD( |
||
50 | |||
51 | public function applyOrganizerImportedFromUDB2( |
||
67 | |||
68 | public function applyOrganizerCreated( |
||
78 | |||
79 | public function applyOrganizerCreatedWithUniqueWebsite( |
||
89 | |||
90 | public function applyOrganizerDeleted( |
||
95 | |||
96 | /** |
||
97 | * @param DomainMessage $domainMessage |
||
98 | * @return DateTime |
||
99 | */ |
||
100 | private function getRecordedDate(DomainMessage $domainMessage) |
||
104 | |||
105 | /** |
||
106 | * @param DomainMessage $domainMessage |
||
107 | * @return string |
||
108 | */ |
||
109 | private function getUserId(DomainMessage $domainMessage) |
||
114 | |||
115 | /** |
||
116 | * @param \CultureFeed_Cdb_Item_Base $udb2Item |
||
117 | * |
||
118 | * @return null|string |
||
119 | */ |
||
120 | View Code Duplication | private function resolveUserId(\CultureFeed_Cdb_Item_Base $udb2Item) |
|
131 | |||
132 | /** |
||
133 | * @param string $dateString |
||
134 | * A UDB2 formatted date string |
||
135 | * |
||
136 | * @return DateTime |
||
137 | */ |
||
138 | protected function dateTimeFromUDB2DateString(string $dateString) |
||
146 | } |
||
147 |
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.