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 |
||
15 | class Helper { |
||
16 | |||
17 | const APP_ID = 'documents'; |
||
18 | |||
19 | 1 | public static function getNewFileName($view, $path, $prepend = ' '){ |
|
20 | 1 | $fileNum = 1; |
|
21 | |||
22 | 1 | while ($view->file_exists($path)){ |
|
23 | $fileNum += 1; |
||
24 | $path = preg_replace('/(\.odt|' . $prepend . '\(\d+\)\.odt)$/', $prepend . '(' . $fileNum . ').odt', $path); |
||
25 | }; |
||
26 | |||
27 | 1 | return $path; |
|
28 | } |
||
29 | |||
30 | public static function getArrayValueByKey($array, $key, $default=''){ |
||
31 | if (array_key_exists($key, $array)){ |
||
32 | return $array[$key]; |
||
33 | } |
||
34 | return $default; |
||
35 | } |
||
36 | |||
37 | public static function isVersionsEnabled(){ |
||
38 | return \OCP\App::isEnabled('files_versions'); |
||
39 | } |
||
40 | |||
41 | public static function getRandomColor(){ |
||
42 | $str = dechex(floor(rand(0, 16777215))); |
||
43 | return '#' . str_pad($str, 6, "0", STR_PAD_LEFT); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $name |
||
48 | * @return string |
||
49 | */ |
||
50 | public static function getMemberColor($name){ |
||
51 | $hash = md5($name); |
||
52 | $maxRange = hexdec('ffffffffffffffffffffffffffffffff'); |
||
53 | $hue = hexdec($hash) / $maxRange * 256; |
||
54 | return '#' . self::convertHSLToRGB($hue, 90, 60); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param integer $iH |
||
59 | * @param integer $iS |
||
60 | * @param integer $iV |
||
61 | * @return string |
||
62 | */ |
||
63 | protected static function convertHSLToRGB($iH, $iS, $iV){ |
||
144 | |||
145 | public static function findOpenOffice(){ |
||
146 | $config = \OC::$server->getConfig(); |
||
147 | $cmd = ''; |
||
148 | if (is_string($config->getSystemValue('preview_libreoffice_path', null))){ |
||
149 | $cmd = $config->getSystemValue('preview_libreoffice_path', null); |
||
178 | |||
179 | } |
||
180 |
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.