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 |
||
| 32 | class Projector implements EventListenerInterface |
||
| 33 | { |
||
| 34 | use DelegateEventHandlingToSpecificMethodTrait { |
||
| 35 | DelegateEventHandlingToSpecificMethodTrait::handle as handleMethodSpecificEvents; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var RepositoryInterface |
||
| 40 | */ |
||
| 41 | protected $repository; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var CreatedByToUserIdResolverInterface |
||
| 45 | */ |
||
| 46 | protected $userIdResolver; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * A list of events that should trigger an index item update. |
||
| 50 | * The key is the namespaced class name. |
||
| 51 | * The value is the method the method to call to get the id of the index item. |
||
| 52 | * |
||
| 53 | * @var string[] |
||
| 54 | */ |
||
| 55 | protected static $indexUpdateEvents = [ |
||
| 56 | EventProjectedToJSONLD::class, |
||
| 57 | PlaceProjectedToJSONLD::class, |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var Domain |
||
| 62 | */ |
||
| 63 | protected $localDomain; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Domain |
||
| 67 | */ |
||
| 68 | protected $UDB2Domain; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var IriOfferIdentifierFactoryInterface |
||
| 72 | */ |
||
| 73 | protected $identifierFactory; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param RepositoryInterface $repository |
||
| 77 | * @param CreatedByToUserIdResolverInterface $createdByToUserIdResolver |
||
| 78 | * @param Domain $localDomain |
||
| 79 | * @param Domain $UDB2Domain |
||
| 80 | * @param IriOfferIdentifierFactoryInterface $identifierFactory |
||
| 81 | */ |
||
| 82 | public function __construct( |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function handle(DomainMessage $domainMessage) |
||
| 104 | |||
| 105 | protected function handleIndexUpdateEvents(DomainMessage $domainMessage) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $itemId |
||
| 123 | * @param DateTimeInterface $dateUpdated |
||
| 124 | */ |
||
| 125 | protected function setItemUpdateDate($itemId, DateTimeInterface $dateUpdated) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param \CultureFeed_Cdb_Item_Base $udb2Item |
||
| 132 | * |
||
| 133 | * @return null|string |
||
| 134 | */ |
||
| 135 | protected function resolveUserId(\CultureFeed_Cdb_Item_Base $udb2Item) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * @param EventImportedFromUDB2 $eventImportedFromUDB2 |
||
| 150 | */ |
||
| 151 | View Code Duplication | protected function applyEventImportedFromUDB2( |
|
| 174 | |||
| 175 | /** |
||
| 176 | * |
||
| 177 | * @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function applyPlaceImportedFromUDB2(PlaceImportedFromUDB2 $placeImportedFromUDB2) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Listener for event created commands. |
||
| 205 | * @param EventCreated $eventCreated |
||
| 206 | * @param DomainMessage $domainMessage |
||
| 207 | */ |
||
| 208 | View Code Duplication | protected function applyEventCreated(EventCreated $eventCreated, DomainMessage $domainMessage) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param EventCopied $eventCopied |
||
| 225 | * @param DomainMessage $domainMessage |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function applyEventCopied(EventCopied $eventCopied, DomainMessage $domainMessage) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Listener for place created commands. |
||
| 244 | * @param PlaceCreated $placeCreated |
||
| 245 | * @param DomainMessage $domainMessage |
||
| 246 | */ |
||
| 247 | View Code Duplication | protected function applyPlaceCreated(PlaceCreated $placeCreated, DomainMessage $domainMessage) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param $dateString |
||
| 264 | * A UDB2 formatted date string |
||
| 265 | * |
||
| 266 | * @return DateTimeInterface |
||
| 267 | */ |
||
| 268 | protected function dateTimeFromUDB2DateString($dateString) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Update the index |
||
| 279 | * @param $id |
||
| 280 | * @param EntityType $type |
||
| 281 | * @param $userId |
||
| 282 | * @param Domain $owningDomain |
||
| 283 | * @param DateTimeInterface $creationDate |
||
| 284 | */ |
||
| 285 | protected function updateIndex( |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Remove the index for events |
||
| 303 | * @param EventDeleted $eventDeleted |
||
| 304 | */ |
||
| 305 | public function applyEventDeleted(EventDeleted $eventDeleted) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Remove the index for places |
||
| 312 | * @param PlaceDeleted $placeDeleted |
||
| 313 | */ |
||
| 314 | public function applyPlaceDeleted(PlaceDeleted $placeDeleted) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param DomainMessage $domainMessage |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | private function getUserId(DomainMessage $domainMessage) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param DomainMessage $domainMessage |
||
| 331 | * @return DateTime |
||
| 332 | */ |
||
| 333 | private function getRecordedDate(DomainMessage $domainMessage) |
||
| 337 | } |
||
| 338 |
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.