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 |
||
| 50 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * @var InstitutionConfigurationId |
||
| 54 | */ |
||
| 55 | private $institutionConfigurationId; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Institution |
||
| 59 | */ |
||
| 60 | private $institution; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var RaLocationList |
||
| 64 | */ |
||
| 65 | private $raLocations; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var UseRaLocationsOption |
||
| 69 | */ |
||
| 70 | private $useRaLocationsOption; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ShowRaaContactInformationOption |
||
| 74 | */ |
||
| 75 | private $showRaaContactInformationOption; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var AllowedSecondFactorList |
||
| 79 | */ |
||
| 80 | private $allowedSecondFactorList; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var boolean |
||
| 84 | */ |
||
| 85 | private $isMarkedAsDestroyed; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 89 | * @param Institution $institution |
||
| 90 | * @return InstitutionConfiguration |
||
| 91 | */ |
||
| 92 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 93 | { |
||
| 94 | $institutionConfiguration = new self; |
||
| 95 | $institutionConfiguration->apply( |
||
| 96 | new NewInstitutionConfigurationCreatedEvent( |
||
| 97 | $institutionConfigurationId, |
||
| 98 | $institution, |
||
| 99 | UseRaLocationsOption::getDefault(), |
||
| 100 | ShowRaaContactInformationOption::getDefault() |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | $institutionConfiguration->apply(new AllowedSecondFactorListUpdatedEvent( |
||
| 104 | $institutionConfigurationId, |
||
| 105 | $institution, |
||
| 106 | AllowedSecondFactorList::blank() |
||
| 107 | )); |
||
| 108 | |||
| 109 | return $institutionConfiguration; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return InstitutionConfiguration |
||
| 114 | */ |
||
| 115 | public function rebuild() |
||
| 116 | { |
||
| 117 | // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid |
||
| 118 | if ($this->isMarkedAsDestroyed !== true) { |
||
| 119 | throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->apply( |
||
| 123 | new NewInstitutionConfigurationCreatedEvent( |
||
| 124 | $this->institutionConfigurationId, |
||
| 125 | $this->institution, |
||
| 126 | UseRaLocationsOption::getDefault(), |
||
| 127 | ShowRaaContactInformationOption::getDefault() |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | $this->apply(new AllowedSecondFactorListUpdatedEvent( |
||
| 131 | $this->institutionConfigurationId, |
||
| 132 | $this->institution, |
||
| 133 | AllowedSecondFactorList::blank() |
||
| 134 | )); |
||
| 135 | |||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | final public function __construct() |
||
| 142 | |||
| 143 | public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption) |
||
| 157 | |||
| 158 | public function configureShowRaaContactInformationOption(ShowRaaContactInformationOption $showRaaContactInformationOption) |
||
| 172 | |||
| 173 | public function updateAllowedSecondFactorList(AllowedSecondFactorList $allowedSecondFactorList) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param RaLocationId $raLocationId |
||
| 193 | * @param RaLocationName $raLocationName |
||
| 194 | * @param Location $location |
||
| 195 | * @param ContactInformation $contactInformation |
||
| 196 | */ |
||
| 197 | public function addRaLocation( |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param RaLocationId $raLocationId |
||
| 224 | * @param RaLocationName $raLocationName |
||
| 225 | * @param Location $location |
||
| 226 | * @param ContactInformation $contactInformation |
||
| 227 | */ |
||
| 228 | public function changeRaLocation( |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param RaLocationId $raLocationId |
||
| 268 | */ |
||
| 269 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function destroy() |
||
| 290 | |||
| 291 | public function getAggregateRootId() |
||
| 295 | |||
| 296 | protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
||
| 305 | |||
| 306 | protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event) |
||
| 310 | |||
| 311 | protected function applyShowRaaContactInformationOptionChangedEvent( |
||
| 316 | |||
| 317 | protected function applyAllowedSecondFactorListUpdatedEvent(AllowedSecondFactorListUpdatedEvent $event) |
||
| 321 | |||
| 322 | protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event) |
||
| 333 | |||
| 334 | protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event) |
||
| 339 | |||
| 340 | protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event) |
||
| 345 | |||
| 346 | protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event) |
||
| 351 | |||
| 352 | protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 359 | * @param InstitutionConfigurationRemovedEvent $event |
||
| 360 | */ |
||
| 361 | protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event) |
||
| 371 | } |
||
| 372 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.