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 |
||
| 41 | class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var InstitutionConfigurationId |
||
| 45 | */ |
||
| 46 | private $institutionConfigurationId; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var RaLocationList |
||
| 50 | */ |
||
| 51 | private $raLocations; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param InstitutionConfigurationId $institutionConfigurationId |
||
| 55 | * @param Institution $institution |
||
| 56 | * @return InstitutionConfiguration |
||
|
|
|||
| 57 | */ |
||
| 58 | public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
||
| 65 | |||
| 66 | final private function __construct() |
||
| 69 | |||
| 70 | public function addRaLocation( |
||
| 71 | RaLocationId $raLocationId, |
||
| 72 | RaLocationName $raLocationName, |
||
| 73 | Location $location, |
||
| 74 | ContactInformation $contactInformation |
||
| 75 | ) { |
||
| 76 | View Code Duplication | if ($this->raLocations->containsWithId($raLocationId)) { |
|
| 77 | throw new DomainException(sprintf( |
||
| 78 | 'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":' |
||
| 79 | . 'it is already present', |
||
| 80 | $raLocationId, |
||
| 81 | $this->getAggregateRootId() |
||
| 82 | )); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->apply(new RaLocationAddedEvent( |
||
| 86 | $this->institutionConfigurationId, |
||
| 87 | $raLocationId, |
||
| 88 | $raLocationName, |
||
| 89 | $location, |
||
| 90 | $contactInformation |
||
| 91 | )); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function changeRaLocation( |
||
| 131 | |||
| 132 | public function removeRaLocation(RaLocationId $raLocationId) |
||
| 145 | |||
| 146 | public function getAggregateRootId() |
||
| 150 | |||
| 151 | public function applyInstitutionConfigurationCreatedEvent() |
||
| 155 | |||
| 156 | public function applyRaLocationAddedEvent() |
||
| 160 | |||
| 161 | public function applyRaLocationRenamedEvent() |
||
| 165 | |||
| 166 | public function applyRaLocationRelocatedEvent() |
||
| 170 | |||
| 171 | public function applyRaLocationContactInformationChangedEvent() |
||
| 175 | |||
| 176 | public function applyRaLocationRemovedEvent() |
||
| 180 | } |
||
| 181 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.