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 | 198 | 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 | 66 | 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 | 12 | 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) |
|
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) |
|
117 | |||
118 | /** |
||
119 | * Truncates a string to the number of words specified. |
||
120 | * |
||
121 | * @param string $string The string to truncate. |
||
122 | * @param int $count How many words from original string to include into truncated string. |
||
123 | * @param string $suffix String to append to the end of truncated string. |
||
124 | * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
||
125 | * This parameter is available since version 2.0.1. |
||
126 | * @return string the truncated string. |
||
127 | */ |
||
128 | 1 | public static function truncateWords($string, $count, $suffix = '...', $asHtml = false) |
|
141 | |||
142 | /** |
||
143 | * Truncate a string while preserving the HTML. |
||
144 | * |
||
145 | * @param string $string The string to truncate |
||
146 | * @param int $count |
||
147 | * @param string $suffix String to append to the end of the truncated string. |
||
148 | * @param string|bool $encoding |
||
149 | * @return string |
||
150 | * @since 2.0.1 |
||
151 | */ |
||
152 | 2 | protected static function truncateHtml($string, $count, $suffix, $encoding = false) |
|
194 | |||
195 | /** |
||
196 | * Check if given string starts with specified substring. |
||
197 | * Binary and multibyte safe. |
||
198 | * |
||
199 | * @param string $string Input string |
||
200 | * @param string $with Part to search inside the $string |
||
201 | * @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. |
||
202 | * @return bool Returns true if first input starts with second input, false otherwise |
||
203 | */ |
||
204 | 19 | public static function startsWith($string, $with, $caseSensitive = true) |
|
215 | |||
216 | /** |
||
217 | * Check if given string ends with specified substring. |
||
218 | * Binary and multibyte safe. |
||
219 | * |
||
220 | * @param string $string Input string to check |
||
221 | * @param string $with Part to search inside of the $string. |
||
222 | * @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. |
||
223 | * @return bool Returns true if first input ends with second input, false otherwise |
||
224 | */ |
||
225 | 19 | public static function endsWith($string, $with, $caseSensitive = true) |
|
240 | |||
241 | /** |
||
242 | * Explodes string into array, optionally trims values and skips empty ones |
||
243 | * |
||
244 | * @param string $string String to be exploded. |
||
245 | * @param string $delimiter Delimiter. Default is ','. |
||
246 | * @param mixed $trim Whether to trim each element. Can be: |
||
247 | * - boolean - to trim normally; |
||
248 | * - string - custom characters to trim. Will be passed as a second argument to `trim()` function. |
||
249 | * - callable - will be called for each value instead of trim. Takes the only argument - value. |
||
250 | * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false. |
||
251 | * @return array |
||
252 | * @since 2.0.4 |
||
253 | */ |
||
254 | 1 | public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) |
|
275 | |||
276 | /** |
||
277 | * Counts words in a string |
||
278 | * @since 2.0.8 |
||
279 | * |
||
280 | * @param string $string |
||
281 | * @return int |
||
282 | */ |
||
283 | 2 | public static function countWords($string) |
|
287 | } |
||
288 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: