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 | class AggregateRepository implements RepositoryInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var EventStoreInterface |
||
| 34 | */ |
||
| 35 | protected $eventStore; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $aggregateClassName; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * AggregateRepository constructor. |
||
| 44 | * |
||
| 45 | * @param EventStoreInterface $eventStore |
||
| 46 | * @param string $aggregateClassName |
||
| 47 | */ |
||
| 48 | public function __construct(EventStoreInterface $eventStore, $aggregateClassName) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function get(IdInterface $id) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function persist($element) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function persistAll($elements) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function remove($element) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Load a aggregate history from the storage. |
||
| 120 | * |
||
| 121 | * @param IdInterface $id |
||
| 122 | * |
||
| 123 | * @return EventStream|null |
||
| 124 | */ |
||
| 125 | protected function loadHistory(IdInterface $id) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Save the aggregate history. |
||
| 132 | * |
||
| 133 | * @param EventSourcedAggregateRootInterface $aggregateRoot |
||
| 134 | */ |
||
| 135 | protected function saveHistory(EventSourcedAggregateRootInterface $aggregateRoot) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param IdInterface $id |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | protected function streamName(IdInterface $id) |
||
| 166 | } |
||
| 167 |
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.