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 |
||
30 | class Package |
||
31 | { |
||
32 | private $filepath; |
||
33 | private $filepathNoExt; |
||
34 | private $filename; |
||
35 | private $filenameNoExt; |
||
36 | private $metafile; |
||
37 | private $metadata; |
||
38 | |||
39 | /** |
||
40 | * @param string $filename Filename of SPK file |
||
41 | */ |
||
42 | 17 | public function __construct($filename) |
|
56 | |||
57 | /** |
||
58 | * Getter magic method. |
||
59 | * |
||
60 | * @param string $name Name of requested value. |
||
61 | * @return mixed Requested value. |
||
62 | */ |
||
63 | 10 | public function __get($name) |
|
68 | |||
69 | /** |
||
70 | * Setter magic method. |
||
71 | * |
||
72 | * @param string $name Name of variable to set. |
||
73 | * @param mixed $value Value to set. |
||
74 | */ |
||
75 | 5 | public function __set($name, $value) |
|
80 | |||
81 | /** |
||
82 | * Isset feature magic method. |
||
83 | * |
||
84 | * @param string $name Name of requested value. |
||
85 | * @return bool TRUE if value exists, FALSE otherwise. |
||
86 | */ |
||
87 | 6 | public function __isset($name) |
|
92 | |||
93 | /** |
||
94 | * Unset feature magic method. |
||
95 | * |
||
96 | * @param string $name Name of value to unset. |
||
97 | */ |
||
98 | 1 | public function __unset($name) |
|
103 | |||
104 | /** |
||
105 | * Parses boolean value ('yes', '1', 'true') into |
||
106 | * boolean type. |
||
107 | * |
||
108 | * @param mixed $value Input value |
||
109 | * @return bool Boolean interpretation of $value. |
||
110 | */ |
||
111 | 8 | public function parseBool($value) |
|
115 | |||
116 | /** |
||
117 | * Checks if given property $prop exists and converts it |
||
118 | * into a boolean value. |
||
119 | * |
||
120 | * @param string $prop Property to convert |
||
121 | */ |
||
122 | 12 | private function fixBoolIfExist($prop) |
|
128 | |||
129 | /** |
||
130 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
131 | */ |
||
132 | 12 | private function collectMetadata() |
|
133 | { |
||
134 | 12 | if (!is_null($this->metadata)) { |
|
135 | // metadata already collected |
||
136 | 12 | return; |
|
137 | } |
||
138 | 12 | $this->extractIfMissing('INFO', $this->metafile); |
|
139 | 12 | $this->metadata = parse_ini_file($this->metafile); |
|
140 | 12 | if (!isset($this->metadata['displayname'])) { |
|
141 | 12 | $this->metadata['displayname'] = $this->metadata['package']; |
|
142 | 12 | } |
|
143 | 12 | $this->metadata['spk'] = $this->filepath; |
|
144 | |||
145 | // Convert architecture(s) to array, as multiple architectures can be specified |
||
146 | 12 | $this->metadata['arch'] = explode(' ', $this->metadata['arch']); |
|
147 | |||
148 | 12 | $this->fixBoolIfExist('silent_install'); |
|
149 | 12 | $this->fixBoolIfExist('silent_uninstall'); |
|
150 | 12 | $this->fixBoolIfExist('silent_upgrade'); |
|
151 | |||
152 | 12 | if (isset($this->metadata['beta']) && in_array($this->metadata['beta'], array('true', '1', 'beta'))) { |
|
153 | 4 | $this->metadata['beta'] = true; |
|
154 | 4 | } else { |
|
155 | 12 | $this->metadata['beta'] = false; |
|
156 | } |
||
157 | |||
158 | 12 | $this->metadata['thumbnail'] = $this->getThumbnails(); |
|
159 | 12 | $this->metadata['snapshot'] = $this->getSnapshots(); |
|
160 | 12 | } |
|
161 | |||
162 | /** |
||
163 | * Returns metadata for this package. |
||
164 | * |
||
165 | * @return array Metadata. |
||
166 | */ |
||
167 | 3 | public function getMetadata() |
|
172 | |||
173 | /** |
||
174 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
175 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
176 | * |
||
177 | * @param string $inPkgName Filename in package |
||
178 | * @param string $targetFile Path to destination |
||
179 | * @throws \Exception if the file couldn't get extracted. |
||
180 | * @return bool TRUE if successful or no action needed. |
||
181 | */ |
||
182 | 13 | public function extractIfMissing($inPkgName, $targetFile) |
|
208 | |||
209 | /** |
||
210 | * Returns a list of thumbnails for the specified package. |
||
211 | * |
||
212 | * @param string $pathPrefix Prefix to put before file path |
||
213 | * @return array List of thumbnail urls |
||
214 | */ |
||
215 | 12 | public function getThumbnails($pathPrefix = '') |
|
248 | |||
249 | /** |
||
250 | * Returns a list of screenshots for the specified package. |
||
251 | * |
||
252 | * @param string $pathPrefix Prefix to put before file path |
||
253 | * @return array List of screenshots |
||
254 | */ |
||
255 | 12 | public function getSnapshots($pathPrefix = '') |
|
274 | |||
275 | /** |
||
276 | * Checks compatibility to the given $arch-itecture. |
||
277 | * |
||
278 | * @param string $arch Architecture to check against (or "noarch") |
||
279 | * @return bool TRUE if compatible, otherwise FALSE. |
||
280 | */ |
||
281 | 1 | public function isCompatibleToArch($arch) |
|
287 | |||
288 | /** |
||
289 | * Checks compatibility to the given firmware $version. |
||
290 | * |
||
291 | * @param string $version Target firmware version. |
||
292 | * @return bool TRUE if compatible, otherwise FALSE. |
||
293 | */ |
||
294 | 1 | public function isCompatibleToFirmware($version) |
|
299 | |||
300 | /** |
||
301 | * Checks if this package is a beta version or not. |
||
302 | * |
||
303 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
304 | */ |
||
305 | 1 | public function isBeta() |
|
310 | } |
||
311 |
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.