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 |
||
| 63 | class BaseStringHelper |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * Returns the number of bytes in the given string. |
||
| 67 | * This method ensures the string is treated as a byte array by using `mb_strlen()`. |
||
| 68 | * |
||
| 69 | * @param string $string the string being measured for length |
||
| 70 | * |
||
| 71 | * @return int the number of bytes in the given string. |
||
| 72 | */ |
||
| 73 | public static function byteLength($string) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns the portion of string specified by the start and length parameters. |
||
| 80 | * This method ensures the string is treated as a byte array by using `mb_substr()`. |
||
| 81 | * |
||
| 82 | * @param string $string the input string. Must be one character or longer. |
||
| 83 | * @param int $start the starting position |
||
| 84 | * @param int $length the desired portion length. If not specified or `null`, there will be |
||
| 85 | * no limit on length i.e. the output will be until the end of the string. |
||
| 86 | * |
||
| 87 | * @return string the extracted part of string, or FALSE on failure or an empty string. |
||
| 88 | * @see http://www.php.net/manual/en/function.substr.php |
||
| 89 | */ |
||
| 90 | public static function byteSubstr($string, $start, $length = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the trailing name component of a path. |
||
| 97 | * This method is similar to the php function `basename()` except that it will |
||
| 98 | * treat both \ and / as directory separators, independent of the operating system. |
||
| 99 | * This method was mainly created to work on php namespaces. When working with real |
||
| 100 | * file paths, php's `basename()` should work fine for you. |
||
| 101 | * Note: this method is not aware of the actual filesystem, or path components such as "..". |
||
| 102 | * |
||
| 103 | * @param string $path A path string. |
||
| 104 | * @param string $suffix If the name component ends in suffix this will also be cut off. |
||
| 105 | * |
||
| 106 | * @return string the trailing name component of the given path. |
||
| 107 | * @see http://www.php.net/manual/en/function.basename.php |
||
| 108 | */ |
||
| 109 | public static function basename($path, $suffix = '') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns parent directory's path. |
||
| 124 | * This method is similar to `dirname()` except that it will treat |
||
| 125 | * both \ and / as directory separators, independent of the operating system. |
||
| 126 | * |
||
| 127 | * @param string $path A path string. |
||
| 128 | * |
||
| 129 | * @return string the parent directory's path. |
||
| 130 | * @see http://www.php.net/manual/en/function.basename.php |
||
| 131 | */ |
||
| 132 | public static function dirname($path) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Truncates a string to the number of characters specified. |
||
| 144 | * |
||
| 145 | * @param string $string The string to truncate. |
||
| 146 | * @param int $length How many characters from original string to include into truncated string. |
||
| 147 | * @param string $suffix String to append to the end of truncated string. |
||
| 148 | * @param string $encoding The charset to use, defaults to charset currently used by application. |
||
| 149 | * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
||
| 150 | * This parameter is available since version 2.0.1. |
||
| 151 | * |
||
| 152 | * @return string the truncated string. |
||
| 153 | */ |
||
| 154 | public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Truncates a string to the number of words specified. |
||
| 172 | * |
||
| 173 | * @param string $string The string to truncate. |
||
| 174 | * @param int $count How many words from original string to include into truncated string. |
||
| 175 | * @param string $suffix String to append to the end of truncated string. |
||
| 176 | * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags. |
||
| 177 | * This parameter is available since version 2.0.1. |
||
| 178 | * |
||
| 179 | * @return string the truncated string. |
||
| 180 | */ |
||
| 181 | public static function truncateWords($string, $count, $suffix = '...', $asHtml = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Truncate a string while preserving the HTML. |
||
| 197 | * |
||
| 198 | * @param string $string The string to truncate |
||
| 199 | * @param int $count |
||
| 200 | * @param string $suffix String to append to the end of the truncated string. |
||
| 201 | * @param string|bool $encoding |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | * @since 2.0.1 |
||
| 205 | */ |
||
| 206 | protected static function truncateHtml($string, $count, $suffix, $encoding = false) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Check if given string starts with specified substring. |
||
| 257 | * Binary and multibyte safe. |
||
| 258 | * |
||
| 259 | * @param string $string Input string |
||
| 260 | * @param string $with Part to search inside the $string |
||
| 261 | * @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. |
||
| 262 | * |
||
| 263 | * @return bool Returns true if first input starts with second input, false otherwise |
||
| 264 | */ |
||
| 265 | public static function startsWith($string, $with, $caseSensitive = true) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Check if given string ends with specified substring. |
||
| 279 | * Binary and multibyte safe. |
||
| 280 | * |
||
| 281 | * @param string $string Input string to check |
||
| 282 | * @param string $with Part to search inside of the $string. |
||
| 283 | * @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. |
||
| 284 | * |
||
| 285 | * @return bool Returns true if first input ends with second input, false otherwise |
||
| 286 | */ |
||
| 287 | public static function endsWith($string, $with, $caseSensitive = true) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Explodes string into array, optionally trims values and skips empty ones. |
||
| 307 | * |
||
| 308 | * @param string $string String to be exploded. |
||
| 309 | * @param string $delimiter Delimiter. Default is ','. |
||
| 310 | * @param mixed $trim Whether to trim each element. Can be: |
||
| 311 | * - boolean - to trim normally; |
||
| 312 | * - string - custom characters to trim. Will be passed as a second argument to `trim()` function. |
||
| 313 | * - callable - will be called for each value instead of trim. Takes the only argument - value. |
||
| 314 | * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | * @since 2.0.4 |
||
| 318 | */ |
||
| 319 | public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Counts words in a string. |
||
| 344 | * |
||
| 345 | * @since 2.0.8 |
||
| 346 | * |
||
| 347 | * @param string $string |
||
| 348 | * |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | public static function countWords($string) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Returns string representation of number value with replaced commas to dots, if decimal point |
||
| 358 | * of current locale is comma. |
||
| 359 | * |
||
| 360 | * @param int|float|string $value |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | * @since 2.0.11 |
||
| 364 | */ |
||
| 365 | public static function normalizeNumber($value) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
||
| 381 | * |
||
| 382 | * > Note: Base 64 padding `=` may be at the end of the returned string. |
||
| 383 | * > `=` is not transparent to URL encoding. |
||
| 384 | * |
||
| 385 | * @see https://tools.ietf.org/html/rfc4648#page-7 |
||
| 386 | * |
||
| 387 | * @param string $input the string to encode. |
||
| 388 | * |
||
| 389 | * @return string encoded string. |
||
| 390 | * @since 2.0.12 |
||
| 391 | */ |
||
| 392 | public static function base64UrlEncode($input) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). |
||
| 399 | * |
||
| 400 | * @see https://tools.ietf.org/html/rfc4648#page-7 |
||
| 401 | * |
||
| 402 | * @param string $input encoded string. |
||
| 403 | * |
||
| 404 | * @return string decoded string. |
||
| 405 | * @since 2.0.12 |
||
| 406 | */ |
||
| 407 | public static function base64UrlDecode($input) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Safely casts a float to string independent of the current locale. |
||
| 414 | * |
||
| 415 | * The decimal separator will always be `.`. |
||
| 416 | * |
||
| 417 | * @param float|int $number a floating point number or integer. |
||
| 418 | * |
||
| 419 | * @return string the string representation of the number. |
||
| 420 | * @since 2.0.13 |
||
| 421 | */ |
||
| 422 | public static function floatToString($number) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Checks if the passed string would match the given shell wildcard pattern. |
||
| 431 | * This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE. |
||
| 432 | * |
||
| 433 | * @param string $pattern the shell wildcard pattern. |
||
| 434 | * @param string $string the tested string. |
||
| 435 | * @param array $options options for matching. Valid options are: |
||
| 436 | * |
||
| 437 | * - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`. |
||
| 438 | * - escape: bool, whether backslash escaping is enabled. Defaults to `true`. |
||
| 439 | * - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`. |
||
| 440 | * |
||
| 441 | * @return bool whether the string matches pattern or not. |
||
| 442 | * @since 2.0.14 |
||
| 443 | */ |
||
| 444 | public static function matchWildcard($pattern, $string, $options = array()) |
||
| 482 | } |
||
| 483 | } |
||
| 484 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.