Complex classes like StringHelper 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 StringHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class StringHelper extends BaseStringHelper |
||
23 | { |
||
24 | /** |
||
25 | * TypeCast a string to its specific types. |
||
26 | * |
||
27 | * Arrays will passed to to the {{luya\helpers\ArrayHelper::typeCast()}} class. |
||
28 | * |
||
29 | * @param mixed $string The input string to type cast. Arrays will be passted to {{luya\helpers\ArrayHelper::typeCast()}}. |
||
30 | * @return mixed The new type casted value, if the input is an array the output is the typecasted array. |
||
31 | */ |
||
32 | public static function typeCast($string) |
||
42 | |||
43 | /** |
||
44 | * String Wildcard Check. |
||
45 | * |
||
46 | * Checks whether a strings starts with the wildcard symbole and compares the string before the wild card symbol * |
||
47 | * with the string provided, if there is NO wildcard symbold it always return false. |
||
48 | * |
||
49 | * |
||
50 | * @param string $string The string which should be checked with $with comperator |
||
51 | * @param string $with The with string which must end with the wildcard symbol * e.g. `foo*` would match string `foobar`. |
||
52 | * @param boolean $caseSensitive Whether to compare the starts with string as case sensitive or not, defaults to true. |
||
53 | * @return boolean Whether the string starts with the wildcard marked string or not, if no wildcard symbol is contained. |
||
54 | * in the $with it always returns false. |
||
55 | */ |
||
56 | public static function startsWithWildcard($string, $with, $caseSensitive = true) |
||
64 | |||
65 | |||
66 | |||
67 | /** |
||
68 | * See if filter conditions match the given value. |
||
69 | * |
||
70 | * Example filter conditions: |
||
71 | * |
||
72 | * + "cms_*" include only cms_* tables |
||
73 | * + "cms_*,admin_*" include only cms_* and admin_* tables |
||
74 | * + "!cms_*" exclude all cms_* tables |
||
75 | * + "!cms_*,!admin_*" exclude all cms_*and admin_* tables |
||
76 | * + "cms_*,!admin_*" include all cms_* tables but exclude all admin_* tables |
||
77 | * |
||
78 | * Only first match is relevant: |
||
79 | * |
||
80 | * + "cms_*,!admin_*,admin_*" include all cms_* tables but exclude all admin_* tables (last match has no effect) |
||
81 | * + "cms_*,admin_*,!admin_*" include all cms_* and admin_* tables (last match has no effect) |
||
82 | * |
||
83 | * Example using condition string: |
||
84 | * |
||
85 | * ```php |
||
86 | * filterMatch('hello', 'he*'); // true |
||
87 | * filterMatch('hello', 'ho,he*'); // true |
||
88 | * filterMatch('hello', ['ho', 'he*']); // true |
||
89 | * ``` |
||
90 | * |
||
91 | * @param $value The value on which the filter conditions should be applied on. |
||
92 | * @param array|string $filters An array of filter conditions, if a string is given he will be exploded by commas. |
||
|
|||
93 | * @param boolean $caseSensitive Whether to match value even when lower/upper case is not correct/same. |
||
94 | * @return bool True if table can be skipped. |
||
95 | * @since 1.3.0 |
||
96 | */ |
||
97 | public static function filterMatch($value, $conditions, $caseSensitive = true) |
||
140 | |||
141 | /** |
||
142 | * TypeCast a numeric value to float or integer. |
||
143 | * |
||
144 | * If the given value is not a numeric or float value it will be returned as it is. In order to find out whether its float |
||
145 | * or not use {{luya\helpers\StringHelper::isFloat()}}. |
||
146 | * |
||
147 | * @param mixed $value The given value to parse. |
||
148 | * @return mixed Returns the original value if not numeric or integer, float casted value. |
||
149 | */ |
||
150 | public static function typeCastNumeric($value) |
||
162 | |||
163 | /** |
||
164 | * Checks whether a string is a float value. |
||
165 | * |
||
166 | * Compared to `is_float` function of php, it only ensures whether the input variable is type float. |
||
167 | * |
||
168 | * @param mixed $value The value to check whether its float or not. |
||
169 | * @return boolean Whether its a float value or not. |
||
170 | */ |
||
171 | public static function isFloat($value) |
||
179 | |||
180 | /** |
||
181 | * Replace only the first occurance found inside the string. |
||
182 | * |
||
183 | * The replace first method is *case sensitive*. |
||
184 | * |
||
185 | * ```php |
||
186 | * StringHelper::replaceFirst('abc', '123', 'abc abc abc'); // returns "123 abc abc" |
||
187 | * ``` |
||
188 | * |
||
189 | * @param string $search Search string to look for. |
||
190 | * @param string $replace Replacement value for the first found occurrence. |
||
191 | * @param string $subject The string you want to look up to replace the first element. |
||
192 | * @return mixed Replaced string |
||
193 | */ |
||
194 | public static function replaceFirst($search, $replace, $subject) |
||
198 | |||
199 | /** |
||
200 | * Check whether a char or word exists in a string or not. |
||
201 | * |
||
202 | * This method is case sensitive. The need can be an array with multiple chars or words who |
||
203 | * are going to look up in the haystack string. |
||
204 | * |
||
205 | * If an array of needle words is provided the $strict parameter defines whether all need keys must be found |
||
206 | * in the string to get the `true` response or if just one of the keys are found the response is already `true`. |
||
207 | * |
||
208 | * ```php |
||
209 | * if (StringHelper::contains('foo', 'the foo bar Bar'')) { |
||
210 | * echo "yes!"; |
||
211 | * } |
||
212 | * ``` |
||
213 | * |
||
214 | * check if one of the given needles exists: |
||
215 | * |
||
216 | * ```php |
||
217 | * if (StringHelper::contains(['jungle', 'hell0], 'Welcome to the jungle!)) { |
||
218 | * echo "yes!"; |
||
219 | * } |
||
220 | * ``` |
||
221 | * |
||
222 | * @param string|array $needle The char or word to find in the $haystack. Can be an array to multi find words or char in the string. |
||
223 | * @param string $haystack The haystack where the $needle string should be looked up. A string or phrase with words. |
||
224 | * @param boolean $strict If an array of needles is provided the $strict parameter defines whether all keys must be found ($strict = true) or just one result must be found ($strict = false). |
||
225 | * @return boolean If an array of values is provided the response may change depending on $findAll. |
||
226 | */ |
||
227 | public static function contains($needle, $haystack, $strict = false) |
||
247 | |||
248 | /** |
||
249 | * "Minify" html content. |
||
250 | * |
||
251 | * + remove space |
||
252 | * + remove tabs |
||
253 | * + remove newlines |
||
254 | * + remove html comments |
||
255 | * |
||
256 | * @param string $content The content to minify. |
||
257 | * @param array $options Optional arguments to provide for minification: |
||
258 | * - comments: boolean, where html comments should be removed or not. defaults to false |
||
259 | * @return mixed Returns the minified content. |
||
260 | * @since 1.0.7 |
||
261 | */ |
||
262 | public static function minify($content, array $options = []) |
||
273 | |||
274 | /** |
||
275 | * Cut the given word/string from the content. Its truncates to the left side and to the right side of the word. |
||
276 | * |
||
277 | * An example of how a sentenced is cut: |
||
278 | * |
||
279 | * ```php |
||
280 | * $cut = StringHelper::truncateMiddle('the quick fox jumped over the lazy dog', 'jumped', 12); |
||
281 | * echo $cut; // ..e quick fox jumped over the la.. |
||
282 | * ``` |
||
283 | * |
||
284 | * @param string $content The content to cut the words from. |
||
285 | * @param string $word The word which should be in the middle of the string |
||
286 | * @param integer $length The amount of the chars to cut on the left and right side from the word. |
||
287 | * @param string $affix The chars which should be used for prefix and suffix when string is cuted. |
||
288 | * @param boolean $caseSensitive Whether the search word in the string even when lower/upper case is not correct. |
||
289 | * @since 1.0.12 |
||
290 | */ |
||
291 | public static function truncateMiddle($content, $word, $length, $affix = '..', $caseSensitive = false) |
||
316 | |||
317 | /** |
||
318 | * Highlight a word within a content. |
||
319 | * |
||
320 | * Since version 1.0.14 an array of words to highlight is possible. |
||
321 | * |
||
322 | * > This function IS NOT case sensitive! |
||
323 | * |
||
324 | * |
||
325 | * |
||
326 | * @param string $content The content to find the word. |
||
327 | * @param string $word The word to find within the content. |
||
328 | * @param string $markup The markup used wrap the word to highlight. |
||
329 | * @since 1.0.12 |
||
330 | */ |
||
331 | public static function highlightWord($content, $word, $markup = '<b>%s</b>') |
||
352 | |||
353 | /** |
||
354 | * Multibyte-safe str_split funciton. |
||
355 | * |
||
356 | * @param string $string The string to split into an array |
||
357 | * @param integer $length The length of the chars to cut. |
||
358 | * @since 1.0.12 |
||
359 | * @see https://www.php.net/manual/de/function.str-split.php#115703 |
||
360 | */ |
||
361 | public static function mb_str_split($string, $length = 1) |
||
365 | |||
366 | /** |
||
367 | * Check whether a value is numeric or not. |
||
368 | * |
||
369 | * There are situations where is_numeric does not provide the desried result, |
||
370 | * like for example `is_numeric('3e30')` would return true, as e can be considered |
||
371 | * as exponential operator. |
||
372 | * |
||
373 | * Therfore this function checks with regex whether values or 0-9 if strict is enabled, |
||
374 | * which is default behavior. |
||
375 | * |
||
376 | * @param mixed $value The value to check. |
||
377 | * @param boolean $strict |
||
378 | * @return boolean |
||
379 | * @since 1.2.0 |
||
380 | */ |
||
381 | public static function isNummeric($value, $strict = true) |
||
397 | } |
||
398 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.