Complex classes like BaseStringHelper 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 BaseStringHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class BaseStringHelper |
||
22 | { |
||
23 | /** |
||
24 | * Returns the number of bytes in the given string. |
||
25 | * This method ensures the string is treated as a byte array by using `mb_strlen()`. |
||
26 | * @param string $string the string being measured for length |
||
27 | * @return int the number of bytes in the given string. |
||
28 | */ |
||
29 | 314 | public static function byteLength($string) |
|
33 | |||
34 | /** |
||
35 | * Returns the portion of string specified by the start and length parameters. |
||
36 | * This method ensures the string is treated as a byte array by using `mb_substr()`. |
||
37 | * @param string $string the input string. Must be one character or longer. |
||
38 | * @param int $start the starting position |
||
39 | * @param int $length the desired portion length. If not specified or `null`, there will be |
||
40 | * no limit on length i.e. the output will be until the end of the string. |
||
41 | * @return string the extracted part of string, or FALSE on failure or an empty string. |
||
42 | * @see http://www.php.net/manual/en/function.substr.php |
||
43 | */ |
||
44 | 90 | public static function byteSubstr($string, $start, $length = null) |
|
48 | |||
49 | /** |
||
50 | * Returns the trailing name component of a path. |
||
51 | * This method is similar to the php function `basename()` except that it will |
||
52 | * treat both \ and / as directory separators, independent of the operating system. |
||
53 | * This method was mainly created to work on php namespaces. When working with real |
||
54 | * file paths, php's `basename()` should work fine for you. |
||
55 | * Note: this method is not aware of the actual filesystem, or path components such as "..". |
||
56 | * |
||
57 | * @param string $path A path string. |
||
58 | * @param string $suffix If the name component ends in suffix this will also be cut off. |
||
59 | * @return string the trailing name component of the given path. |
||
60 | * @see http://www.php.net/manual/en/function.basename.php |
||
61 | */ |
||
62 | 15 | public static function basename($path, $suffix = '') |
|
74 | |||
75 | /** |
||
76 | * Returns parent directory's path. |
||
77 | * This method is similar to `dirname()` except that it will treat |
||
78 | * both \ and / as directory separators, independent of the operating system. |
||
79 | * |
||
80 | * @param string $path A path string. |
||
81 | * @return string the parent directory's path. |
||
82 | * @see http://www.php.net/manual/en/function.basename.php |
||
83 | */ |
||
84 | 5 | public static function dirname($path) |
|
85 | { |
||
86 | 5 | $pos = mb_strrpos(str_replace('\\', '/', $path), '/'); |
|
87 | 5 | if ($pos !== false) { |
|
88 | 5 | return mb_substr($path, 0, $pos); |
|
89 | } |
||
90 | |||
91 | return ''; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Truncates a string to the number of characters specified. |
||
96 | * |
||
97 | * @param string $string The string to truncate. |
||
98 | * @param int $length How many characters from original string to include into truncated string. |
||
99 | * @param string $suffix String to append to the end of truncated string. |
||
100 | * @param string $encoding The charset to use, defaults to charset currently used by application. |
||
101 | * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
||
102 | * This parameter is available since version 2.0.1. |
||
103 | * @return string the truncated string. |
||
104 | */ |
||
105 | 1 | public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) |
|
106 | { |
||
107 | 1 | if ($encoding === null) { |
|
108 | 1 | $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
|
109 | } |
||
110 | 1 | if ($asHtml) { |
|
111 | 1 | return static::truncateHtml($string, $length, $suffix, $encoding); |
|
112 | } |
||
113 | |||
114 | 1 | if (mb_strlen($string, $encoding) > $length) { |
|
115 | 1 | return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix; |
|
116 | } |
||
117 | |||
118 | 1 | return $string; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Truncates a string to the number of words specified. |
||
123 | * |
||
124 | * @param string $string The string to truncate. |
||
125 | * @param int $count How many words from original string to include into truncated string. |
||
126 | * @param string $suffix String to append to the end of truncated string. |
||
127 | * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
||
128 | * This parameter is available since version 2.0.1. |
||
129 | * @return string the truncated string. |
||
130 | */ |
||
131 | 1 | public static function truncateWords($string, $count, $suffix = '...', $asHtml = false) |
|
132 | { |
||
133 | 1 | if ($asHtml) { |
|
134 | 1 | return static::truncateHtml($string, $count, $suffix); |
|
135 | } |
||
136 | |||
137 | 1 | $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE); |
|
138 | 1 | if (count($words) / 2 > $count) { |
|
139 | 1 | return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix; |
|
140 | } |
||
141 | |||
142 | 1 | return $string; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Truncate a string while preserving the HTML. |
||
147 | * |
||
148 | * @param string $string The string to truncate |
||
149 | * @param int $count |
||
150 | * @param string $suffix String to append to the end of the truncated string. |
||
151 | * @param string|bool $encoding |
||
152 | * @return string |
||
153 | * @since 2.0.1 |
||
154 | */ |
||
155 | 2 | protected static function truncateHtml($string, $count, $suffix, $encoding = false) |
|
206 | |||
207 | /** |
||
208 | * Check if given string starts with specified substring. |
||
209 | * Binary and multibyte safe. |
||
210 | * |
||
211 | * @param string $string Input string |
||
212 | * @param string $with Part to search inside the $string |
||
213 | * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the starting of the string in order to get a true value. |
||
214 | * @return bool Returns true if first input starts with second input, false otherwise |
||
215 | */ |
||
216 | 23 | public static function startsWith($string, $with, $caseSensitive = true) |
|
228 | |||
229 | /** |
||
230 | * Check if given string ends with specified substring. |
||
231 | * Binary and multibyte safe. |
||
232 | * |
||
233 | * @param string $string Input string to check |
||
234 | * @param string $with Part to search inside of the $string. |
||
235 | * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the ending of the string in order to get a true value. |
||
236 | * @return bool Returns true if first input ends with second input, false otherwise |
||
237 | */ |
||
238 | 20 | public static function endsWith($string, $with, $caseSensitive = true) |
|
255 | |||
256 | /** |
||
257 | * Explodes string into array, optionally trims values and skips empty ones. |
||
258 | * |
||
259 | * @param string $string String to be exploded. |
||
260 | * @param string $delimiter Delimiter. Default is ','. |
||
261 | * @param mixed $trim Whether to trim each element. Can be: |
||
262 | * - boolean - to trim normally; |
||
263 | * - string - custom characters to trim. Will be passed as a second argument to `trim()` function. |
||
264 | * - callable - will be called for each value instead of trim. Takes the only argument - value. |
||
265 | * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false. |
||
266 | * @return array |
||
267 | * @since 2.0.4 |
||
268 | */ |
||
269 | 1 | public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) |
|
291 | |||
292 | /** |
||
293 | * Counts words in a string. |
||
294 | * @since 2.0.8 |
||
295 | * |
||
296 | * @param string $string |
||
297 | * @return int |
||
298 | */ |
||
299 | 2 | public static function countWords($string) |
|
303 | |||
304 | /** |
||
305 | * Returns string representation of number value with replaced commas to dots, if decimal point |
||
306 | * of current locale is comma. |
||
307 | * @param int|float|string $value |
||
308 | * @return string |
||
309 | * @since 2.0.11 |
||
310 | */ |
||
311 | 24 | public static function normalizeNumber($value) |
|
324 | |||
325 | /** |
||
326 | * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
||
327 | * |
||
328 | * > Note: Base 64 padding `=` may be at the end of the returned string. |
||
329 | * > `=` is not transparent to URL encoding. |
||
330 | * |
||
331 | * @see https://tools.ietf.org/html/rfc4648#page-7 |
||
332 | * @param string $input the string to encode. |
||
333 | * @return string encoded string. |
||
334 | * @since 2.0.12 |
||
335 | */ |
||
336 | 59 | public static function base64UrlEncode($input) |
|
340 | |||
341 | /** |
||
342 | * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
||
343 | * |
||
344 | * @see https://tools.ietf.org/html/rfc4648#page-7 |
||
345 | * @param string $input encoded string. |
||
346 | * @return string decoded string. |
||
347 | * @since 2.0.12 |
||
348 | */ |
||
349 | 12 | public static function base64UrlDecode($input) |
|
353 | |||
354 | /** |
||
355 | * Ensures string is prefixed with the prefix specified. Avoids double prefixing. |
||
356 | * |
||
357 | * Useful for adding "http://" to user entered website addresses, twitter handles etc. |
||
358 | * |
||
359 | * @param string $string Input string |
||
360 | * @param string $prefix Prefix to be ensured in the string |
||
361 | * @param bool $caseSensitive Case sensitive comparison. Default is true. When case sensitive is enabled, $prefix |
||
362 | * must exactly match the starting of the string. |
||
363 | * @return string |
||
364 | * |
||
365 | * @since 2.0.13 |
||
366 | */ |
||
367 | 3 | public static function ensurePrefix($string, $prefix, $caseSensitive = true) |
|
374 | } |
||
375 |
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.