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 |
||
11 | class Helper |
||
12 | { |
||
13 | /** |
||
14 | * Places item of extra columns into results by care of their order. |
||
15 | * |
||
16 | * @param $item |
||
17 | * @param $array |
||
18 | * @return array |
||
19 | */ |
||
20 | public static function includeInArray($item, $array) |
||
40 | |||
41 | /** |
||
42 | * Check if item order is valid. |
||
43 | * |
||
44 | * @param array $item |
||
45 | * @param array $array |
||
46 | * @return bool |
||
47 | */ |
||
48 | protected static function isItemOrderInvalid($item, $array) |
||
52 | |||
53 | /** |
||
54 | * Determines if content is callable or blade string, processes and returns. |
||
55 | * |
||
56 | * @param string|callable $content Pre-processed content |
||
57 | * @param array $data data to use with blade template |
||
58 | * @param mixed $param parameter to call with callable |
||
59 | * @return string Processed content |
||
60 | */ |
||
61 | public static function compileContent($content, array $data, $param) |
||
71 | |||
72 | /** |
||
73 | * Parses and compiles strings by using Blade Template System. |
||
74 | * |
||
75 | * @param string $str |
||
76 | * @param array $data |
||
77 | * @return string |
||
78 | * @throws \Exception |
||
79 | */ |
||
80 | public static function compileBlade($str, $data = []) |
||
104 | |||
105 | /** |
||
106 | * @param array $data |
||
107 | * @param mixed $param |
||
108 | * @return array |
||
109 | */ |
||
110 | public static function getMixedValue(array $data, $param) |
||
122 | |||
123 | /** |
||
124 | * @param $param |
||
125 | * @return array |
||
126 | */ |
||
127 | public static function castToArray($param) |
||
137 | |||
138 | /** |
||
139 | * Get equivalent or method of query builder. |
||
140 | * |
||
141 | * @param string $method |
||
142 | * @return string |
||
143 | */ |
||
144 | public static function getOrMethod($method) |
||
152 | |||
153 | /** |
||
154 | * Wrap value depending on database type. |
||
155 | * |
||
156 | * @param string $database |
||
157 | * @param string $value |
||
158 | * @return string |
||
159 | */ |
||
160 | public static function wrapDatabaseValue($database, $value) |
||
170 | |||
171 | /** |
||
172 | * Database column wrapper |
||
173 | * |
||
174 | * @param string $database |
||
175 | * @param string $key |
||
176 | * @param string $column |
||
177 | * @return string |
||
178 | */ |
||
179 | public static function wrapDatabaseColumn($database, $key, $column) |
||
201 | |||
202 | /** |
||
203 | * Converts array object values to associative array. |
||
204 | * |
||
205 | * @param mixed $row |
||
206 | * @return array |
||
207 | */ |
||
208 | public static function convertToArray($row) |
||
219 | |||
220 | /** |
||
221 | * @param array $data |
||
222 | * @return array |
||
223 | */ |
||
224 | public static function transform(array $data) |
||
230 | |||
231 | /** |
||
232 | * @param $row |
||
233 | * @return mixed |
||
234 | */ |
||
235 | protected static function transformRow($row) |
||
251 | |||
252 | /** |
||
253 | * Build parameters depending on # of arguments passed. |
||
254 | * |
||
255 | * @param array $args |
||
256 | * @return array |
||
257 | */ |
||
258 | public static function buildParameters(array $args) |
||
275 | |||
276 | /** |
||
277 | * Replace all pattern occurrences with keyword |
||
278 | * |
||
279 | * @param array $subject |
||
280 | * @param string $keyword |
||
281 | * @param string $pattern |
||
282 | * @return array |
||
283 | */ |
||
284 | public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
||
297 | } |
||
298 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.