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 |
||
19 | final class Metadata |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $metadata; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $notes = []; |
||
30 | |||
31 | /** |
||
32 | * @param array $metadata |
||
33 | */ |
||
34 | 3 | public function __construct($metadata) |
|
45 | |||
46 | /** |
||
47 | * @return string|null |
||
48 | */ |
||
49 | 1 | View Code Duplication | public function getState() |
58 | |||
59 | /** |
||
60 | * @param string $state |
||
61 | */ |
||
62 | public function setState($state) |
||
63 | { |
||
64 | $this->removeAllInCategory('state'); |
||
65 | $this->addCategory('state', $state); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return string|null |
||
70 | */ |
||
71 | View Code Duplication | public function getDesc() |
|
72 | { |
||
73 | $notes = $this->getAllInCategory('desc'); |
||
74 | foreach ($notes as $note) { |
||
75 | if (isset($note['content'])) { |
||
76 | return $note['content']; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return null; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get the extracted translation if any. |
||
85 | * |
||
86 | * @return string|null |
||
87 | */ |
||
88 | View Code Duplication | public function getTranslation() |
|
89 | { |
||
90 | $notes = $this->getAllInCategory('translation'); |
||
91 | foreach ($notes as $note) { |
||
92 | if (isset($note['content'])) { |
||
93 | return $note['content']; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return null; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | */ |
||
103 | 1 | View Code Duplication | public function isApproved() |
114 | |||
115 | /** |
||
116 | * @param bool $bool |
||
117 | */ |
||
118 | public function setApproved($bool) |
||
123 | |||
124 | /** |
||
125 | * @return array |
||
126 | */ |
||
127 | public function getSourceLocations() |
||
141 | |||
142 | /** |
||
143 | * Add metadata. |
||
144 | * |
||
145 | * @param string $name |
||
146 | * @param string $content |
||
147 | */ |
||
148 | public function addCategory($name, $content, $priority = 1) |
||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | public function toArray() |
||
163 | |||
164 | /** |
||
165 | * Return all notes for one category. It will also order data according to priority. |
||
166 | * |
||
167 | * @param string $category |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 1 | public function getAllInCategory($category) |
|
172 | { |
||
173 | 1 | $data = []; |
|
174 | 1 | foreach ($this->notes as $note) { |
|
175 | 1 | if (!isset($note['category'])) { |
|
176 | continue; |
||
177 | } |
||
178 | 1 | if ($note['category'] === $category) { |
|
179 | 1 | if (!isset($note['priority'])) { |
|
180 | 1 | $note['priority'] = '1'; |
|
181 | } |
||
182 | 1 | $data[] = $note; |
|
183 | } |
||
184 | } |
||
185 | |||
186 | \usort($data, function (array $a, array $b) { |
||
187 | return (int) $a['priority'] - (int) $b['priority']; |
||
188 | 1 | }); |
|
189 | |||
190 | 1 | return $data; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Remove all metadata in category. |
||
195 | * |
||
196 | * @param string $category |
||
197 | */ |
||
198 | public function removeAllInCategory($category) |
||
206 | } |
||
207 |
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.