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 |
||
28 | class Utils { |
||
29 | /** |
||
30 | * Gets the unix epoch UTC timestamp |
||
31 | * @return int |
||
32 | */ |
||
33 | public static function getTime() { |
||
36 | /** |
||
37 | * @return int the current unix time in milliseconds |
||
38 | */ |
||
39 | public static function getMicroTime() { |
||
42 | |||
43 | /** |
||
44 | * @param $uid |
||
45 | * @return string |
||
46 | */ |
||
47 | public static function getNameByUid($uid){ |
||
52 | |||
53 | /** |
||
54 | * Splits a string in parts of 5Mb |
||
55 | * @param $str |
||
56 | * @return array |
||
57 | */ |
||
58 | public function splitContent($str) { |
||
59 | $maxlength = 2621440; // 5 Megs (2 bytes per character) |
||
60 | $count = 0; |
||
61 | $strarray = array(); |
||
62 | while (true) { |
||
63 | if (strlen($str) <= $maxlength) { |
||
64 | $strarray[$count++] = $str; |
||
65 | return $strarray; |
||
66 | } else { |
||
67 | $strarray[$count++] = substr($str, 0, $maxlength); |
||
68 | $str = substr($str, $maxlength); |
||
69 | } |
||
70 | } |
||
71 | return $strarray; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $filetime \DateTime |
||
76 | * @param $now \DateTime |
||
77 | * @return mixed|string |
||
78 | */ |
||
79 | public function getTimeString($filetime, $now) { |
||
80 | $l = \OCP\Util::getL10N('ownnote'); |
||
81 | $difftime = $filetime->diff($now); |
||
82 | $years = $difftime->y; |
||
83 | $months = $difftime->m; |
||
84 | $days = $difftime->d; |
||
85 | $hours = $difftime->h; |
||
86 | $minutes = $difftime->i; |
||
87 | $seconds = $difftime->s; |
||
88 | $timestring = ""; |
||
89 | View Code Duplication | if ($timestring == "" && $years == 1) $timestring = str_replace('#', $years, $l->t("# year ago")); |
|
|
|||
90 | View Code Duplication | if ($timestring == "" && $years > 0) $timestring = str_replace('#', $years, $l->t("# years ago")); |
|
91 | View Code Duplication | if ($timestring == "" && $months == 1) $timestring = str_replace('#', $months, $l->t("# month ago")); |
|
92 | View Code Duplication | if ($timestring == "" && $months > 0) $timestring = str_replace('#', $months, $l->t("# months ago")); |
|
93 | View Code Duplication | if ($timestring == "" && $days == 1) $timestring = str_replace('#', $days, $l->t("# day ago")); |
|
94 | View Code Duplication | if ($timestring == "" && $days > 0) $timestring = str_replace('#', $days, $l->t("# days ago")); |
|
95 | View Code Duplication | if ($timestring == "" && $hours == 1) $timestring = str_replace('#', $hours, $l->t("# hour ago")); |
|
96 | View Code Duplication | if ($timestring == "" && $hours > 0) $timestring = str_replace('#', $hours, $l->t("# hours ago")); |
|
97 | View Code Duplication | if ($timestring == "" && $minutes == 1) $timestring = str_replace('#', $minutes, $l->t("# minute ago")); |
|
98 | View Code Duplication | if ($timestring == "" && $minutes > 0) $timestring = str_replace('#', $minutes, $l->t("# minutes ago")); |
|
99 | View Code Duplication | if ($timestring == "" && $seconds == 1) $timestring = str_replace('#', $seconds, $l->t("# second ago")); |
|
100 | View Code Duplication | if ($timestring == "" && $seconds > 0) $timestring = str_replace('#', $seconds, $l->t("# seconds ago")); |
|
101 | return $timestring; |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @param $haystack |
||
107 | * @param $needle |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function startsWith($haystack, $needle) { |
||
113 | |||
114 | /** |
||
115 | * @param $string |
||
116 | * @param $test |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function endsWith($string, $test) { |
||
125 | } |
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.