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 Package 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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class Package |
||
34 | { |
||
35 | private $config; |
||
36 | private $filepath; |
||
37 | private $filepathNoExt; |
||
38 | private $filename; |
||
39 | private $filenameNoExt; |
||
40 | private $metafile; |
||
41 | private $wizfile; |
||
42 | private $nowizfile; |
||
43 | private $metadata; |
||
44 | |||
45 | /** |
||
46 | * @param \SSpkS\Config $config Config object |
||
47 | * @param string $filename Filename of SPK file |
||
48 | */ |
||
49 | 17 | public function __construct(\SSpkS\Config $config, $filename) |
|
66 | |||
67 | /** |
||
68 | * Getter magic method. |
||
69 | * |
||
70 | * @param string $name Name of requested value. |
||
71 | * @return mixed Requested value. |
||
72 | */ |
||
73 | 10 | public function __get($name) |
|
78 | |||
79 | /** |
||
80 | * Setter magic method. |
||
81 | * |
||
82 | * @param string $name Name of variable to set. |
||
83 | * @param mixed $value Value to set. |
||
84 | */ |
||
85 | 5 | public function __set($name, $value) |
|
90 | |||
91 | /** |
||
92 | * Isset feature magic method. |
||
93 | * |
||
94 | * @param string $name Name of requested value. |
||
95 | * @return bool TRUE if value exists, FALSE otherwise. |
||
96 | */ |
||
97 | 6 | public function __isset($name) |
|
102 | |||
103 | /** |
||
104 | * Unset feature magic method. |
||
105 | * |
||
106 | * @param string $name Name of value to unset. |
||
107 | */ |
||
108 | 1 | public function __unset($name) |
|
113 | |||
114 | /** |
||
115 | * Parses boolean value ('yes', '1', 'true') into |
||
116 | * boolean type. |
||
117 | * |
||
118 | * @param mixed $value Input value |
||
119 | * @return bool Boolean interpretation of $value. |
||
120 | */ |
||
121 | 8 | public function parseBool($value) |
|
125 | |||
126 | /** |
||
127 | * Checks if given property $prop exists and converts it |
||
128 | * into a boolean value. |
||
129 | * |
||
130 | * @param string $prop Property to convert |
||
131 | */ |
||
132 | 12 | private function fixBoolIfExist($prop) |
|
138 | |||
139 | /** |
||
140 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
141 | */ |
||
142 | 12 | private function collectMetadata() |
|
175 | |||
176 | /** |
||
177 | * Returns metadata for this package. |
||
178 | * |
||
179 | * @return array Metadata. |
||
180 | */ |
||
181 | 3 | public function getMetadata() |
|
186 | |||
187 | /** |
||
188 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
189 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
190 | * |
||
191 | * @param string $inPkgName Filename in package |
||
192 | * @param string $targetFile Path to destination |
||
193 | * @throws \Exception if the file couldn't get extracted. |
||
194 | * @return bool TRUE if successful or no action needed. |
||
195 | */ |
||
196 | 13 | public function extractIfMissing($inPkgName, $targetFile) |
|
197 | { |
||
198 | 13 | if (file_exists($targetFile)) { |
|
199 | // Everything in working order |
||
200 | 8 | return true; |
|
201 | } |
||
202 | // Try to extract file |
||
203 | 13 | $tmp_dir = sys_get_temp_dir(); |
|
204 | 13 | $free_tmp = @disk_free_space($tmp_dir); |
|
205 | 13 | View Code Duplication | if (!empty($free_tmp) && $free_tmp < 2048) { |
|
|||
206 | throw new \Exception('TMP folder only has ' . $free_tmp . ' Bytes space available. Disk full!'); |
||
207 | } |
||
208 | 13 | $free = @disk_free_space(dirname($targetFile)); |
|
209 | 13 | View Code Duplication | if (!empty($free) && $free < 2048) { |
210 | throw new \Exception('Package folder only has ' . $free . ' Bytes space available. Disk full!'); |
||
211 | } |
||
212 | try { |
||
213 | 13 | $p = new \PharData($this->filepath, \Phar::CURRENT_AS_FILEINFO | \Phar::KEY_AS_FILENAME); |
|
214 | 13 | } catch (\UnexpectedValueException $e) { |
|
215 | rename($this->filepath, $this->filepath . '.invalid'); |
||
216 | throw new \Exception('Package ' . $this->filepath . ' not readable! Will be ignored in the future. Please try again!'); |
||
217 | } |
||
218 | 13 | $tmpExtractedFilepath = $tmp_dir . DIRECTORY_SEPARATOR . $inPkgName; |
|
219 | 13 | if (file_exists($tmpExtractedFilepath)) { |
|
220 | // stale file from before - unlink first |
||
221 | unlink($tmpExtractedFilepath); |
||
222 | } |
||
223 | 13 | $p->extractTo($tmp_dir, $inPkgName); |
|
224 | 13 | rename($tmpExtractedFilepath, $targetFile); |
|
225 | 13 | return true; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Returns a true if the package contains WIZARD_UIFILES. |
||
230 | * |
||
231 | * @return bool Package has a wizard |
||
232 | */ |
||
233 | 12 | public function hasWizardDir() |
|
258 | |||
259 | /** |
||
260 | * Returns a list of thumbnails for the specified package. |
||
261 | * |
||
262 | * @param string $pathPrefix Prefix to put before file path |
||
263 | * @return array List of thumbnail urls |
||
264 | */ |
||
265 | 12 | public function getThumbnails($pathPrefix = '') |
|
302 | |||
303 | /** |
||
304 | * Returns a list of screenshots for the specified package. |
||
305 | * |
||
306 | * @param string $pathPrefix Prefix to put before file path |
||
307 | * @return array List of screenshots |
||
308 | */ |
||
309 | 12 | public function getSnapshots($pathPrefix = '') |
|
328 | |||
329 | /** |
||
330 | * Checks compatibility to the given $arch-itecture. |
||
331 | * |
||
332 | * @param string $arch Architecture to check against (or "noarch") |
||
333 | * @return bool TRUE if compatible, otherwise FALSE. |
||
334 | */ |
||
335 | 1 | public function isCompatibleToArch($arch) |
|
342 | |||
343 | /** |
||
344 | * Checks compatibility to the given firmware $version. |
||
345 | * |
||
346 | * @param string $version Target firmware version. |
||
347 | * @return bool TRUE if compatible, otherwise FALSE. |
||
348 | */ |
||
349 | 1 | public function isCompatibleToFirmware($version) |
|
354 | |||
355 | /** |
||
356 | * Checks if this package is a beta version or not. |
||
357 | * |
||
358 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
359 | */ |
||
360 | 1 | public function isBeta() |
|
365 | } |
||
366 |
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.