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 |
||
22 | class CatalogueManager |
||
23 | { |
||
24 | /** |
||
25 | * @var MessageCatalogue[] |
||
26 | */ |
||
27 | private $catalogues; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $projectRoot; |
||
33 | |||
34 | /** |
||
35 | * @param string $projectRoot |
||
36 | */ |
||
37 | public function __construct($projectRoot) |
||
41 | |||
42 | /** |
||
43 | * @param MessageCatalogue[] $catalogues |
||
44 | */ |
||
45 | public function load(array $catalogues) |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getDomains() |
||
63 | |||
64 | /** |
||
65 | * @param string $locale |
||
66 | * @param string $domain |
||
67 | * |
||
68 | * @return Message[] |
||
69 | */ |
||
70 | public function getMessages($locale, $domain) |
||
83 | |||
84 | /** |
||
85 | * @param string $domain |
||
86 | * @param string $key |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getTranslations($domain, $key) |
||
101 | |||
102 | /** |
||
103 | * @param string $domain |
||
104 | * @param string $key |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getSourceLocations($domain, $key) |
||
121 | |||
122 | /** |
||
123 | * @param string $domain |
||
124 | * @param string $key |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | View Code Duplication | public function isNew($domain, $key) |
|
139 | |||
140 | /** |
||
141 | * @param string $domain |
||
142 | * @param string $key |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | View Code Duplication | public function isObsolete($domain, $key) |
|
157 | |||
158 | /** |
||
159 | * @param $domain |
||
160 | * @param $key |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | private function getNotes($domain, $key) |
||
176 | } |
||
177 |
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.