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 |
||
20 | class QueryHelper |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $operators = ['not ', '!=', '<=', '>=', '<', '>', '=']; |
||
26 | |||
27 | /** |
||
28 | * @param $condition |
||
29 | * @return array |
||
30 | */ |
||
31 | public static function conditionToCriteria($condition) |
||
46 | |||
47 | /** |
||
48 | * @param QueryInterface|Query $query |
||
49 | * @param array $config |
||
50 | * @return QueryInterface |
||
51 | */ |
||
52 | public static function configure(QueryInterface $query, $config = []): QueryInterface |
||
73 | |||
74 | /** |
||
75 | * Standard param parsing. |
||
76 | * |
||
77 | * @param $value |
||
78 | * @param $join |
||
79 | * @return bool |
||
80 | */ |
||
81 | public static function parseBaseParam(&$value, &$join): bool |
||
99 | |||
100 | /** |
||
101 | * Attempt to resolve a param value by the value. |
||
102 | * Return false if a 'handle' or other string identifier is detected. |
||
103 | * |
||
104 | * @param $value |
||
105 | * @param $operator |
||
106 | * @return bool |
||
107 | */ |
||
108 | public static function findParamValue(&$value, &$operator): bool |
||
133 | |||
134 | /** |
||
135 | * Format the param value so that we return a string w/ a prepended operator. |
||
136 | * |
||
137 | * @param $value |
||
138 | * @param $operator |
||
139 | * @param string|int|mixed $defaultValue |
||
140 | * @return array|string |
||
141 | */ |
||
142 | public static function assembleParamValue($value, $operator, $defaultValue = ':default:') |
||
158 | |||
159 | /** |
||
160 | * Attempt to resolve a param value by the value. |
||
161 | * Return false if a 'handle' or other string identifier is detected. |
||
162 | * |
||
163 | * @param $value |
||
164 | * @param $operator |
||
165 | * @return bool |
||
166 | */ |
||
167 | public static function prepParamValue(&$value, &$operator): bool |
||
192 | |||
193 | /** |
||
194 | * @param $value |
||
195 | * @param string $default |
||
196 | * @return mixed|string |
||
197 | */ |
||
198 | private static function getJoinType(&$value, $default = 'or') |
||
215 | |||
216 | /** |
||
217 | * Attempt to get a numeric value from an object array. |
||
218 | * @param $value |
||
219 | * @param null $operator |
||
220 | * @return mixed|string |
||
221 | */ |
||
222 | private static function findIdFromObjectArray($value, $operator = null) |
||
230 | |||
231 | /** |
||
232 | * Prepend the operator to a value |
||
233 | * |
||
234 | * @param $value |
||
235 | * @param null $operator |
||
236 | * @return string|array |
||
237 | */ |
||
238 | private static function prependOperator($value, $operator = null) |
||
261 | |||
262 | /** |
||
263 | * Normalizes “empty” values. |
||
264 | * |
||
265 | * @param string &$value The param value. |
||
266 | */ |
||
267 | private static function normalizeEmptyValue(&$value) |
||
277 | |||
278 | /** |
||
279 | * Extracts the operator from a DB param and returns it. |
||
280 | * |
||
281 | * @param string &$value Te param value. |
||
282 | * |
||
283 | * @return string The operator. |
||
284 | */ |
||
285 | private static function parseParamOperator(&$value) |
||
309 | } |
||
310 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: