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 | protected function applyEventImportedFromUDB2( |
||
163 | |||
164 | /** |
||
165 | * @param string $itemId |
||
166 | * @param EntityType $itemType |
||
167 | * @param string $userId |
||
168 | * @param CultureFeed_Cdb_Item_Event $udb2Event |
||
169 | */ |
||
170 | protected function updateIndexWithUDB2Event( |
||
223 | |||
224 | /** |
||
225 | * |
||
226 | * @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
227 | */ |
||
228 | protected function applyPlaceImportedFromUDB2(PlaceImportedFromUDB2 $placeImportedFromUDB2) |
||
285 | |||
286 | /** |
||
287 | * Listener for event created commands. |
||
288 | * @param EventCreated $eventCreated |
||
289 | * @param DomainMessage $domainMessage |
||
290 | */ |
||
291 | View Code Duplication | protected function applyEventCreated(EventCreated $eventCreated, DomainMessage $domainMessage) |
|
313 | |||
314 | /** |
||
315 | * Listener for place created commands. |
||
316 | * @param PlaceCreated $placeCreated |
||
317 | * @param DomainMessage $domainMessage |
||
318 | */ |
||
319 | View Code Duplication | protected function applyPlaceCreated(PlaceCreated $placeCreated, DomainMessage $domainMessage) |
|
340 | |||
341 | /** |
||
342 | * @param $dateString |
||
343 | * A UDB2 formatted date string |
||
344 | * |
||
345 | * @return DateTimeInterface |
||
346 | */ |
||
347 | protected function dateTimeFromUDB2DateString($dateString) |
||
355 | |||
356 | /** |
||
357 | * Update the index |
||
358 | * @param $id |
||
359 | * @param EntityType $type |
||
360 | * @param $userId |
||
361 | * @param $name |
||
362 | * @param $postalCode |
||
363 | * @param Domain $owningDomain |
||
364 | * @param DateTimeInterface $creationDate |
||
365 | */ |
||
366 | protected function updateIndex( |
||
385 | |||
386 | /** |
||
387 | * Remove the index for events |
||
388 | * @param EventDeleted $eventDeleted |
||
389 | * @param DomainMessage $domainMessage |
||
390 | */ |
||
391 | public function applyEventDeleted(EventDeleted $eventDeleted, DomainMessage $domainMessage) |
||
395 | |||
396 | /** |
||
397 | * Remove the index for places |
||
398 | * @param PlaceDeleted $placeDeleted |
||
399 | * @param DomainMessage $domainMessage |
||
400 | */ |
||
401 | public function applyPlaceDeleted(PlaceDeleted $placeDeleted, DomainMessage $domainMessage) |
||
405 | } |
||
406 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.