Complex classes like Utils 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 Utils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | final class Utils |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Requires/Includes the given file and returns the results. |
||
| 24 | * |
||
| 25 | * @param string $file The path to the file |
||
| 26 | * @param string $type Type of loading, REQ for require, INC for include |
||
| 27 | * @param bool $once Require once or not |
||
| 28 | * @return mixed The results of the include |
||
| 29 | * @codeCoverageIgnore |
||
| 30 | * @since 0.1.0 |
||
| 31 | */ |
||
| 32 | public static function load($file = null, $type = REQ, $once = false) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get the content of given file and returns the results. |
||
| 47 | * |
||
| 48 | * @param string $file The path to the file |
||
| 49 | * @return mixed The results of the include |
||
| 50 | * @codeCoverageIgnore |
||
| 51 | * @since 0.2.4 |
||
| 52 | */ |
||
| 53 | public static function getContent($file = null) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the given file and returns the results. |
||
| 60 | * |
||
| 61 | * @param string $path The path to the file |
||
| 62 | * @return mixed The results of the include |
||
| 63 | * @codeCoverageIgnore |
||
| 64 | * @since 0.2.4 |
||
| 65 | */ |
||
| 66 | public static function getFile($path = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Takes a value and checks if it's a Closure or not, if it's a Closure it |
||
| 73 | * will return the result of the closure, if not, it will simply return the |
||
| 74 | * value. |
||
| 75 | * |
||
| 76 | * @param mixed $var The value to get |
||
| 77 | * @return mixed |
||
| 78 | * @since 0.1.0 |
||
| 79 | * @codeCoverageIgnore |
||
| 80 | */ |
||
| 81 | public static function checkValue($var = null) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Trim array elements |
||
| 88 | * |
||
| 89 | * @param array $content The array to trim |
||
| 90 | * @return mixed |
||
| 91 | * @since 0.2.4 |
||
| 92 | * @codeCoverageIgnore |
||
| 93 | */ |
||
| 94 | public static function trimArrayElements($content = null) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Remove quotes |
||
| 107 | * |
||
| 108 | * @param string $string The string to remove quotes from |
||
| 109 | * @return string |
||
| 110 | * @since 0.2.4 |
||
| 111 | * @codeCoverageIgnore |
||
| 112 | */ |
||
| 113 | public static function removeQuotes($string = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param array $content |
||
| 124 | * @return array |
||
| 125 | * @since 0.2.4 |
||
| 126 | * @codeCoverageIgnore |
||
| 127 | */ |
||
| 128 | public static function stripBackslashes($content = null) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $needle |
||
| 141 | * @param string $string |
||
| 142 | * @return bool |
||
| 143 | * @since 0.2.4 |
||
| 144 | * @codeCoverageIgnore |
||
| 145 | */ |
||
| 146 | public static function stringStart($needle, $string) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $path The path to the file |
||
| 153 | * @return array |
||
| 154 | * @since 0.2.4 |
||
| 155 | * @codeCoverageIgnore |
||
| 156 | */ |
||
| 157 | public static function fileToArray($path = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $content The file content |
||
| 174 | * @return string |
||
| 175 | * @since 0.2.4 |
||
| 176 | * @codeCoverageIgnore |
||
| 177 | */ |
||
| 178 | public static function fileContentToArray($content = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $type The line type |
||
| 198 | * @param array $analysis Array to analyze |
||
| 199 | * @return int |
||
| 200 | * @since 0.2.4 |
||
| 201 | * @codeCoverageIgnore |
||
| 202 | */ |
||
| 203 | public static function getNumberLinesMatching($type, array $analysis) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param $callback |
||
| 218 | * @param array $args |
||
| 219 | * @return void |
||
| 220 | * @since 0.2.4 |
||
| 221 | * @codeCoverageIgnore |
||
| 222 | */ |
||
| 223 | public static function callFuncArray($callback, array $args) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string |
||
| 301 | * @codeCoverageIgnore |
||
| 302 | * @since 0.1.2 |
||
| 303 | */ |
||
| 304 | public function __toString() |
||
| 308 | } |
||
| 309 | |||
| 311 |
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.