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 |
||
| 47 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var InstitutionConfigurationId |
||
| 51 | */ |
||
| 52 | private $institutionConfigurationId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Institution |
||
| 56 | */ |
||
| 57 | private $institution; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var RaLocationList |
||
| 61 | */ |
||
| 62 | private $raLocations; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var UseRaLocationsOption |
||
| 66 | */ |
||
| 67 | private $useRaLocationsOption; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var ShowRaaContactInformationOption |
||
| 71 | */ |
||
| 72 | private $showRaaContactInformationOption; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 76 | * @param Institution $institution |
||
| 77 | * @return InstitutionConfiguration |
||
| 78 | */ |
||
| 79 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 96 | |||
| 97 | final public function __construct() |
||
| 100 | |||
| 101 | public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption) |
||
| 102 | { |
||
| 103 | if ($this->useRaLocationsOption->equals($useRaLocationsOption)) { |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->apply( |
||
| 108 | new UseRaLocationsOptionChangedEvent( |
||
| 109 | $this->institutionConfigurationId, |
||
| 110 | $this->institution, |
||
| 111 | $useRaLocationsOption |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function configureShowRaaContactInformationOption(ShowRaaContactInformationOption $showRaaContactInformationOption) |
||
| 117 | { |
||
| 118 | if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->apply( |
||
| 123 | new ShowRaaContactInformationOptionChangedEvent( |
||
| 124 | $this->institutionConfigurationId, |
||
| 125 | $this->institution, |
||
| 126 | $showRaaContactInformationOption |
||
| 127 | ) |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param RaLocationId $raLocationId |
||
| 133 | * @param RaLocationName $raLocationName |
||
| 134 | * @param Location $location |
||
| 135 | * @param ContactInformation $contactInformation |
||
| 136 | */ |
||
| 137 | public function addRaLocation( |
||
| 138 | RaLocationId $raLocationId, |
||
| 139 | RaLocationName $raLocationName, |
||
| 140 | Location $location, |
||
| 141 | ContactInformation $contactInformation |
||
| 142 | ) { |
||
| 143 | View Code Duplication | if ($this->raLocations->containsWithId($raLocationId)) { |
|
| 144 | throw new DomainException(sprintf( |
||
| 145 | 'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":' |
||
| 146 | . ' it is already present', |
||
| 147 | $raLocationId, |
||
| 148 | $this->getAggregateRootId() |
||
| 149 | )); |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->apply(new RaLocationAddedEvent( |
||
| 153 | $this->institutionConfigurationId, |
||
| 154 | $this->institution, |
||
| 155 | $raLocationId, |
||
| 156 | $raLocationName, |
||
| 157 | $location, |
||
| 158 | $contactInformation |
||
| 159 | )); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param RaLocationId $raLocationId |
||
| 164 | * @param RaLocationName $raLocationName |
||
| 165 | * @param Location $location |
||
| 166 | * @param ContactInformation $contactInformation |
||
| 167 | */ |
||
| 168 | public function changeRaLocation( |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param RaLocationId $raLocationId |
||
| 208 | */ |
||
| 209 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 210 | { |
||
| 211 | View Code Duplication | if (!$this->raLocations->containsWithId($raLocationId)) { |
|
| 212 | throw new DomainException(sprintf( |
||
| 213 | 'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":' |
||
| 214 | . ' it is not present', |
||
| 215 | $raLocationId, |
||
| 216 | $this->getAggregateRootId() |
||
| 217 | )); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId)); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getAggregateRootId() |
||
| 224 | { |
||
| 225 | return $this->institutionConfigurationId; |
||
| 226 | } |
||
| 227 | |||
| 228 | protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
||
| 229 | { |
||
| 230 | $this->institutionConfigurationId = $event->institutionConfigurationId; |
||
| 231 | $this->institution = $event->institution; |
||
| 232 | $this->useRaLocationsOption = $event->useRaLocationsOption; |
||
| 233 | $this->showRaaContactInformationOption = $event->showRaaContactInformationOption; |
||
| 234 | $this->raLocations = new RaLocationList([]); |
||
| 235 | } |
||
| 236 | |||
| 237 | protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event) |
||
| 241 | |||
| 242 | protected function applyShowRaaContactInformationOptionChangedEvent( |
||
| 247 | |||
| 248 | protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event) |
||
| 249 | { |
||
| 250 | $this->raLocations->add( |
||
| 251 | RaLocation::create( |
||
| 252 | $event->raLocationId, |
||
| 253 | $event->raLocationName, |
||
| 254 | $event->location, |
||
| 255 | $event->contactInformation |
||
| 256 | ) |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event) |
||
| 261 | { |
||
| 262 | $raLocation = $this->raLocations->getById($event->raLocationId); |
||
| 263 | $raLocation->rename($event->raLocationName); |
||
| 264 | } |
||
| 265 | |||
| 266 | protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event) |
||
| 271 | |||
| 272 | protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event) |
||
| 277 | |||
| 278 | protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event) |
||
| 282 | } |
||
| 283 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.