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 |
||
| 25 | class SnapshotAggregateRepository extends AggregateRepository |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var SnapshotStoreInterface |
||
| 29 | */ |
||
| 30 | protected $snapshotStore; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var SnapshottingPolicyInterface |
||
| 34 | */ |
||
| 35 | protected $snapshottingPolicy; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * AggregateRepository constructor. |
||
| 39 | * |
||
| 40 | * @param EventStoreInterface $eventStore |
||
| 41 | * @param SnapshotStoreInterface $snapshotStore |
||
| 42 | * @param SnapshottingPolicyInterface $snapshottingPolicy |
||
| 43 | * @param string $aggregateClassName |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function get(IdInterface $id) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function persist($element) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Load a aggregate snapshot from the storage. |
||
| 92 | * |
||
| 93 | * @param IdInterface $id |
||
| 94 | * |
||
| 95 | * @return Snapshot|null |
||
| 96 | */ |
||
| 97 | protected function loadSnapshot(IdInterface $id) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Save the aggregate snapshot. |
||
| 104 | * |
||
| 105 | * @param EventSourcedAggregateRootInterface $aggregateRoot |
||
| 106 | */ |
||
| 107 | protected function saveSnapshot(EventSourcedAggregateRootInterface $aggregateRoot) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Snapshot $snapshot |
||
| 116 | * |
||
| 117 | * @return EventSourcedAggregateRootInterface |
||
| 118 | */ |
||
| 119 | protected function snapshotToAggregateRoot(Snapshot $snapshot) |
||
| 133 | } |
||
| 134 |
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.