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 |
||
11 | final class LazyModelCollection implements ModelCollectionInterface |
||
12 | { |
||
13 | use ModelSortTrait; |
||
14 | |||
15 | /** |
||
16 | * @var ResolverInterface |
||
17 | */ |
||
18 | private $resolver; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $modelClass; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $foreignField; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $foreignId; |
||
34 | |||
35 | /** |
||
36 | * @var array|null |
||
37 | */ |
||
38 | private $orderBy; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $resolved = false; |
||
44 | |||
45 | /** |
||
46 | * @var ModelInterface[]|array |
||
47 | */ |
||
48 | private $initialModels; |
||
49 | |||
50 | /** |
||
51 | * @var ModelInterface[]|array |
||
52 | */ |
||
53 | private $models; |
||
54 | |||
55 | /** |
||
56 | * @param ResolverInterface $resolver |
||
57 | * @param string $modelClass |
||
58 | * @param string $foreignField |
||
59 | * @param string $foreignId |
||
60 | * @param array|null $orderBy |
||
61 | */ |
||
62 | View Code Duplication | public function __construct( |
|
75 | |||
76 | private function resolveModels() |
||
94 | |||
95 | /** |
||
96 | * @param ModelInterface $model |
||
97 | * |
||
98 | * @return ModelCollectionInterface |
||
99 | */ |
||
100 | public function addModel(ModelInterface $model): ModelCollectionInterface |
||
108 | |||
109 | /** |
||
110 | * @param ModelInterface $model |
||
111 | * |
||
112 | * @return ModelCollectionInterface |
||
113 | */ |
||
114 | View Code Duplication | public function removeModel(ModelInterface $model): ModelCollectionInterface |
|
124 | |||
125 | /** |
||
126 | * @param ModelInterface[]|array $models |
||
127 | * |
||
128 | * @return ModelCollectionInterface |
||
129 | */ |
||
130 | public function setModels(array $models): ModelCollectionInterface |
||
141 | |||
142 | /** |
||
143 | * @return ModelInterface[]|array |
||
144 | */ |
||
145 | public function getModels(): array |
||
151 | |||
152 | /** |
||
153 | * @return ModelInterface[]|array |
||
154 | */ |
||
155 | public function getInitialModels(): array |
||
161 | |||
162 | /** |
||
163 | * @return \ArrayIterator |
||
164 | */ |
||
165 | public function getIterator() |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public function jsonSerialize(): array |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getForeignField(): string |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getForeignId(): string |
||
202 | } |
||
203 |
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.