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 |
||
| 43 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var InstitutionConfigurationId |
||
| 47 | */ |
||
| 48 | private $institutionConfigurationId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Institution |
||
| 52 | */ |
||
| 53 | private $institution; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var RaLocationList |
||
| 57 | */ |
||
| 58 | private $raLocations; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 62 | * @param Institution $institution |
||
| 63 | * @return InstitutionConfiguration |
||
| 64 | */ |
||
| 65 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 74 | |||
| 75 | final public function __construct() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param RaLocationId $raLocationId |
||
| 81 | * @param RaLocationName $raLocationName |
||
| 82 | * @param Location $location |
||
| 83 | * @param ContactInformation $contactInformation |
||
| 84 | */ |
||
| 85 | public function addRaLocation( |
||
| 86 | RaLocationId $raLocationId, |
||
| 87 | RaLocationName $raLocationName, |
||
| 88 | Location $location, |
||
| 89 | ContactInformation $contactInformation |
||
| 90 | ) { |
||
| 91 | View Code Duplication | if ($this->raLocations->containsWithId($raLocationId)) { |
|
|
|
|||
| 92 | throw new DomainException(sprintf( |
||
| 93 | 'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":' |
||
| 94 | . 'it is already present', |
||
| 95 | $raLocationId, |
||
| 96 | $this->getAggregateRootId() |
||
| 97 | )); |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->apply(new RaLocationAddedEvent( |
||
| 101 | $this->institutionConfigurationId, |
||
| 102 | $raLocationId, |
||
| 103 | $raLocationName, |
||
| 104 | $location, |
||
| 105 | $contactInformation |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param RaLocationId $raLocationId |
||
| 111 | * @param RaLocationName $raLocationName |
||
| 112 | * @param Location $location |
||
| 113 | * @param ContactInformation $contactInformation |
||
| 114 | */ |
||
| 115 | public function changeRaLocation( |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param RaLocationId $raLocationId |
||
| 155 | */ |
||
| 156 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 169 | |||
| 170 | public function getAggregateRootId() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return InstitutionConfigurationId |
||
| 177 | */ |
||
| 178 | public function getInstitutionConfigurationId() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return Institution |
||
| 185 | */ |
||
| 186 | public function getInstitution() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return RaLocationList |
||
| 193 | */ |
||
| 194 | public function getRaLocations() |
||
| 198 | |||
| 199 | public function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
||
| 205 | |||
| 206 | public function applyRaLocationAddedEvent(RaLocationAddedEvent $event) |
||
| 217 | |||
| 218 | public function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event) |
||
| 223 | |||
| 224 | public function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event) |
||
| 229 | |||
| 230 | public function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event) |
||
| 235 | |||
| 236 | public function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event) |
||
| 240 | } |
||
| 241 |
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.