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 namespace Gears\String\Methods; |
||
16 | trait To |
||
17 | { |
||
18 | /** |
||
19 | * Explicitly turn Str back into a scalar string. |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | public function toString() |
||
27 | |||
28 | /** |
||
29 | * Converts the string to an array of characters. |
||
30 | * |
||
31 | * Each character is an instance of Str. |
||
32 | * |
||
33 | * @return static[] |
||
34 | */ |
||
35 | public function toArray() |
||
39 | |||
40 | /** |
||
41 | * Converts all characters in the string to lowercase. |
||
42 | * |
||
43 | * @return static |
||
44 | */ |
||
45 | public function toLowerCase() |
||
52 | |||
53 | /** |
||
54 | * Converts all characters in the string to lowercase. |
||
55 | * |
||
56 | * @return static |
||
57 | */ |
||
58 | public function toUpperCase() |
||
65 | |||
66 | /** |
||
67 | * Returns the singular version of the word. |
||
68 | * |
||
69 | * @param string $language The language for the inflector. |
||
70 | * |
||
71 | * @return static |
||
72 | */ |
||
73 | View Code Duplication | public function toSingular($language = 'en') |
|
90 | |||
91 | /** |
||
92 | * Returns the plural version of the word. |
||
93 | * |
||
94 | * @param string $language The language for the inflector. |
||
95 | * |
||
96 | * @return static |
||
97 | */ |
||
98 | View Code Duplication | public function toPlural($language = 'en') |
|
115 | |||
116 | /** |
||
117 | * Returns an ASCII version of the string. |
||
118 | * |
||
119 | * A set of non-ASCII characters are replaced with their closest ASCII |
||
120 | * counterparts, and the rest are removed unless instructed otherwise. |
||
121 | * |
||
122 | * @return static |
||
123 | */ |
||
124 | public function toAscii() |
||
131 | |||
132 | /** |
||
133 | * Returns a boolean representation of the given logical string value. |
||
134 | * |
||
135 | * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
||
136 | * 'off', and 'no' will return false. In all instances, case is ignored. |
||
137 | * For other numeric strings, their sign will determine the return value. |
||
138 | * In addition, blank strings consisting of only whitespace will return |
||
139 | * false. For all other strings, the return value is a result of a |
||
140 | * boolean cast. |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function toBoolean() |
||
173 | |||
174 | /** |
||
175 | * Converts tabs to spaces. |
||
176 | * |
||
177 | * Each tab in the string is replaced with some number of spaces, |
||
178 | * as defined by $tabLength. By default, each tab is converted to |
||
179 | * 4 consecutive spaces. |
||
180 | * |
||
181 | * @param int $tabLength Number of spaces to replace each tab with. |
||
182 | * |
||
183 | * @return static |
||
184 | */ |
||
185 | public function toSpaces($tabLength = 4) |
||
197 | |||
198 | /** |
||
199 | * Converts spaces to tabs. |
||
200 | * |
||
201 | * Replaces each occurrence of some consecutive number of spaces, |
||
202 | * as defined by $tabLength, to a tab. By default, each 4 consecutive |
||
203 | * spaces are converted to a tab. |
||
204 | * |
||
205 | * @param int $tabLength Number of spaces to replace with a tab. |
||
206 | * |
||
207 | * @return static |
||
208 | */ |
||
209 | public function toTabs($tabLength = 4) |
||
221 | |||
222 | /** |
||
223 | * Returns a lowercase and trimmed string separated by dashes. |
||
224 | * |
||
225 | * Dashes are inserted before uppercase characters (with the exception of |
||
226 | * the first character of the string), and in place of spaces as well as |
||
227 | * underscores. |
||
228 | * |
||
229 | * @return static |
||
230 | */ |
||
231 | public function toDashed() |
||
235 | |||
236 | /** |
||
237 | * Returns a lowercase and trimmed string separated by underscores. |
||
238 | * |
||
239 | * Underscores are inserted before uppercase characters (with the exception |
||
240 | * of the first character of the string), and in place of spaces as well as |
||
241 | * dashes. |
||
242 | * |
||
243 | * @return static |
||
244 | */ |
||
245 | public function toUnderScored() |
||
249 | |||
250 | /** |
||
251 | * Returns a camelCase version of the string. |
||
252 | * |
||
253 | * Trims surrounding spaces, capitalizes letters following digits, spaces, |
||
254 | * dashes & underscores and removes spaces & dashes as well as underscores. |
||
255 | * |
||
256 | * @param bool $upperFirst If true, the first char will be UPPERCASE. |
||
257 | * |
||
258 | * @return static |
||
259 | */ |
||
260 | public function toCamelCase($upperFirst = false) |
||
299 | |||
300 | /** |
||
301 | * Convert a string to e.g.: "snake_case" |
||
302 | * |
||
303 | * @return static |
||
304 | */ |
||
305 | public function toSnakeCase() |
||
354 | |||
355 | /** |
||
356 | * Returns a trimmed string with the first letter of each word capitalized. |
||
357 | * |
||
358 | * Also accepts an array, $ignore, allowing you to |
||
359 | * list words not to be capitalized. |
||
360 | * |
||
361 | * @param array|null $ignore An array of words not to capitalize |
||
362 | * |
||
363 | * @return static |
||
364 | */ |
||
365 | public function toTitleCase($ignore = null) |
||
385 | |||
386 | /** |
||
387 | * Returns a trimmed string with the first letter capitalized. |
||
388 | * |
||
389 | * TODO: Be smarter and capitalise after every period. |
||
390 | * |
||
391 | * @return static |
||
392 | */ |
||
393 | public function toSentenceCase() |
||
397 | |||
398 | /** |
||
399 | * Converts the string into an URL slug. |
||
400 | * |
||
401 | * This includes replacing non-ASCII characters with their closest ASCII |
||
402 | * equivalents, removing remaining non-ASCII and non-alphanumeric |
||
403 | * characters, and replacing whitespace with $replacement. |
||
404 | * |
||
405 | * The replacement defaults to a single dash |
||
406 | * and the string is also converted to lowercase. |
||
407 | * |
||
408 | * @param string $replacement The string used to replace whitespace |
||
409 | * @param string $language The language for the url |
||
410 | * @param bool $strToLower string to lower |
||
411 | * |
||
412 | * @return static |
||
413 | */ |
||
414 | public function toSlugCase($replacement = '-', $language = 'en', $strToLower = true) |
||
436 | } |
||
437 |
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.