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 |
||
| 20 | abstract class LockingRepository extends AbstractRepository |
||
| 21 | { |
||
| 22 | |||
| 23 | private $lockManager; |
||
| 24 | |||
| 25 | 23 | public function __construct($className, EventBusInterface $eventBus, LockManagerInterface $lockManager) |
|
| 30 | |||
| 31 | 3 | View Code Duplication | public function add(AggregateRootInterface $object) |
| 43 | |||
| 44 | 14 | View Code Duplication | public function load($id, $expectedVersion = null) |
| 56 | |||
| 57 | 1 | View Code Duplication | protected function doDelete(AggregateRootInterface $object) |
| 70 | |||
| 71 | 9 | View Code Duplication | protected function doSave(AggregateRootInterface $object) |
| 84 | |||
| 85 | /** |
||
| 86 | * Perform the actual saving of the aggregate. All necessary locks have been verified. |
||
| 87 | * |
||
| 88 | * @param AggregateRootInterface $aggregate the aggregate to store |
||
| 89 | */ |
||
| 90 | protected abstract function doSaveWithLock(AggregateRootInterface $aggregate); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Perform the actual deleting of the aggregate. All necessary locks have been verifierd. |
||
| 94 | * |
||
| 95 | * @param AggregateRootInterface $aggregate the aggregate to delete |
||
| 96 | */ |
||
| 97 | protected abstract function doDeleteWithLock(AggregateRootInterface $aggregate); |
||
| 98 | } |
||
| 99 |
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.