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 PackageFile[] |
||
35 | */ |
||
36 | private $packageFiles = 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 | 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 | public function getPath() |
||
65 | |||
66 | /** |
||
67 | * Sets the package files. |
||
68 | * |
||
69 | * @param PackageFile[] $packageFiles The package files. |
||
70 | */ |
||
71 | public function setPackageFiles(array $packageFiles) |
||
75 | |||
76 | /** |
||
77 | * Adds a package to the cache file. |
||
78 | * |
||
79 | * @param PackageFile $packageFile The added package file. |
||
80 | */ |
||
81 | public function addPackageFile(PackageFile $packageFile) |
||
87 | |||
88 | /** |
||
89 | * Removes a package file from the cache file. |
||
90 | * |
||
91 | * @param string $packageName The package name. |
||
92 | */ |
||
93 | public function removePackageFile($packageName) |
||
97 | |||
98 | /** |
||
99 | * Removes all package files from the cache file. |
||
100 | */ |
||
101 | public function clearPackageFiles() |
||
105 | |||
106 | /** |
||
107 | * Returns the package file with the given name. |
||
108 | * |
||
109 | * @param string $packageName The package name. |
||
110 | * |
||
111 | * @return PackageFile The package with the passed name. |
||
112 | * |
||
113 | * @throws InvalidArgumentException If the package was not found. |
||
114 | */ |
||
115 | View Code Duplication | public function getPackageFile($packageName) |
|
125 | |||
126 | /** |
||
127 | * Returns the package files in the cache file. |
||
128 | * |
||
129 | * @return PackageFile[] The package files in the cache file. |
||
130 | */ |
||
131 | public function getPackageFiles() |
||
135 | |||
136 | /** |
||
137 | * Returns whether a package with the given name exists. |
||
138 | * |
||
139 | * @param string $packageName The package name. |
||
140 | * |
||
141 | * @return bool Whether a package with this name exists. |
||
142 | */ |
||
143 | public function hasPackageFile($packageName) |
||
147 | |||
148 | /** |
||
149 | * Returns whether a cache file contains any package files. |
||
150 | * |
||
151 | * @return bool Whether a cache file contains any package files. |
||
152 | */ |
||
153 | public function hasPackageFiles() |
||
157 | |||
158 | /** |
||
159 | * Adds install info to the cache file. |
||
160 | * |
||
161 | * @param InstallInfo $installInfo The package install info. |
||
162 | */ |
||
163 | public function addInstallInfo(InstallInfo $installInfo) |
||
169 | |||
170 | /** |
||
171 | * Removes install info file from the cache file. |
||
172 | * |
||
173 | * @param string $packageName The package name. |
||
174 | */ |
||
175 | public function removeInstallInfo($packageName) |
||
179 | |||
180 | /** |
||
181 | * Removes all package install info from the cache file. |
||
182 | */ |
||
183 | public function clearInstallInfo() |
||
187 | |||
188 | /** |
||
189 | * Returns the install info with the given package name. |
||
190 | * |
||
191 | * @param string $packageName The package name. |
||
192 | * |
||
193 | * @return InstallInfo The package install info with the passed name. |
||
194 | * |
||
195 | * @throws InvalidArgumentException If the install info was not found. |
||
196 | */ |
||
197 | View Code Duplication | public function getInstallInfo($packageName) |
|
207 | |||
208 | /** |
||
209 | * Returns the install info for all packages in the cache file. |
||
210 | * |
||
211 | * @return InstallInfo[] The install info for all packages in the cache file. |
||
212 | */ |
||
213 | public function getInstallInfos() |
||
217 | |||
218 | /** |
||
219 | * Returns whether install info for a package with the given name exists. |
||
220 | * |
||
221 | * @param string $packageName The package name. |
||
222 | * |
||
223 | * @return bool Whether install info for a package with this name exists. |
||
224 | */ |
||
225 | public function hasInstallInfo($packageName) |
||
229 | |||
230 | /** |
||
231 | * Returns whether a cache file contains any package install info. |
||
232 | * |
||
233 | * @return bool Whether a cache file contains any package install info. |
||
234 | */ |
||
235 | public function hasInstallInfos() |
||
239 | } |
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.