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 |
||
33 | class LessHelper extends AppHelper |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * List of helpers used by this helper. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $helpers = [ |
||
42 | 'Url' => ['className' => 'Core.Url'], |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Constructor hook method. |
||
47 | * |
||
48 | * @param array $config |
||
49 | */ |
||
50 | public function initialize(array $config) |
||
64 | |||
65 | /** |
||
66 | * Process less file. |
||
67 | * |
||
68 | * @param string $source |
||
69 | * @param bool $force |
||
70 | * @return string|null |
||
71 | * @throws \JBZoo\Less\Exception |
||
72 | */ |
||
73 | public function process($source, $force = true) |
||
100 | |||
101 | /** |
||
102 | * CSS compressing. |
||
103 | * |
||
104 | * @param string $code |
||
105 | * @param string $cacheId |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function _compress($code, $cacheId) |
||
130 | |||
131 | /** |
||
132 | * Find source webroot dir. |
||
133 | * |
||
134 | * @param string $source |
||
135 | * @return array |
||
136 | */ |
||
137 | protected function _findWebRoot($source) |
||
150 | |||
151 | /** |
||
152 | * Get asset url by source. |
||
153 | * |
||
154 | * @param string $source (Example: TestPlugin.path/to/image.png) |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function _getAssetUrl($source) |
||
161 | |||
162 | /** |
||
163 | * Get plugin asset url. |
||
164 | * |
||
165 | * @param string $path |
||
166 | * @return array |
||
167 | */ |
||
168 | protected function _getPlgAssetUrl($path) |
||
190 | |||
191 | /** |
||
192 | * Normalize style file. |
||
193 | * |
||
194 | * @param string $path |
||
195 | * @param string $fileHead |
||
196 | * @return mixed |
||
197 | */ |
||
198 | protected function _normalizeContent($path, $fileHead) |
||
214 | |||
215 | /** |
||
216 | * Normalize plugin asset url. |
||
217 | * |
||
218 | * @param string $path |
||
219 | * @return string |
||
220 | */ |
||
221 | protected function _normalizePlgAssetUrl($path) |
||
234 | |||
235 | /** |
||
236 | * Preg replace url. |
||
237 | * |
||
238 | * @param string $css |
||
239 | * @return mixed |
||
240 | */ |
||
241 | protected function _replaceUrl($css) |
||
246 | |||
247 | /** |
||
248 | * Replace url callback. |
||
249 | * |
||
250 | * @param array $match |
||
251 | * @return string |
||
252 | */ |
||
253 | protected function _replaceUrlCallback(array $match) |
||
274 | |||
275 | /** |
||
276 | * Set less process force. |
||
277 | * |
||
278 | * @param bool $force |
||
279 | * @return void |
||
280 | */ |
||
281 | protected function _setForce($force = false) |
||
287 | |||
288 | /** |
||
289 | * Write content in to the file. |
||
290 | * |
||
291 | * @param string $path |
||
292 | * @param string $content |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function _write($path, $content) |
||
301 | } |
||
302 |
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.