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 |
||
21 | class EntityRepository implements Repository |
||
22 | { |
||
23 | /** @var string */ |
||
24 | private $name; |
||
25 | /** @var StorageDriver */ |
||
26 | private $driver; |
||
27 | |||
28 | public function __construct($name, StorageDriver $driver) |
||
33 | |||
34 | /** |
||
35 | * @return string |
||
36 | */ |
||
37 | 4 | public function getName() |
|
41 | |||
42 | /** |
||
43 | * @return \ReflectionClass |
||
44 | */ |
||
45 | public function getEntityClass() |
||
49 | |||
50 | /** |
||
51 | * TODO: remove me! It was only used to find a repo by sub-classes ... we no longer need this |
||
52 | * @return string[] |
||
53 | */ |
||
54 | public function getEntityClassAliases() |
||
58 | |||
59 | /** |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function buildIndexes() |
||
66 | |||
67 | /** |
||
68 | * Insert a database entry |
||
69 | * |
||
70 | * @param mixed $subject |
||
71 | * |
||
72 | * @return InsertOneResult |
||
73 | * |
||
74 | * @throws SlumberRuntimeException When the given subject cannot be stored in this repository |
||
75 | * @throws DuplicateError When the underlying database permits the insert, due to a duplicate key exception |
||
76 | */ |
||
77 | 3 | View Code Duplication | public function insert($subject) |
87 | |||
88 | /** |
||
89 | * @param mixed $subject |
||
90 | * |
||
91 | * @return SaveOneResult |
||
92 | * |
||
93 | * @throws SlumberRuntimeException When the given subject cannot be stored in this repository |
||
94 | * @throws DuplicateError When the underlying database permits the insert, due to a duplicate key exception |
||
95 | */ |
||
96 | 25 | View Code Duplication | public function save($subject) |
106 | |||
107 | /** |
||
108 | * @param $query |
||
109 | * |
||
110 | * @return Cursor |
||
111 | */ |
||
112 | 11 | public function find(array $query = null) |
|
116 | |||
117 | /** |
||
118 | * @param array|null $query |
||
119 | * |
||
120 | * @return mixed|null |
||
121 | */ |
||
122 | 19 | public function findOne(array $query = null) |
|
126 | |||
127 | /** |
||
128 | * @param mixed $id |
||
129 | * |
||
130 | * @return mixed|null |
||
131 | */ |
||
132 | 19 | public function findById($id) |
|
138 | |||
139 | /** |
||
140 | * Find one object by its public reference |
||
141 | * |
||
142 | * @param string $reference |
||
143 | * |
||
144 | * @return mixed|null |
||
145 | */ |
||
146 | 1 | public function findByReference($reference) |
|
152 | |||
153 | /** |
||
154 | * @param $entity |
||
155 | * |
||
156 | * @return Result\RemoveResult |
||
157 | */ |
||
158 | 3 | public function remove($entity) |
|
162 | |||
163 | /** |
||
164 | * Remove all from this collection |
||
165 | * |
||
166 | * @param array|null $query |
||
167 | * |
||
168 | * @return Result\RemoveResult |
||
169 | */ |
||
170 | 25 | public function removeAll(array $query = null) |
|
174 | |||
175 | /** |
||
176 | * @param $subject |
||
177 | * |
||
178 | * @throw SlumberRuntimeException |
||
179 | */ |
||
180 | 28 | private function checkObjectCompatibility($subject) |
|
193 | } |
||
194 |
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.