| Total Complexity | 77 | 
| Total Lines | 420 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like StringFilter 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.
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 StringFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 59 | class StringFilter extends AbstractFilter | ||
| 60 | { | ||
| 61 | /** | ||
| 62 | * Return the length of string or array | ||
| 63 | * @param mixed $variable | ||
| 64 | * @return int|mixed | ||
| 65 | */ | ||
| 66 | public static function length($variable) | ||
| 67 |     { | ||
| 68 |         if ($variable instanceof Traversable) { | ||
| 69 | return iterator_count($variable); | ||
| 70 | } | ||
| 71 | |||
| 72 |         if (is_array($variable)) { | ||
| 73 | return count($variable); | ||
| 74 | } | ||
| 75 | |||
| 76 |         if (is_object($variable)) { | ||
| 77 |             if (method_exists($variable, 'size')) { | ||
| 78 | return $variable->size(); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 |         if (!is_string($variable)) { | ||
| 83 | return $variable; | ||
| 84 | } | ||
| 85 | |||
| 86 |         if (function_exists('mb_strlen')) { | ||
| 87 | return mb_strlen($variable); | ||
| 88 | } | ||
| 89 | |||
| 90 | return strlen($variable); | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Add one string to another | ||
| 95 | * @param mixed $variable | ||
| 96 | * @param mixed $value | ||
| 97 | * @return string|mixed | ||
| 98 | */ | ||
| 99 | public static function append($variable, $value) | ||
| 100 |     { | ||
| 101 |         if (!is_string($variable) || !is_string($value)) { | ||
| 102 | return $variable; | ||
| 103 | } | ||
| 104 | |||
| 105 | return $variable . $value; | ||
| 106 | } | ||
| 107 | |||
| 108 | /** | ||
| 109 | * Prefix a string to variable | ||
| 110 | * @param mixed $variable | ||
| 111 | * @param mixed $value | ||
| 112 | * @return string|mixed | ||
| 113 | */ | ||
| 114 | public static function prepend($variable, $value) | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Remove a string to variable | ||
| 125 | * @param mixed $variable | ||
| 126 | * @param mixed $value | ||
| 127 | * @return string|mixed | ||
| 128 | */ | ||
| 129 | public static function remove($variable, $value) | ||
| 130 |     { | ||
| 131 |         if (!is_string($variable) || !is_string($value)) { | ||
| 132 | return $variable; | ||
| 133 | } | ||
| 134 | |||
| 135 | return str_replace($value, '', $variable); | ||
| 136 | } | ||
| 137 | |||
| 138 | /** | ||
| 139 | * Replace occurrences of a string with another | ||
| 140 | * @param mixed $variable | ||
| 141 | * @param mixed $value | ||
| 142 | * @param mixed $replacement | ||
| 143 | * @return string|mixed | ||
| 144 | */ | ||
| 145 | public static function replace($variable, $value, $replacement = '') | ||
| 146 |     { | ||
| 147 |         if (!is_string($variable) || !is_string($value) || !is_string($replacement)) { | ||
| 148 | return $variable; | ||
| 149 | } | ||
| 150 | |||
| 151 | return str_replace($value, $replacement, $variable); | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Truncate a string down to x characters | ||
| 156 | * @param mixed $variable | ||
| 157 | * @param int|mixed $count | ||
| 158 | * @param string|mixed $ending | ||
| 159 | * @return string|mixed | ||
| 160 | */ | ||
| 161 | public static function truncate($variable, $count = 100, $ending = '...') | ||
| 162 |     { | ||
| 163 |         if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { | ||
| 164 | return $variable; | ||
| 165 | } | ||
| 166 | |||
| 167 | $numberChar = (int) $count; | ||
| 168 |         if (strlen($variable) > $numberChar) { | ||
| 169 | return substr($variable, 0, $numberChar) . $ending; | ||
| 170 | } | ||
| 171 | |||
| 172 | return $variable; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * Truncate string down to x words | ||
| 177 | * @param mixed $variable | ||
| 178 | * @param int|mixed $count | ||
| 179 | * @param string|mixed $ending | ||
| 180 | * @return string|mixed | ||
| 181 | */ | ||
| 182 | public static function truncateWord($variable, $count = 3, $ending = '...') | ||
| 183 |     { | ||
| 184 |         if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { | ||
| 185 | return $variable; | ||
| 186 | } | ||
| 187 | |||
| 188 | $numberWords = (int) $count; | ||
| 189 |         $wordList = explode(' ', $variable); | ||
| 190 |         if (count($wordList) > $numberWords) { | ||
| 191 |             return implode(' ', array_slice($wordList, 0, $numberWords)) . $ending; | ||
| 192 | } | ||
| 193 | |||
| 194 | return $variable; | ||
| 195 | } | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Put all letters to upper case | ||
| 199 | * @param mixed $variable | ||
| 200 | * @return string|mixed | ||
| 201 | */ | ||
| 202 | public static function upper($variable) | ||
| 203 |     { | ||
| 204 |         if (!is_string($variable)) { | ||
| 205 | return $variable; | ||
| 206 | } | ||
| 207 | |||
| 208 |         if (function_exists('mb_strtoupper')) { | ||
| 209 | return mb_strtoupper($variable); | ||
| 210 | } | ||
| 211 | |||
| 212 | return strtoupper($variable); | ||
| 213 | } | ||
| 214 | |||
| 215 | /** | ||
| 216 | * URL encodes a string | ||
| 217 | * @param mixed $variable | ||
| 218 | * @return string|mixed | ||
| 219 | */ | ||
| 220 | public static function urlEncode($variable) | ||
| 221 |     { | ||
| 222 |         if (!is_string($variable)) { | ||
| 223 | return $variable; | ||
| 224 | } | ||
| 225 | |||
| 226 | return urlencode($variable); | ||
| 227 | } | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Decodes a URL-encoded string | ||
| 231 | * @param mixed $variable | ||
| 232 | * @return string|mixed | ||
| 233 | */ | ||
| 234 | public static function urlDecode($variable) | ||
| 235 |     { | ||
| 236 |         if (!is_string($variable)) { | ||
| 237 | return $variable; | ||
| 238 | } | ||
| 239 | |||
| 240 | return urldecode($variable); | ||
| 241 | } | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Explicit string conversion. | ||
| 245 | * @param mixed $variable | ||
| 246 | * @return string|mixed | ||
| 247 | */ | ||
| 248 | public static function stringfy($variable) | ||
| 255 | } | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Split input string into an array of sub | ||
| 259 | * strings separated by given pattern. | ||
| 260 | * @param string|mixed $variable | ||
| 261 | * @param mixed $pattern | ||
| 262 | * @return array<mixed>|mixed | ||
| 263 | */ | ||
| 264 | public static function split($variable, $pattern) | ||
| 271 | } | ||
| 272 | |||
| 273 | /** | ||
| 274 | * If the given value is part of the variable | ||
| 275 | * @param string|mixed $variable | ||
| 276 | * @param mixed $value | ||
| 277 | * @return int|false|mixed | ||
| 278 | */ | ||
| 279 | public static function find($variable, $value) | ||
| 280 |     { | ||
| 281 |         if (!is_string($variable) || !is_string($value)) { | ||
| 282 | return $variable; | ||
| 283 | } | ||
| 284 | |||
| 285 | return strpos($variable, $value); | ||
| 286 | } | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Pseudo-filter: negates auto-added escape filter | ||
| 290 | * @param mixed $variable | ||
| 291 | * @return string|mixed | ||
| 292 | */ | ||
| 293 | public static function raw($variable) | ||
| 294 |     { | ||
| 295 | return $variable; | ||
| 296 | } | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Escape a string | ||
| 300 | * @param mixed $variable | ||
| 301 | * @return string|mixed | ||
| 302 | */ | ||
| 303 | public static function escape($variable) | ||
| 304 |     { | ||
| 305 |         if (!is_string($variable)) { | ||
| 306 | return $variable; | ||
| 307 | } | ||
| 308 | |||
| 309 | return htmlspecialchars($variable, ENT_QUOTES); | ||
| 310 | } | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Escape a string once, keeping all previous HTML entities intact | ||
| 314 | * @param mixed $variable | ||
| 315 | * @return string|mixed | ||
| 316 | */ | ||
| 317 | public static function escapeOnce($variable) | ||
| 318 |     { | ||
| 319 |         if (!is_string($variable)) { | ||
| 320 | return $variable; | ||
| 321 | } | ||
| 322 | |||
| 323 | return htmlentities($variable, ENT_QUOTES, null, false); | ||
| 324 | } | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Set default value if is blank | ||
| 328 | * @param mixed $variable | ||
| 329 | * @param mixed $value | ||
| 330 | * @return mixed | ||
| 331 | */ | ||
| 332 | public static function defaultValue($variable, $value) | ||
| 333 |     { | ||
| 334 | $isBlank = (is_string($variable) && $variable === '') | ||
| 335 | || is_bool($variable) && $variable === false | ||
| 336 | || $variable === null; | ||
| 337 | |||
| 338 | |||
| 339 | return $isBlank ? $value : $variable; | ||
| 340 | } | ||
| 341 | |||
| 342 | /** | ||
| 343 | * Join elements of an array with a given | ||
| 344 | * character between them | ||
| 345 | * @param array<mixed>|Traversable|mixed $variable | ||
| 346 | * @param mixed $glue | ||
| 347 | * @return string|mixed | ||
| 348 | */ | ||
| 349 | public static function join($variable, $glue = ' ') | ||
| 350 |     { | ||
| 351 |         if (!is_string($glue)) { | ||
| 352 | return $variable; | ||
| 353 | } | ||
| 354 | |||
| 355 |         if ($variable instanceof Traversable) { | ||
| 356 | $str = ''; | ||
| 357 |             foreach ($variable as $element) { | ||
| 358 |                 if ($str) { | ||
| 359 | $str .= $glue; | ||
| 360 | } | ||
| 361 | |||
| 362 | $str .= $element; | ||
| 363 | } | ||
| 364 | |||
| 365 | return $str; | ||
| 366 | } | ||
| 367 | |||
| 368 | return is_array($variable) | ||
| 369 | ? implode($glue, $variable) | ||
| 370 | : $variable; | ||
| 371 | } | ||
| 372 | |||
| 373 | /** | ||
| 374 | * Put all letter to lower case | ||
| 375 | * @param mixed $variable | ||
| 376 | * @return string|mixed | ||
| 377 | */ | ||
| 378 | public static function lower($variable) | ||
| 389 | } | ||
| 390 | |||
| 391 | /** | ||
| 392 | * Capitalize words in the input sentence | ||
| 393 | * @param mixed $variable | ||
| 394 | * @return string|mixed | ||
| 395 | */ | ||
| 396 | public static function capitalize($variable) | ||
| 397 |     { | ||
| 398 |         if (!is_string($variable)) { | ||
| 399 | return $variable; | ||
| 400 | } | ||
| 401 | |||
| 402 | return (string) preg_replace_callback( | ||
| 403 |             '/(^|[^\p{L}\'])([\p{Ll}])/u', | ||
| 404 |             function ($matches) { | ||
| 405 | return $matches[1] . ucfirst($matches[2]); | ||
| 406 | }, | ||
| 407 | ucwords($variable) | ||
| 408 | ); | ||
| 409 | } | ||
| 410 | |||
| 411 | /** | ||
| 412 | * Remove the left blank characters | ||
| 413 | * @param mixed $variable | ||
| 414 | * @return string|mixed | ||
| 415 | */ | ||
| 416 | public static function lstrip($variable) | ||
| 417 |     { | ||
| 418 |         if (!is_string($variable)) { | ||
| 419 | return $variable; | ||
| 420 | } | ||
| 421 | |||
| 422 | return ltrim($variable); | ||
| 423 | } | ||
| 424 | |||
| 425 | /** | ||
| 426 | * Remove the right blank characters | ||
| 427 | * @param mixed $variable | ||
| 428 | * @return string|mixed | ||
| 429 | */ | ||
| 430 | public static function rstrip($variable) | ||
| 431 |     { | ||
| 432 |         if (!is_string($variable)) { | ||
| 433 | return $variable; | ||
| 434 | } | ||
| 435 | |||
| 436 | return rtrim($variable); | ||
| 437 | } | ||
| 438 | |||
| 439 | /** | ||
| 440 | * Remove the left and right blank characters | ||
| 441 | * @param mixed $variable | ||
| 442 | * @return string|mixed | ||
| 443 | */ | ||
| 444 | public static function strip($variable) | ||
| 445 |     { | ||
| 446 |         if (!is_string($variable)) { | ||
| 447 | return $variable; | ||
| 448 | } | ||
| 449 | |||
| 450 | return trim($variable); | ||
| 451 | } | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Removes HTML tags from text | ||
| 455 | * @param mixed $variable | ||
| 456 | * @return string|mixed | ||
| 457 | */ | ||
| 458 | public static function stripHtml($variable) | ||
| 459 |     { | ||
| 460 |         if (!is_string($variable)) { | ||
| 461 | return $variable; | ||
| 462 | } | ||
| 463 | |||
| 464 | return strip_tags($variable); | ||
| 465 | } | ||
| 466 | |||
| 467 | /** | ||
| 468 | * Strip all newlines (\n, \r) from string | ||
| 469 | * @param mixed $variable | ||
| 470 | * @return string|mixed | ||
| 471 | */ | ||
| 472 | public static function stripNewLine($variable) | ||
| 479 | } | ||
| 480 | } | ||
| 481 |