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 |
||
26 | class CacheFile |
||
27 | { |
||
28 | /** |
||
29 | * @var string|null |
||
30 | */ |
||
31 | private $path; |
||
32 | |||
33 | /** |
||
34 | * @var ModuleFile[] |
||
35 | */ |
||
36 | private $moduleFiles = array(); |
||
37 | |||
38 | /** |
||
39 | * @var InstallInfo[] |
||
40 | */ |
||
41 | private $installInfos = array(); |
||
42 | |||
43 | /** |
||
44 | * Creates new CacheFile. |
||
45 | * |
||
46 | * @param string|null $path The path where the cache file is stored. |
||
47 | */ |
||
48 | 41 | public function __construct($path = null) |
|
54 | |||
55 | /** |
||
56 | * Returns the path to the cache file. |
||
57 | * |
||
58 | * @return string|null The path or `null` if this file is not stored on the |
||
59 | * file system. |
||
60 | */ |
||
61 | 8 | public function getPath() |
|
65 | |||
66 | /** |
||
67 | * Sets the Module files. |
||
68 | * |
||
69 | * @param ModuleFile[] $moduleFiles The Module files. |
||
70 | */ |
||
71 | 2 | public function setModuleFiles(array $moduleFiles) |
|
79 | |||
80 | /** |
||
81 | * Adds a Module to the cache file. |
||
82 | * |
||
83 | * @param ModuleFile $moduleFile The added Module file. |
||
84 | */ |
||
85 | 23 | public function addModuleFile(ModuleFile $moduleFile) |
|
91 | |||
92 | /** |
||
93 | * Removes a Module file from the cache file. |
||
94 | * |
||
95 | * @param string $moduleName The Module name. |
||
96 | */ |
||
97 | 1 | public function removeModuleFile($moduleName) |
|
101 | |||
102 | /** |
||
103 | * Removes all Module files from the cache file. |
||
104 | */ |
||
105 | 1 | public function clearModuleFiles() |
|
109 | |||
110 | /** |
||
111 | * Returns the Module file with the given name. |
||
112 | * |
||
113 | * @param string $moduleName The Module name. |
||
114 | * |
||
115 | * @return ModuleFile The Module with the passed name. |
||
116 | * |
||
117 | * @throws InvalidArgumentException If the Module was not found. |
||
118 | */ |
||
119 | 2 | View Code Duplication | public function getModuleFile($moduleName) |
129 | |||
130 | /** |
||
131 | * Returns the Module files in the cache file. |
||
132 | * |
||
133 | * @return ModuleFile[] The Module files in the cache file. |
||
134 | */ |
||
135 | 12 | public function getModuleFiles() |
|
139 | |||
140 | /** |
||
141 | * Returns whether a Module with the given name exists. |
||
142 | * |
||
143 | * @param string $moduleName The Module name. |
||
144 | * |
||
145 | * @return bool Whether a Module with this name exists. |
||
146 | */ |
||
147 | 5 | public function hasModuleFile($moduleName) |
|
151 | |||
152 | /** |
||
153 | * Returns whether a cache file contains any Module files. |
||
154 | * |
||
155 | * @return bool Whether a cache file contains any Module files. |
||
156 | */ |
||
157 | 4 | public function hasModuleFiles() |
|
161 | |||
162 | /** |
||
163 | * Adds install info to the cache file. |
||
164 | * |
||
165 | * @param InstallInfo $installInfo The Module install info. |
||
166 | */ |
||
167 | 22 | public function addInstallInfo(InstallInfo $installInfo) |
|
173 | |||
174 | /** |
||
175 | * Removes install info file from the cache file. |
||
176 | * |
||
177 | * @param string $moduleName The Module name. |
||
178 | */ |
||
179 | 1 | public function removeInstallInfo($moduleName) |
|
183 | |||
184 | /** |
||
185 | * Removes all Module install info from the cache file. |
||
186 | */ |
||
187 | 3 | public function clearInstallInfo() |
|
191 | |||
192 | /** |
||
193 | * Returns the install info with the given Module name. |
||
194 | * |
||
195 | * @param string $moduleName The Module name. |
||
196 | * |
||
197 | * @return InstallInfo The Module install info with the passed name. |
||
198 | * |
||
199 | * @throws InvalidArgumentException If the install info was not found. |
||
200 | */ |
||
201 | 5 | View Code Duplication | public function getInstallInfo($moduleName) |
211 | |||
212 | /** |
||
213 | * Returns the install info for all Modules in the cache file. |
||
214 | * |
||
215 | * @return InstallInfo[] The install info for all Modules in the cache file. |
||
216 | */ |
||
217 | 5 | public function getInstallInfos() |
|
221 | |||
222 | /** |
||
223 | * Returns whether install info for a Module with the given name exists. |
||
224 | * |
||
225 | * @param string $moduleName The Module name. |
||
226 | * |
||
227 | * @return bool Whether install info for a Module with this name exists. |
||
228 | */ |
||
229 | 4 | public function hasInstallInfo($moduleName) |
|
233 | |||
234 | /** |
||
235 | * Returns whether a cache file contains any Module install info. |
||
236 | * |
||
237 | * @return bool Whether a cache file contains any Module install info. |
||
238 | */ |
||
239 | 3 | public function hasInstallInfos() |
|
243 | } |
||
244 |
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.