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 |
||
| 54 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * @var InstitutionConfigurationId |
||
| 58 | */ |
||
| 59 | private $institutionConfigurationId; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Institution |
||
| 63 | */ |
||
| 64 | private $institution; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var RaLocationList |
||
| 68 | */ |
||
| 69 | private $raLocations; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var UseRaLocationsOption |
||
| 73 | */ |
||
| 74 | private $useRaLocationsOption; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ShowRaaContactInformationOption |
||
| 78 | */ |
||
| 79 | private $showRaaContactInformationOption; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var VerifyEmailOption |
||
| 83 | */ |
||
| 84 | private $verifyEmailOption; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var NumberOfTokensPerIdentityOption |
||
| 88 | */ |
||
| 89 | private $numberOfTokensPerIdentityOption; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var AllowedSecondFactorList |
||
| 93 | */ |
||
| 94 | private $allowedSecondFactorList; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var boolean |
||
| 98 | */ |
||
| 99 | private $isMarkedAsDestroyed; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 103 | * @param Institution $institution |
||
| 104 | * @return InstitutionConfiguration |
||
| 105 | */ |
||
| 106 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 107 | { |
||
| 108 | $institutionConfiguration = new self; |
||
| 109 | $institutionConfiguration->apply( |
||
| 110 | new NewInstitutionConfigurationCreatedEvent( |
||
| 111 | $institutionConfigurationId, |
||
| 112 | $institution, |
||
| 113 | UseRaLocationsOption::getDefault(), |
||
| 114 | ShowRaaContactInformationOption::getDefault(), |
||
| 115 | VerifyEmailOption::getDefault(), |
||
| 116 | NumberOfTokensPerIdentityOption::getDefault() |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | $institutionConfiguration->apply(new AllowedSecondFactorListUpdatedEvent( |
||
| 120 | $institutionConfigurationId, |
||
| 121 | $institution, |
||
| 122 | AllowedSecondFactorList::blank() |
||
| 123 | )); |
||
| 124 | |||
| 125 | return $institutionConfiguration; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return InstitutionConfiguration |
||
| 130 | */ |
||
| 131 | public function rebuild() |
||
| 132 | { |
||
| 133 | // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid |
||
| 134 | if ($this->isMarkedAsDestroyed !== true) { |
||
| 135 | throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed'); |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->apply( |
||
| 139 | new NewInstitutionConfigurationCreatedEvent( |
||
| 140 | $this->institutionConfigurationId, |
||
| 141 | $this->institution, |
||
| 142 | UseRaLocationsOption::getDefault(), |
||
| 143 | ShowRaaContactInformationOption::getDefault(), |
||
| 144 | VerifyEmailOption::getDefault(), |
||
| 145 | NumberOfTokensPerIdentityOption::getDefault() |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | $this->apply(new AllowedSecondFactorListUpdatedEvent( |
||
| 149 | $this->institutionConfigurationId, |
||
| 150 | $this->institution, |
||
| 151 | AllowedSecondFactorList::blank() |
||
| 152 | )); |
||
| 153 | |||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | |||
| 157 | final public function __construct() |
||
| 160 | |||
| 161 | public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption) |
||
| 175 | |||
| 176 | public function configureShowRaaContactInformationOption(ShowRaaContactInformationOption $showRaaContactInformationOption) |
||
| 190 | |||
| 191 | public function configureVerifyEmailOption(VerifyEmailOption $verifyEmailOption) |
||
| 205 | |||
| 206 | public function configureNumberOfTokensPerIdentityOption( |
||
| 221 | |||
| 222 | public function updateAllowedSecondFactorList(AllowedSecondFactorList $allowedSecondFactorList) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param RaLocationId $raLocationId |
||
| 242 | * @param RaLocationName $raLocationName |
||
| 243 | * @param Location $location |
||
| 244 | * @param ContactInformation $contactInformation |
||
| 245 | */ |
||
| 246 | public function addRaLocation( |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param RaLocationId $raLocationId |
||
| 273 | * @param RaLocationName $raLocationName |
||
| 274 | * @param Location $location |
||
| 275 | * @param ContactInformation $contactInformation |
||
| 276 | */ |
||
| 277 | public function changeRaLocation( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param RaLocationId $raLocationId |
||
| 317 | */ |
||
| 318 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | public function destroy() |
||
| 339 | |||
| 340 | public function getAggregateRootId() |
||
| 344 | |||
| 345 | protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
||
| 356 | |||
| 357 | protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event) |
||
| 361 | |||
| 362 | protected function applyShowRaaContactInformationOptionChangedEvent( |
||
| 367 | |||
| 368 | protected function applyVerifyEmailOptionChangedEvent( |
||
| 373 | |||
| 374 | protected function applyNumberOfTokensPerIdentityOptionChangedEvent( |
||
| 379 | |||
| 380 | protected function applyAllowedSecondFactorListUpdatedEvent(AllowedSecondFactorListUpdatedEvent $event) |
||
| 384 | |||
| 385 | protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event) |
||
| 396 | |||
| 397 | protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event) |
||
| 402 | |||
| 403 | protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event) |
||
| 408 | |||
| 409 | protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event) |
||
| 414 | |||
| 415 | protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 422 | * @param InstitutionConfigurationRemovedEvent $event |
||
| 423 | */ |
||
| 424 | protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event) |
||
| 436 | } |
||
| 437 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.