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 |
||
| 17 | class Helper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Places item of extra columns into results by care of their order. |
||
| 21 | * |
||
| 22 | * @param array $item |
||
| 23 | * @param array $array |
||
| 24 | * @return array |
||
| 25 | */ |
||
| 26 | public static function includeInArray($item, $array) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Check if item order is valid. |
||
| 49 | * |
||
| 50 | * @param array $item |
||
| 51 | * @param array $array |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | protected static function isItemOrderInvalid($item, $array) |
||
| 55 | { |
||
| 56 | return $item['order'] === false || $item['order'] >= count($array); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Determines if content is callable or blade string, processes and returns. |
||
| 61 | * |
||
| 62 | * @param string|callable $content Pre-processed content |
||
| 63 | * @param array $data data to use with blade template |
||
| 64 | * @param mixed $param parameter to call with callable |
||
| 65 | * @return string Processed content |
||
| 66 | */ |
||
| 67 | public static function compileContent($content, array $data, $param) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Parses and compiles strings by using Blade Template System. |
||
| 80 | * |
||
| 81 | * @param string $str |
||
| 82 | * @param array $data |
||
| 83 | * @return string |
||
| 84 | * @throws \Exception |
||
| 85 | */ |
||
| 86 | public static function compileBlade($str, $data = []) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get a mixed value of custom data and the parameters. |
||
| 113 | * |
||
| 114 | * @param array $data |
||
| 115 | * @param mixed $param |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public static function getMixedValue(array $data, $param) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Cast the parameter into an array. |
||
| 135 | * |
||
| 136 | * @param mixed $param |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public static function castToArray($param) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get equivalent or method of query builder. |
||
| 156 | * |
||
| 157 | * @param string $method |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public static function getOrMethod($method) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Wrap value depending on database type. |
||
| 171 | * |
||
| 172 | * @param string $database |
||
| 173 | * @param string $value |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public static function wrapDatabaseValue($database, $value) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Database column wrapper. |
||
| 189 | * |
||
| 190 | * @param string $database |
||
| 191 | * @param string $key |
||
| 192 | * @param string $column |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public static function wrapDatabaseColumn($database, $key, $column) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Converts array object values to associative array. |
||
| 220 | * |
||
| 221 | * @param mixed $row |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public static function convertToArray($row) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param array $data |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public static function transform(array $data) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Transform row data into an array. |
||
| 249 | * |
||
| 250 | * @param mixed $row |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | protected static function transformRow($row) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Build parameters depending on # of arguments passed. |
||
| 272 | * |
||
| 273 | * @param array $args |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public static function buildParameters(array $args) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Replace all pattern occurrences with keyword |
||
| 296 | * |
||
| 297 | * @param array $subject |
||
| 298 | * @param string $keyword |
||
| 299 | * @param string $pattern |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get column name from string. |
||
| 318 | * |
||
| 319 | * @param string $str |
||
| 320 | * @param bool $wantsAlias |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public static function extractColumnName($str, $wantsAlias) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Adds % wildcards to the given string. |
||
| 344 | * |
||
| 345 | * @param string $str |
||
| 346 | * @param bool $lowercase |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public static function wildcardLikeString($str, $lowercase = true) |
||
| 366 | } |
||
| 367 |
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: