Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Helper |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Places item of extra columns into results by care of their order. |
||
| 14 | * |
||
| 15 | * @param array $item |
||
| 16 | * @param array $array |
||
| 17 | * @return array |
||
| 18 | */ |
||
| 19 | public static function includeInArray($item, $array) |
||
| 20 | { |
||
| 21 | if (self::isItemOrderInvalid($item, $array)) { |
||
| 22 | return array_merge($array, [$item['name'] => $item['content']]); |
||
| 23 | } |
||
| 24 | |||
| 25 | $count = 0; |
||
| 26 | $last = $array; |
||
| 27 | $first = []; |
||
| 28 | foreach ($array as $key => $value) { |
||
| 29 | if ($count == $item['order']) { |
||
| 30 | return array_merge($first, [$item['name'] => $item['content']], $last); |
||
| 31 | } |
||
| 32 | |||
| 33 | unset($last[$key]); |
||
| 34 | $first[$key] = $value; |
||
| 35 | |||
| 36 | $count++; |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Check if item order is valid. |
||
| 42 | * |
||
| 43 | * @param array $item |
||
| 44 | * @param array $array |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | protected static function isItemOrderInvalid($item, $array) |
||
| 48 | { |
||
| 49 | return $item['order'] === false || $item['order'] >= count($array); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Determines if content is callable or blade string, processes and returns. |
||
| 54 | * |
||
| 55 | * @param mixed $content Pre-processed content |
||
| 56 | * @param array $data data to use with blade template |
||
| 57 | * @param mixed $param parameter to call with callable |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | public static function compileContent($content, array $data, $param) |
||
| 61 | { |
||
| 62 | if (is_string($content)) { |
||
| 63 | return static::compileBlade($content, static::getMixedValue($data, $param)); |
||
| 64 | } elseif (is_callable($content)) { |
||
| 65 | return $content($param); |
||
| 66 | } |
||
| 67 | |||
| 68 | return $content; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Parses and compiles strings by using Blade Template System. |
||
| 73 | * |
||
| 74 | * @param string $str |
||
| 75 | * @param array $data |
||
| 76 | * @return mixed |
||
| 77 | * @throws \Exception |
||
| 78 | */ |
||
| 79 | public static function compileBlade($str, $data = []) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get a mixed value of custom data and the parameters. |
||
| 95 | * |
||
| 96 | * @param array $data |
||
| 97 | * @param mixed $param |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public static function getMixedValue(array $data, $param) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Cast the parameter into an array. |
||
| 117 | * |
||
| 118 | * @param mixed $param |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public static function castToArray($param) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get equivalent or method of query builder. |
||
| 138 | * |
||
| 139 | * @param string $method |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public static function getOrMethod($method) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Converts array object values to associative array. |
||
| 153 | * |
||
| 154 | * @param mixed $row |
||
| 155 | * @param array $filters |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public static function convertToArray($row, $filters = []) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $data |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public static function transform(array $data) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Transform row data into an array. |
||
| 188 | * |
||
| 189 | * @param mixed $row |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | protected static function transformRow($row) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Build parameters depending on # of arguments passed. |
||
| 211 | * |
||
| 212 | * @param array $args |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public static function buildParameters(array $args) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Replace all pattern occurrences with keyword. |
||
| 235 | * |
||
| 236 | * @param array $subject |
||
| 237 | * @param string $keyword |
||
| 238 | * @param string $pattern |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get column name from string. |
||
| 257 | * |
||
| 258 | * @param string $str |
||
| 259 | * @param bool $wantsAlias |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public static function extractColumnName($str, $wantsAlias) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Adds % wildcards to the given string. |
||
| 283 | * |
||
| 284 | * @param string $str |
||
| 285 | * @param bool $lowercase |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public static function wildcardLikeString($str, $lowercase = true) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Adds wildcards to the given string. |
||
| 295 | * |
||
| 296 | * @param string $str |
||
| 297 | * @param string $wildcard |
||
| 298 | * @param bool $lowercase |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public static function wildcardString($str, $wildcard, $lowercase = true) |
||
| 318 | } |
||
| 319 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: