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 | * Date placeholder replacement. |
||
21 | * Replaces %{somevalue} with date({somevalue}). |
||
22 | * |
||
23 | * @param string $string |
||
24 | * @param mixed <integer|null> $time |
||
25 | * @return string |
||
26 | */ |
||
27 | 44 | public static function replaceDatePlaceholders($string, $time = null) |
|
38 | |||
39 | /** |
||
40 | * Does a given string contain a date placeholder. |
||
41 | * |
||
42 | * @param string $string |
||
43 | * @return bool |
||
44 | */ |
||
45 | public static function isContainingPlaceholder($string) |
||
49 | 2 | ||
50 | 2 | /** |
|
51 | 2 | * Replaces %TARGET_DIR% and %TARGET_FILE% in given string. |
|
52 | * |
||
53 | * @param string $string |
||
54 | * @param string $target |
||
55 | * @return string |
||
56 | */ |
||
57 | public static function replaceTargetPlaceholders($string, $target) |
||
64 | |||
65 | /** |
||
66 | * Create a regex that matches the raw path considering possible date placeholders. |
||
67 | * |
||
68 | * @param string $stringWithDatePlaceholders |
||
69 | * @return string |
||
70 | */ |
||
71 | public static function datePlaceholdersToRegex($stringWithDatePlaceholders) |
||
76 | 14 | ||
77 | 71 | /** |
|
78 | 18 | * Converts a given value to boolean. |
|
79 | * |
||
80 | 67 | * @param string $value |
|
81 | * @param boolean $default |
||
82 | * @return boolean |
||
83 | */ |
||
84 | public static function toBoolean($value, $default) |
||
93 | |||
94 | /** |
||
95 | * Return given size in bytes. |
||
96 | * Allowed units: |
||
97 | * B => byte |
||
98 | * K => kilo byte |
||
99 | * M => mega byte |
||
100 | * G => giga byte |
||
101 | * T => terra byte |
||
102 | 9 | * P => peta byte |
|
103 | * |
||
104 | 9 | * e.g. |
|
105 | 1 | * 1K => 1024 |
|
106 | * 2K => 2048 |
||
107 | 8 | * ... |
|
108 | 8 | * |
|
109 | 8 | * @param string $value |
|
110 | * @throws \RuntimeException |
||
111 | 8 | * @return integer |
|
112 | */ |
||
113 | View Code Duplication | public static function toBytes($value) |
|
124 | |||
125 | /** |
||
126 | * Return time in seconds for a given value. |
||
127 | * Allowed units: |
||
128 | * S => second |
||
129 | * I => minute |
||
130 | * D => day |
||
131 | * W => week |
||
132 | * M => month |
||
133 | 8 | * Y => year |
|
134 | * |
||
135 | 8 | * e.g. |
|
136 | 3 | * 2I => 120 |
|
137 | * 10D => 864000 |
||
138 | 5 | * ... |
|
139 | 5 | * |
|
140 | 5 | * @param string $offset |
|
141 | * @throws \RuntimeException |
||
142 | 5 | * @return integer |
|
143 | */ |
||
144 | View Code Duplication | public static function toTime($offset) |
|
155 | |||
156 | 1 | /** |
|
157 | 1 | * Pads all given strings to given length. |
|
158 | 1 | * |
|
159 | 1 | * @param array $strings |
|
160 | 1 | * @param integer $length |
|
161 | * @param string $pad |
||
162 | * @param integer $mode |
||
163 | * @return array |
||
164 | */ |
||
165 | public static function padAll(array $strings, $length, $pad = ' ', $mode = STR_PAD_LEFT) |
||
173 | 25 | ||
174 | 25 | /** |
|
175 | 24 | * Explodes string to array but empty string results in empty array not array with empty string in it. |
|
176 | 24 | * |
|
177 | 25 | * @param string $separated |
|
178 | * @param string $separator |
||
179 | * @param boolean $trim |
||
180 | * @return array |
||
181 | */ |
||
182 | public static function toList($separated, $separator = ',', $trim = true) |
||
190 | |||
191 | /** |
||
192 | * Adds trailing slash to a string/path if not already there. |
||
193 | * |
||
194 | * @param string $string |
||
195 | * @return string |
||
196 | */ |
||
197 | 3 | public static function withTrailingSlash($string) |
|
201 | |||
202 | /** |
||
203 | * Removes the trailing slash from a string/path. |
||
204 | * |
||
205 | * @param string $string |
||
206 | * @return string |
||
207 | */ |
||
208 | 2 | public static function withoutTrailingSlash($string) |
|
212 | |||
213 | /** |
||
214 | * Adds leading slash to a string/path if not already there. |
||
215 | * |
||
216 | * @param string $string |
||
217 | * @return string |
||
218 | */ |
||
219 | 1 | public static function withLeadingSlash($string) |
|
223 | |||
224 | /** |
||
225 | * Removes the leading slash from a string/path. |
||
226 | * |
||
227 | * @param string $string |
||
228 | * @return string |
||
229 | */ |
||
230 | public static function withoutLeadingSlash($string) |
||
234 | |||
235 | /** |
||
236 | * Appends a plural "s" or "'s". |
||
237 | * |
||
238 | * @param string $subject |
||
239 | * @param integer $amount |
||
240 | * @return string |
||
241 | */ |
||
242 | public static function appendPluralS($subject, $amount) |
||
246 | } |
||
247 |