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 |
||
| 42 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var InstitutionConfigurationId |
||
| 46 | */ |
||
| 47 | private $institutionConfigurationId; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var RaLocationList |
||
| 51 | */ |
||
| 52 | private $raLocations; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 56 | * @param Institution $institution |
||
| 57 | * @return InstitutionConfiguration |
||
| 58 | */ |
||
| 59 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 68 | |||
| 69 | final public function __construct() |
||
| 72 | |||
| 73 | public function addRaLocation( |
||
| 74 | RaLocationId $raLocationId, |
||
| 75 | RaLocationName $raLocationName, |
||
| 76 | Location $location, |
||
| 77 | ContactInformation $contactInformation |
||
| 78 | ) { |
||
| 79 | View Code Duplication | if ($this->raLocations->containsWithId($raLocationId)) { |
|
|
|
|||
| 80 | throw new DomainException(sprintf( |
||
| 81 | 'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":' |
||
| 82 | . 'it is already present', |
||
| 83 | $raLocationId, |
||
| 84 | $this->getAggregateRootId() |
||
| 85 | )); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->apply(new RaLocationAddedEvent( |
||
| 89 | $this->institutionConfigurationId, |
||
| 90 | $raLocationId, |
||
| 91 | $raLocationName, |
||
| 92 | $location, |
||
| 93 | $contactInformation |
||
| 94 | )); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function changeRaLocation( |
||
| 134 | |||
| 135 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 148 | |||
| 149 | public function getAggregateRootId() |
||
| 153 | |||
| 154 | public function applyInstitutionConfigurationCreatedEvent() |
||
| 158 | |||
| 159 | public function applyRaLocationAddedEvent() |
||
| 163 | |||
| 164 | public function applyRaLocationRenamedEvent() |
||
| 168 | |||
| 169 | public function applyRaLocationRelocatedEvent() |
||
| 173 | |||
| 174 | public function applyRaLocationContactInformationChangedEvent() |
||
| 178 | |||
| 179 | public function applyRaLocationRemovedEvent() |
||
| 183 | } |
||
| 184 |
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.