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:
Complex classes like TranslationUpdated often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TranslationUpdated, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | View Code Duplication | class TranslationUpdated implements CreatableFromArray |
|
|
|||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $id; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $content; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $unverified; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $excluded; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $pluralSuffix; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $createdAt; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $updatedAt; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $placeholders = []; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | private $wordCount; |
||
63 | |||
64 | /** |
||
65 | * @var Key |
||
66 | */ |
||
67 | private $key; |
||
68 | |||
69 | /** |
||
70 | * @var Locale |
||
71 | */ |
||
72 | private $locale; |
||
73 | |||
74 | /** |
||
75 | * @var User |
||
76 | */ |
||
77 | private $user; |
||
78 | |||
79 | /** |
||
80 | * @param array $data |
||
81 | * |
||
82 | * @return TranslationUpdated |
||
83 | */ |
||
84 | public static function createFromArray(array $data) |
||
127 | |||
128 | public function getId(): string |
||
132 | |||
133 | private function setId(string $id) |
||
137 | |||
138 | public function getContent(): string |
||
142 | |||
143 | private function setContent(string $content) |
||
147 | |||
148 | public function isUnverified(): bool |
||
152 | |||
153 | private function setUnverified(bool $unverified) |
||
157 | |||
158 | public function isExcluded(): bool |
||
162 | |||
163 | private function setExcluded(bool $excluded) |
||
167 | |||
168 | public function getPluralSuffix(): string |
||
172 | |||
173 | private function setPluralSuffix(string $pluralSuffix) |
||
177 | |||
178 | public function getKey(): Key |
||
182 | |||
183 | private function setKey(Key $key) |
||
187 | |||
188 | public function getCreatedAt(): string |
||
192 | |||
193 | private function setCreatedAt(string $createdAt) |
||
197 | |||
198 | public function getUpdatedAt(): string |
||
202 | |||
203 | private function setUpdatedAt(string $updatedAt) |
||
207 | |||
208 | public function getWordCount(): int |
||
212 | |||
213 | private function setWordCount(int $wordCount) |
||
217 | |||
218 | public function getPlaceholders(): array |
||
222 | |||
223 | private function setPlaceholders(array $placeholders) |
||
227 | |||
228 | public function getLocale(): Locale |
||
232 | |||
233 | private function setLocale(Locale $locale) |
||
237 | |||
238 | public function getUser(): User |
||
242 | |||
243 | private function setUser(User $user) |
||
247 | } |
||
248 |
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.