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 |
||
| 30 | final class Whitelist extends EventSourcedAggregateRoot implements WhitelistApi |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * There can ever be only one whitelist, so using a fixed UUIDv4 |
||
| 34 | */ |
||
| 35 | const WHITELIST_AGGREGATE_ID = '125ccee5-d650-437a-a0b0-6bf17c8188fa'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var InstitutionCollection The collection of institutions currently on the whitelist |
||
| 39 | */ |
||
| 40 | private $whitelist; |
||
| 41 | |||
| 42 | public function __construct() |
||
| 45 | |||
| 46 | public function getAggregateRootId() |
||
| 50 | |||
| 51 | public static function create(InstitutionCollection $institutionCollection) |
||
| 52 | { |
||
| 53 | $whitelist = new self(); |
||
| 54 | $whitelist->apply(new WhitelistCreatedEvent($institutionCollection)); |
||
| 55 | |||
| 56 | return $whitelist; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function replaceAll(InstitutionCollection $institutionCollection) |
||
| 63 | |||
| 64 | View Code Duplication | public function add(InstitutionCollection $institutionCollection) |
|
|
|
|||
| 65 | { |
||
| 66 | foreach ($institutionCollection as $institution) { |
||
| 67 | if ($this->whitelist->contains($institution)) { |
||
| 68 | throw new DomainException(sprintf( |
||
| 69 | 'Cannot add institution "%s" as it is already whitelisted', |
||
| 70 | $institution |
||
| 71 | )); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->apply(new InstitutionsAddedToWhitelistEvent($institutionCollection)); |
||
| 76 | } |
||
| 77 | |||
| 78 | View Code Duplication | public function remove(InstitutionCollection $institutionCollection) |
|
| 79 | { |
||
| 80 | foreach ($institutionCollection as $institution) { |
||
| 81 | if (!$this->whitelist->contains($institution)) { |
||
| 82 | throw new DomainException(sprintf( |
||
| 83 | 'Cannot remove institution "%s" as it is not whitelisted', |
||
| 84 | $institution |
||
| 85 | )); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->apply(new InstitutionsRemovedFromWhitelistEvent($institutionCollection)); |
||
| 90 | } |
||
| 91 | |||
| 92 | protected function applyWhitelistCreatedEvent(WhitelistCreatedEvent $event) |
||
| 93 | { |
||
| 94 | $this->whitelist = new InstitutionCollection(); |
||
| 95 | $this->whitelist->addAllFrom($event->whitelistedInstitutions); |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function applyWhitelistReplacedEvent(WhitelistReplacedEvent $event) |
||
| 99 | { |
||
| 100 | $this->whitelist = new InstitutionCollection(); |
||
| 101 | $this->whitelist->addAllFrom($event->whitelistedInstitutions); |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event) |
||
| 108 | |||
| 109 | protected function applyInstitutionsRemovedFromWhitelistEvent(InstitutionsRemovedFromWhitelistEvent $event) |
||
| 113 | } |
||
| 114 |
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.