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 |
||
9 | class EloquentWriteRepository extends BaseEloquentRepository implements WriteRepository |
||
10 | { |
||
11 | /** |
||
12 | * Returns the total amount of elements in the repository given the restrictions provided by the Filter object. |
||
13 | * |
||
14 | * @param Filter|null $filter |
||
15 | * |
||
16 | * @return int |
||
17 | */ |
||
18 | View Code Duplication | public function count(Filter $filter = null) |
|
29 | |||
30 | /** |
||
31 | * Returns whether an entity with the given id exists. |
||
32 | * |
||
33 | * @param $id |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | public function exists(Identity $id) |
||
44 | |||
45 | /** |
||
46 | * Adds a new entity to the storage. |
||
47 | * |
||
48 | * @param Identity $value |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function add(Identity $value) |
||
59 | |||
60 | /** |
||
61 | * Adds a collections of entities to the storage. |
||
62 | * |
||
63 | * @param array $values |
||
64 | * |
||
65 | * @return mixed |
||
66 | * |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function addAll(array $values) |
||
84 | |||
85 | /** |
||
86 | * Removes the entity with the given id. |
||
87 | * |
||
88 | * @param $id |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function remove(Identity $id) |
||
98 | |||
99 | /** |
||
100 | * Removes all elements in the repository given the restrictions provided by the Filter object. |
||
101 | * If $filter is null, all the repository data will be deleted. |
||
102 | * |
||
103 | * @param Filter $filter |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | View Code Duplication | public function removeAll(Filter $filter = null) |
|
118 | |||
119 | /** |
||
120 | * Repository data is added or removed as a whole block. |
||
121 | * Must work or fail and rollback any persisted/erased data. |
||
122 | * |
||
123 | * @param callable $transaction |
||
124 | * |
||
125 | * @throws \Exception |
||
126 | */ |
||
127 | public function transactional(callable $transaction) |
||
135 | } |
||
136 |
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.