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 |
||
23 | class Projector implements EventListenerInterface |
||
24 | { |
||
25 | use DelegateEventHandlingToSpecificMethodTraitWithDomainMessageAdapter; |
||
26 | |||
27 | /** |
||
28 | * @var RepositoryInterface |
||
29 | */ |
||
30 | protected $repository; |
||
31 | |||
32 | /** |
||
33 | * @var Domain |
||
34 | */ |
||
35 | protected $localDomain; |
||
36 | |||
37 | /** |
||
38 | * @var IriOfferIdentifierFactoryInterface |
||
39 | */ |
||
40 | protected $identifierFactory; |
||
41 | |||
42 | /** |
||
43 | * @param RepositoryInterface $repository |
||
44 | * @param Domain $localDomain |
||
45 | * @param IriOfferIdentifierFactoryInterface $identifierFactory |
||
46 | */ |
||
47 | public function __construct( |
||
56 | |||
57 | public function applyEventProjectedToJSONLD( |
||
66 | |||
67 | public function applyPlaceProjectedToJSONLD( |
||
76 | |||
77 | /** |
||
78 | * @param AbstractEventWithIri $eventWithIri |
||
79 | * @param DomainMessage $domainMessage |
||
80 | */ |
||
81 | protected function setItemUpdateDate( |
||
94 | |||
95 | /** |
||
96 | * Listener for event created commands. |
||
97 | * @param EventCreated $eventCreated |
||
98 | * @param DomainMessage $domainMessage |
||
99 | */ |
||
100 | View Code Duplication | protected function applyEventCreated( |
|
117 | |||
118 | /** |
||
119 | * @param EventCopied $eventCopied |
||
120 | * @param DomainMessage $domainMessage |
||
121 | */ |
||
122 | public function applyEventCopied( |
||
134 | |||
135 | /** |
||
136 | * Listener for place created commands. |
||
137 | * @param PlaceCreated $placeCreated |
||
138 | * @param DomainMessage $domainMessage |
||
139 | */ |
||
140 | View Code Duplication | protected function applyPlaceCreated( |
|
157 | |||
158 | private function addNewItemToIndex( |
||
177 | |||
178 | /** |
||
179 | * Remove the index for events |
||
180 | * @param EventDeleted $eventDeleted |
||
181 | */ |
||
182 | public function applyEventDeleted( |
||
190 | |||
191 | /** |
||
192 | * Remove the index for places |
||
193 | * @param PlaceDeleted $placeDeleted |
||
194 | */ |
||
195 | public function applyPlaceDeleted( |
||
203 | } |
||
204 |
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.