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 Storage implements StorageInterface |
||
31 | { |
||
32 | use NormalizeNamespaceTrait; |
||
33 | |||
34 | /** |
||
35 | * @var Connection |
||
36 | */ |
||
37 | protected $connection; |
||
38 | |||
39 | /** |
||
40 | * @var ModelBag |
||
41 | */ |
||
42 | protected $models; |
||
43 | |||
44 | /** |
||
45 | * @var RelationFactoryInterface |
||
46 | */ |
||
47 | protected $factory; |
||
48 | |||
49 | /** |
||
50 | * @var AccessorInterface |
||
51 | */ |
||
52 | protected $accessor; |
||
53 | |||
54 | /** |
||
55 | * @var EventDispatcherInterface |
||
56 | */ |
||
57 | protected $dispatcher; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | * |
||
62 | * @param Connection $connection |
||
63 | * @param ModelBag $models |
||
64 | * @param EventDispatcherInterface $dispatcher |
||
65 | */ |
||
66 | View Code Duplication | public function __construct(Connection $connection, ModelBag $models, EventDispatcherInterface $dispatcher) |
|
74 | |||
75 | /** |
||
76 | * Returns connection |
||
77 | * |
||
78 | * @return Connection |
||
79 | */ |
||
80 | public function connection() |
||
84 | |||
85 | /** |
||
86 | * Registers event listener |
||
87 | * |
||
88 | * @param string $event |
||
89 | * @param callable $listener |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function registerEventListener($event, callable $listener) |
||
99 | |||
100 | /** |
||
101 | * Sets read operation |
||
102 | * |
||
103 | * @param string $entityName |
||
104 | * |
||
105 | * @return ReadQueryInterface |
||
106 | */ |
||
107 | public function read($entityName) |
||
117 | |||
118 | /** |
||
119 | * Sets read one operation |
||
120 | * |
||
121 | * @param string $entityName |
||
122 | * |
||
123 | * @return ReadQueryInterface |
||
124 | */ |
||
125 | public function readOne($entityName) |
||
135 | |||
136 | /** |
||
137 | * Sets write operation |
||
138 | * |
||
139 | * @param array|object $instance |
||
140 | * @param null|string|object $entity |
||
141 | * |
||
142 | * @return WriteQueryInterface |
||
143 | */ |
||
144 | View Code Duplication | public function write($instance, $entity = null) |
|
157 | |||
158 | /** |
||
159 | * Sets update operation |
||
160 | * |
||
161 | * @param array|object $instance |
||
162 | * @param null|string|object $entity |
||
163 | * |
||
164 | * @return UpdateQueryInterface |
||
165 | */ |
||
166 | View Code Duplication | public function update($instance, $entity = null) |
|
179 | |||
180 | /** |
||
181 | * Sets insert operation |
||
182 | * |
||
183 | * @param array|object $instance |
||
184 | * @param null|string|object $entity |
||
185 | * |
||
186 | * @return InsertQueryInterface |
||
187 | */ |
||
188 | View Code Duplication | public function insert($instance, $entity = null) |
|
201 | |||
202 | /** |
||
203 | * Sets delete operation |
||
204 | * |
||
205 | * @param array|object $instance |
||
206 | * @param null|string|object $entity |
||
207 | * |
||
208 | * @return DeleteQueryInterface |
||
209 | */ |
||
210 | View Code Duplication | public function delete($instance, $entity = null) |
|
223 | |||
224 | /** |
||
225 | * Reassigns entity/instance variables if entity is object |
||
226 | * |
||
227 | * @param array|object $instance |
||
228 | * @param null|string|object $entity |
||
229 | * |
||
230 | * @return array |
||
231 | * @throws StorageException |
||
232 | */ |
||
233 | protected function reassignEntity($instance, $entity = null) |
||
245 | } |
||
246 |
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.