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 |
||
17 | class Str |
||
18 | { |
||
19 | /** |
||
20 | * Converts a given value to boolean. |
||
21 | * |
||
22 | * @param string $value |
||
23 | * @param bool $default |
||
24 | * @return bool |
||
25 | */ |
||
26 | 111 | public static function toBoolean($value, $default) |
|
38 | |||
39 | /** |
||
40 | * Return given size in bytes. |
||
41 | * Allowed units: |
||
42 | * B => byte |
||
43 | * K => kilo byte |
||
44 | * M => mega byte |
||
45 | * G => giga byte |
||
46 | * T => terra byte |
||
47 | * P => peta byte |
||
48 | * |
||
49 | * e.g. |
||
50 | * 1K => 1024 |
||
51 | * 2K => 2048 |
||
52 | * ... |
||
53 | * |
||
54 | * @param string $value |
||
55 | * @throws \RuntimeException |
||
56 | * @return int |
||
57 | */ |
||
58 | 11 | View Code Duplication | public static function toBytes($value) |
69 | |||
70 | /** |
||
71 | * Return time in seconds for a given value. |
||
72 | * Allowed units: |
||
73 | * S => second |
||
74 | * I => minute |
||
75 | * D => day |
||
76 | * W => week |
||
77 | * M => month |
||
78 | * Y => year |
||
79 | * |
||
80 | * e.g. |
||
81 | * 2I => 120 |
||
82 | * 10D => 864000 |
||
83 | * ... |
||
84 | * |
||
85 | * @param string $offset |
||
86 | * @throws \RuntimeException |
||
87 | * @return integer |
||
88 | */ |
||
89 | 9 | View Code Duplication | public static function toTime($offset) |
100 | |||
101 | /** |
||
102 | * Pads all given strings to given length. |
||
103 | * |
||
104 | * @param array $strings |
||
105 | * @param integer $length |
||
106 | * @param string $pad |
||
107 | * @param integer $mode |
||
108 | * @return array |
||
109 | */ |
||
110 | 1 | public static function padAll(array $strings, $length, $pad = ' ', $mode = STR_PAD_LEFT) |
|
118 | |||
119 | /** |
||
120 | * Explodes string to array but empty string results in empty array not array with empty string in it. |
||
121 | * |
||
122 | * @param string $separated |
||
123 | * @param string $separator |
||
124 | * @param boolean $trim |
||
125 | * @return array |
||
126 | */ |
||
127 | 65 | public static function toList($separated, $separator = ',', $trim = true) |
|
135 | |||
136 | /** |
||
137 | * Appends a plural "s" or "'s". |
||
138 | * |
||
139 | * @param string $subject |
||
140 | * @param int $amount |
||
141 | * @return string |
||
142 | */ |
||
143 | 6 | public static function appendPluralS($subject, $amount) |
|
147 | } |
||
148 |