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:
Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Str |
||
20 | { |
||
21 | protected $encoding = 'UTF-8'; |
||
22 | |||
23 | /** |
||
24 | * Truncates a string to the given length. It will optionally preserve |
||
25 | * HTML tags if $is_html is set to true. |
||
26 | * |
||
27 | * @param string $string the string to truncate |
||
28 | * @param int $limit the number of characters to truncate too |
||
29 | * @param string $continuation the string to use to denote it was truncated |
||
30 | * @param bool $is_html whether the string has HTML |
||
31 | * @return string the truncated string |
||
32 | */ |
||
33 | public function truncate($string, $limit, $continuation = '...', $is_html = false) |
||
75 | |||
76 | /** |
||
77 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
||
78 | * |
||
79 | * @param string $str required |
||
80 | * @return string |
||
81 | */ |
||
82 | public function increment($str, $first = 1, $separator = '_') |
||
88 | |||
89 | /** |
||
90 | * Checks wether a string has a precific beginning. |
||
91 | * |
||
92 | * @param string $str string to check |
||
93 | * @param string $start beginning to check for |
||
94 | * @param boolean $ignore_case wether to ignore the case |
||
95 | * @return boolean wether a string starts with a specified beginning |
||
96 | */ |
||
97 | public function startsWith($str, $start, $ignore_case = false) |
||
101 | |||
102 | /** |
||
103 | * Checks wether a string has a precific ending. |
||
104 | * |
||
105 | * @param string $str string to check |
||
106 | * @param string $end ending to check for |
||
107 | * @param boolean $ignore_case wether to ignore the case |
||
108 | * @return boolean wether a string ends with a specified ending |
||
109 | */ |
||
110 | public function endsWith($str, $end, $ignore_case = false) |
||
114 | |||
115 | /** |
||
116 | * substr |
||
117 | * |
||
118 | * @param string $str required |
||
119 | * @param int $start required |
||
120 | * @param int|null $length |
||
121 | * @param string $encoding default UTF-8 |
||
122 | * @return string |
||
123 | */ |
||
124 | public function sub($str, $start, $length = null, $encoding = null) |
||
135 | |||
136 | /** |
||
137 | * strlen |
||
138 | * |
||
139 | * @param string $str required |
||
140 | * @param string $encoding default UTF-8 |
||
141 | * @return int |
||
142 | */ |
||
143 | public function length($str, $encoding = null) |
||
151 | |||
152 | /** |
||
153 | * lower |
||
154 | * |
||
155 | * @param string $str required |
||
156 | * @param string $encoding default UTF-8 |
||
157 | * @return string |
||
158 | */ |
||
159 | public function lower($str, $encoding = null) |
||
167 | |||
168 | /** |
||
169 | * upper |
||
170 | * |
||
171 | * @param string $str required |
||
172 | * @param string $encoding default UTF-8 |
||
173 | * @return string |
||
174 | */ |
||
175 | public function upper($str, $encoding = null) |
||
183 | |||
184 | /** |
||
185 | * lcfirst |
||
186 | * |
||
187 | * Does not strtoupper first |
||
188 | * |
||
189 | * @param string $str required |
||
190 | * @param string $encoding default UTF-8 |
||
191 | * @return string |
||
192 | */ |
||
193 | View Code Duplication | public function lcfirst($str, $encoding = null) |
|
202 | |||
203 | /** |
||
204 | * ucfirst |
||
205 | * |
||
206 | * Does not strtolower first |
||
207 | * |
||
208 | * @param string $str required |
||
209 | * @param string $encoding default UTF-8 |
||
210 | * @return string |
||
211 | */ |
||
212 | View Code Duplication | public function ucfirst($str, $encoding = null) |
|
221 | |||
222 | /** |
||
223 | * ucwords |
||
224 | * |
||
225 | * First strtolower then ucwords |
||
226 | * |
||
227 | * ucwords normally doesn't strtolower first |
||
228 | * but MB_CASE_TITLE does, so ucwords now too |
||
229 | * |
||
230 | * @param string $str required |
||
231 | * @param string $encoding default UTF-8 |
||
232 | * @return string |
||
233 | */ |
||
234 | public function ucwords($str, $encoding = null) |
||
242 | |||
243 | /** |
||
244 | * Creates a random string of characters |
||
245 | * |
||
246 | * @param string the type of string |
||
247 | * @param int the number of characters |
||
248 | * @return string the random string |
||
249 | */ |
||
250 | public function random($type = 'alnum', $length = 16) |
||
309 | |||
310 | /** |
||
311 | * Returns a closure that will alternate between the args which to return. |
||
312 | * If you call the closure with false as the arg it will return the value without |
||
313 | * alternating the next time. |
||
314 | * |
||
315 | * @return \Closure |
||
316 | */ |
||
317 | public function alternator() |
||
328 | |||
329 | /** |
||
330 | * Parse the params from a string using strtr() |
||
331 | * |
||
332 | * @param string string to parse |
||
333 | * @param array params to str_replace |
||
334 | * @return string |
||
335 | */ |
||
336 | public function tr($string, $array = array()) |
||
356 | |||
357 | /** |
||
358 | * Check if a string is json encoded |
||
359 | * |
||
360 | * @param string $string string to check |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function is_json($string) |
||
368 | |||
369 | /** |
||
370 | * Check if a string is a valid XML |
||
371 | * |
||
372 | * @param string $string string to check |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function is_xml($string) |
||
384 | |||
385 | /** |
||
386 | * Check if a string is serialized |
||
387 | * |
||
388 | * @param string $string string to check |
||
389 | * @return bool |
||
390 | */ |
||
391 | public function is_serialized($string) |
||
396 | |||
397 | /** |
||
398 | * Check if a string is html |
||
399 | * |
||
400 | * @param string $string string to check |
||
401 | * @return bool |
||
402 | */ |
||
403 | public function is_html($string) |
||
407 | } |
||
408 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.