Complex classes like QueryHelper 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 QueryHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class QueryHelper |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $operators = ['not ', '!=', '<=', '>=', '<', '>', '=']; |
||
25 | |||
26 | /** |
||
27 | * @param $condition |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function conditionToCriteria($condition) |
||
45 | |||
46 | /** |
||
47 | * @param QueryInterface|Query $query |
||
48 | * @param array $config |
||
49 | * @return QueryInterface |
||
50 | */ |
||
51 | public static function configure(QueryInterface $query, $config = []): QueryInterface |
||
72 | |||
73 | /** |
||
74 | * Prepares a query params input value to be used as a condition. This will attempt to resolve an object |
||
75 | * or look one up based on a 'handle' or other unique string identifier (via the lookup callable). |
||
76 | * |
||
77 | * ```php |
||
78 | * |
||
79 | * [ |
||
80 | * 'and', |
||
81 | * 'not' => [ |
||
82 | * 4, elementFiveHandle, $someElementWithIdOfSix |
||
83 | * ] |
||
84 | * ] |
||
85 | * ``` |
||
86 | * Would result in a query condition as `NOT IN (4, 5, 6)`. |
||
87 | * |
||
88 | * @param $value |
||
89 | * @param callable|null $lookup |
||
90 | * @return array|string |
||
91 | */ |
||
92 | public static function prepareParam($value, callable $lookup = null) |
||
131 | |||
132 | /** |
||
133 | * @param $key |
||
134 | * @param $value |
||
135 | * @param callable|null $lookup |
||
136 | * @return array |
||
137 | */ |
||
138 | protected static function prepareParamValue($key, $value, callable $lookup = null): array |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Standard param parsing. |
||
178 | * |
||
179 | * @param $value |
||
180 | * @param $join |
||
181 | * @return bool |
||
182 | * |
||
183 | * @deprecated |
||
184 | */ |
||
185 | public static function parseBaseParam(&$value, &$join): bool |
||
203 | |||
204 | /** |
||
205 | * Attempt to resolve a param value by the value. |
||
206 | * Return false if a 'handle' or other string identifier is detected. |
||
207 | * |
||
208 | * @param $value |
||
209 | * @param $operator |
||
210 | * @return bool |
||
211 | * |
||
212 | * @deprecated |
||
213 | */ |
||
214 | public static function findParamValue(&$value, &$operator): bool |
||
239 | |||
240 | /** |
||
241 | * Format the param value so that we return a string w/ a prepended operator. |
||
242 | * |
||
243 | * @param $value |
||
244 | * @param $operator |
||
245 | * @param string|int|mixed $defaultValue |
||
246 | * @return array|string |
||
247 | * |
||
248 | * @deprecated |
||
249 | */ |
||
250 | public static function assembleParamValue($value, $operator, $defaultValue = ':default:') |
||
266 | |||
267 | /** |
||
268 | * Attempt to resolve a param value by the value. |
||
269 | * Return false if a 'handle' or other string identifier is detected. |
||
270 | * |
||
271 | * @param $value |
||
272 | * @param $operator |
||
273 | * @return bool |
||
274 | * |
||
275 | * @deprecated |
||
276 | */ |
||
277 | public static function prepParamValue(&$value, &$operator): bool |
||
302 | |||
303 | /** |
||
304 | * @param $value |
||
305 | * @param string $default |
||
306 | * @return mixed|string |
||
307 | * |
||
308 | * @deprecated |
||
309 | */ |
||
310 | private static function getJoinType(&$value, $default = 'or') |
||
327 | |||
328 | /** |
||
329 | * Attempt to get a numeric value from an object array. |
||
330 | * @param $value |
||
331 | * @param null $operator |
||
332 | * @return mixed|string |
||
333 | * |
||
334 | * @deprecated |
||
335 | */ |
||
336 | private static function findIdFromObjectArray($value, $operator = null) |
||
344 | |||
345 | /** |
||
346 | * Prepend the operator to a value |
||
347 | * |
||
348 | * @param $value |
||
349 | * @param null $operator |
||
350 | * @return string|array |
||
351 | * |
||
352 | * @deprecated |
||
353 | */ |
||
354 | private static function prependOperator($value, $operator = null) |
||
377 | |||
378 | /** |
||
379 | * Normalizes “empty” values. |
||
380 | * |
||
381 | * @param string &$value The param value. |
||
382 | * |
||
383 | * @deprecated |
||
384 | */ |
||
385 | private static function normalizeEmptyValue(&$value) |
||
395 | |||
396 | /** |
||
397 | * Extracts the operator from a DB param and returns it. |
||
398 | * |
||
399 | * @param string &$value Te param value. |
||
400 | * |
||
401 | * @return string The operator. |
||
402 | * |
||
403 | * @deprecated |
||
404 | */ |
||
405 | private static function parseParamOperator(&$value) |
||
429 | } |
||
430 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.