Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Query extends QueryHelper |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $operators = ['not ', '!=', '<=', '>=', '<', '>', '=']; |
||
32 | |||
33 | /** |
||
34 | * @param ElementQueryInterface $query |
||
35 | * @param array $params |
||
36 | */ |
||
37 | public static function applyOrganizationParam(ElementQueryInterface $query, array $params = []) |
||
54 | |||
55 | /** |
||
56 | * @param $value |
||
57 | * @return array |
||
58 | */ |
||
59 | public static function parseUserValue($value) |
||
92 | |||
93 | /** |
||
94 | * Standard param parsing. |
||
95 | * |
||
96 | * @param $value |
||
97 | * @param $join |
||
98 | * @return bool |
||
99 | */ |
||
100 | public static function parseBaseParam(&$value, &$join) |
||
121 | |||
122 | /** |
||
123 | * Format the param value so that we return a string w/ a prepended operator. |
||
124 | * |
||
125 | * @param $value |
||
126 | * @param $operator |
||
127 | * @return string |
||
128 | */ |
||
129 | public static function assembleParamValue($value, $operator) |
||
143 | |||
144 | /** |
||
145 | * Attempt to resolve a param value by the value. |
||
146 | * Return false if a 'handle' or other string identifier is detected. |
||
147 | * |
||
148 | * @param $value |
||
149 | * @param $operator |
||
150 | * @return bool |
||
151 | */ |
||
152 | public static function findParamValue(&$value, &$operator) |
||
178 | |||
179 | /** |
||
180 | * Attempt to resolve a param value by the value. |
||
181 | * Return false if a 'handle' or other string identifier is detected. |
||
182 | * |
||
183 | * @param $value |
||
184 | * @param $operator |
||
185 | * @return bool |
||
186 | */ |
||
187 | public static function prepParamValue(&$value, &$operator) |
||
212 | |||
213 | /** |
||
214 | * @param $value |
||
215 | * @param string $default |
||
216 | * @return mixed|string |
||
217 | */ |
||
218 | private static function getJoinType(&$value, $default = 'or') |
||
235 | |||
236 | /** |
||
237 | * Attempt to get a numeric value from an object array. |
||
238 | * @param $value |
||
239 | * @param null $operator |
||
240 | * @return mixed|string |
||
241 | */ |
||
242 | private static function findIdFromObjectArray($value, $operator = null) |
||
251 | |||
252 | /** |
||
253 | * Prepend the operator to a value |
||
254 | * |
||
255 | * @param $value |
||
256 | * @param null $operator |
||
257 | * @return string |
||
258 | */ |
||
259 | private static function prependOperator($value, $operator = null) |
||
282 | |||
283 | /** |
||
284 | * Normalizes “empty” values. |
||
285 | * |
||
286 | * @param string &$value The param value. |
||
287 | */ |
||
288 | private static function normalizeEmptyValue(&$value) |
||
298 | |||
299 | /** |
||
300 | * Extracts the operator from a DB param and returns it. |
||
301 | * |
||
302 | * @param string &$value Te param value. |
||
303 | * |
||
304 | * @return string The operator. |
||
305 | */ |
||
306 | private static function parseParamOperator(&$value) |
||
330 | |||
331 | |||
332 | /** |
||
333 | * @param ElementQueryInterface $query |
||
334 | * @param $owner |
||
335 | * |
||
336 | * @return void |
||
337 | */ |
||
338 | private static function applyOrganizationOwnerParam(ElementQueryInterface $query, $owner) |
||
355 | |||
356 | /** |
||
357 | * @param ElementQueryInterface $query |
||
358 | * @param $user |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | private static function applyOrganizationUserParam(ElementQueryInterface $query, $user) |
||
379 | |||
380 | /** |
||
381 | * @param ElementQueryInterface $query |
||
382 | * @param $member |
||
383 | * |
||
384 | * @return void |
||
385 | */ |
||
386 | private static function applyOrganizationMemberParam(ElementQueryInterface $query, $member) |
||
418 | } |
||
419 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: