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 |
||
| 9 | class Helper |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Places item of extra columns into results by care of their order. |
||
| 13 | * |
||
| 14 | * @param array $item |
||
| 15 | * @param array $array |
||
| 16 | * @return array |
||
| 17 | */ |
||
| 18 | public static function includeInArray($item, $array) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Check if item order is valid. |
||
| 41 | * |
||
| 42 | * @param array $item |
||
| 43 | * @param array $array |
||
| 44 | * @return bool |
||
| 45 | */ |
||
| 46 | protected static function isItemOrderInvalid($item, $array) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Determines if content is callable or blade string, processes and returns. |
||
| 53 | * |
||
| 54 | * @param mixed $content Pre-processed content |
||
| 55 | * @param array $data data to use with blade template |
||
| 56 | * @param mixed $param parameter to call with callable |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | public static function compileContent($content, array $data, $param) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Parses and compiles strings by using Blade Template System. |
||
| 72 | * |
||
| 73 | * @param string $str |
||
| 74 | * @param array $data |
||
| 75 | * @return mixed |
||
| 76 | * @throws \Exception |
||
| 77 | */ |
||
| 78 | public static function compileBlade($str, $data = []) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get a mixed value of custom data and the parameters. |
||
| 94 | * |
||
| 95 | * @param array $data |
||
| 96 | * @param mixed $param |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public static function getMixedValue(array $data, $param) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Cast the parameter into an array. |
||
| 116 | * |
||
| 117 | * @param mixed $param |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public static function castToArray($param) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get equivalent or method of query builder. |
||
| 137 | * |
||
| 138 | * @param string $method |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public static function getOrMethod($method) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Converts array object values to associative array. |
||
| 152 | * |
||
| 153 | * @param mixed $row |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public static function convertToArray($row) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $data |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public static function transform(array $data) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Transform row data into an array. |
||
| 181 | * |
||
| 182 | * @param mixed $row |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | protected static function transformRow($row) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Build parameters depending on # of arguments passed. |
||
| 204 | * |
||
| 205 | * @param array $args |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public static function buildParameters(array $args) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Replace all pattern occurrences with keyword |
||
| 228 | * |
||
| 229 | * @param array $subject |
||
| 230 | * @param string $keyword |
||
| 231 | * @param string $pattern |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get column name from string. |
||
| 250 | * |
||
| 251 | * @param string $str |
||
| 252 | * @param bool $wantsAlias |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public static function extractColumnName($str, $wantsAlias) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds % wildcards to the given string. |
||
| 276 | * |
||
| 277 | * @param string $str |
||
| 278 | * @param bool $lowercase |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function wildcardLikeString($str, $lowercase = true) |
||
| 298 | } |
||
| 299 |
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: