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 = []) |
||
103 | |||
104 | /** |
||
105 | * Get a mixed value of custom data and the parameters. |
||
106 | * |
||
107 | * @param array $data |
||
108 | * @param mixed $param |
||
109 | * @return array |
||
110 | */ |
||
111 | public static function getMixedValue(array $data, $param) |
||
125 | |||
126 | /** |
||
127 | * Cast the parameter into an array. |
||
128 | * |
||
129 | * @param mixed $param |
||
130 | * @return array |
||
131 | */ |
||
132 | public static function castToArray($param) |
||
146 | |||
147 | /** |
||
148 | * Get equivalent or method of query builder. |
||
149 | * |
||
150 | * @param string $method |
||
151 | * @return string |
||
152 | */ |
||
153 | public static function getOrMethod($method) |
||
161 | |||
162 | /** |
||
163 | * Wrap value depending on database type. |
||
164 | * |
||
165 | * @param string $database |
||
166 | * @param string $value |
||
167 | * @return string |
||
168 | */ |
||
169 | public static function wrapDatabaseValue($database, $value) |
||
179 | |||
180 | /** |
||
181 | * Database column wrapper. |
||
182 | * |
||
183 | * @param string $database |
||
184 | * @param string $key |
||
185 | * @param string $column |
||
186 | * @return string |
||
187 | */ |
||
188 | public static function wrapDatabaseColumn($database, $key, $column) |
||
210 | |||
211 | /** |
||
212 | * Converts array object values to associative array. |
||
213 | * |
||
214 | * @param mixed $row |
||
215 | * @return array |
||
216 | */ |
||
217 | public static function convertToArray($row) |
||
228 | |||
229 | /** |
||
230 | * @param array $data |
||
231 | * @return array |
||
232 | */ |
||
233 | public static function transform(array $data) |
||
239 | |||
240 | /** |
||
241 | * Transform row data into an array. |
||
242 | * |
||
243 | * @param mixed $row |
||
244 | * @return array |
||
245 | */ |
||
246 | protected static function transformRow($row) |
||
262 | |||
263 | /** |
||
264 | * Build parameters depending on # of arguments passed. |
||
265 | * |
||
266 | * @param array $args |
||
267 | * @return array |
||
268 | */ |
||
269 | public static function buildParameters(array $args) |
||
286 | |||
287 | /** |
||
288 | * Replace all pattern occurrences with keyword |
||
289 | * |
||
290 | * @param array $subject |
||
291 | * @param string $keyword |
||
292 | * @param string $pattern |
||
293 | * @return array |
||
294 | */ |
||
295 | public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
||
308 | |||
309 | /** |
||
310 | * Get column name from string. |
||
311 | * |
||
312 | * @param string $str |
||
313 | * @param bool $wantsAlias |
||
314 | * @return string |
||
315 | */ |
||
316 | public static function extractColumnName($str, $wantsAlias) |
||
334 | |||
335 | /** |
||
336 | * Adds % wildcards to the given string. |
||
337 | * |
||
338 | * @param string $str |
||
339 | * @param bool $lowercase |
||
340 | * @return string |
||
341 | */ |
||
342 | public static function wildcardLikeString($str, $lowercase = true) |
||
359 | } |
||
360 |