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 |
||
10 | final class ModelCollection implements ModelCollectionInterface |
||
11 | { |
||
12 | use ModelSortTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $modelClass; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $foreignField; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $foreignId; |
||
28 | |||
29 | /** |
||
30 | * @var array|null |
||
31 | */ |
||
32 | private $orderBy; |
||
33 | |||
34 | /** |
||
35 | * @var ModelInterface[]|array |
||
36 | */ |
||
37 | private $models; |
||
38 | |||
39 | /** |
||
40 | * @var \ReflectionProperty |
||
41 | */ |
||
42 | private $propertyReflection; |
||
43 | |||
44 | /** |
||
45 | * @param string $modelClass |
||
46 | * @param string $foreignField |
||
47 | * @param string $foreignId |
||
48 | * @param array|null $orderBy |
||
49 | */ |
||
50 | 11 | View Code Duplication | public function __construct( |
66 | |||
67 | /** |
||
68 | * @param ModelInterface $model |
||
69 | * |
||
70 | * @return ModelCollectionInterface |
||
71 | */ |
||
72 | 1 | View Code Duplication | public function addModel(ModelInterface $model): ModelCollectionInterface |
80 | |||
81 | /** |
||
82 | * @param ModelInterface $model |
||
83 | * |
||
84 | * @return ModelCollectionInterface |
||
85 | */ |
||
86 | 1 | View Code Duplication | public function removeModel(ModelInterface $model): ModelCollectionInterface |
96 | |||
97 | /** |
||
98 | * @param ModelInterface[]|array $models |
||
99 | * |
||
100 | * @return ModelCollectionInterface |
||
101 | */ |
||
102 | 1 | View Code Duplication | public function setModels(array $models): ModelCollectionInterface |
113 | |||
114 | /** |
||
115 | * @return ModelInterface[]|array |
||
116 | */ |
||
117 | 2 | public function getModels(): array |
|
121 | |||
122 | /** |
||
123 | * @return ModelInterface[]|array |
||
124 | */ |
||
125 | 1 | public function getInitialModels(): array |
|
129 | |||
130 | /** |
||
131 | * @return \ArrayIterator |
||
132 | */ |
||
133 | 1 | public function getIterator() |
|
137 | |||
138 | /** |
||
139 | * @return int |
||
140 | */ |
||
141 | 1 | public function count() |
|
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | 1 | View Code Duplication | public function jsonSerialize(): array |
160 | |||
161 | /** |
||
162 | * @throws \LogicException |
||
163 | */ |
||
164 | View Code Duplication | private function jsonSerializableOrException() |
|
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | 1 | public function getForeignField(): string |
|
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 1 | public function getForeignId(): string |
|
188 | } |
||
189 |
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.