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 ModelCollection implements ModelCollectionInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var ModelInterface[]|array |
||
13 | */ |
||
14 | private $initialModels; |
||
15 | |||
16 | /** |
||
17 | * @var ModelInterface[]|array |
||
18 | */ |
||
19 | private $models; |
||
20 | |||
21 | /** |
||
22 | * @param ModelInterface[]|array $models |
||
23 | */ |
||
24 | public function __construct(array $models = []) |
||
25 | { |
||
26 | $models = $this->modelsWithIdKey($models); |
||
27 | |||
28 | $this->initialModels = $models; |
||
29 | $this->models = $models; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param ModelInterface[]|array $models |
||
34 | * |
||
35 | * @return ModelInterface[]|array |
||
36 | */ |
||
37 | View Code Duplication | private function modelsWithIdKey(array $models): array |
|
|
|||
38 | { |
||
39 | $modelsWithIdKey = []; |
||
40 | foreach ($models as $model) { |
||
41 | if (!$model instanceof ModelInterface) { |
||
42 | throw new \InvalidArgumentException( |
||
43 | sprintf('Model with index %d needs to implement: %s', ModelInterface::class) |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | $modelsWithIdKey[$model->getId()] = $model; |
||
48 | } |
||
49 | |||
50 | return $modelsWithIdKey; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return ModelInterface |
||
55 | */ |
||
56 | public function current() |
||
57 | { |
||
58 | return current($this->models); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return ModelInterface|false |
||
63 | */ |
||
64 | public function next() |
||
65 | { |
||
66 | return next($this->models); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | public function key() |
||
73 | { |
||
74 | return key($this->models); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function valid() |
||
81 | { |
||
82 | return (bool) current($this->models); |
||
83 | } |
||
84 | |||
85 | public function rewind() |
||
86 | { |
||
87 | reset($this->models); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param ModelInterface[]|array $models |
||
92 | */ |
||
93 | public function set(array $models) |
||
94 | { |
||
95 | $this->models = $this->modelsWithIdKey($models); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return ModelInterface[]|array |
||
100 | */ |
||
101 | public function toPersistence(): array |
||
102 | { |
||
103 | return $this->models; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return ModelInterface[]|array |
||
108 | */ |
||
109 | View Code Duplication | public function removeFromPersistence(): array |
|
110 | { |
||
111 | $removeFromPersistenceModels = []; |
||
112 | foreach ($this->initialModels as $initialModel) { |
||
113 | if (!isset($this->models[$initialModel->getId()])) { |
||
114 | $removeFromPersistenceModels[$initialModel->getId()] = $initialModel; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | return $removeFromPersistenceModels; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return array |
||
123 | */ |
||
124 | public function jsonSerialize(): array |
||
133 | } |
||
134 |
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.