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 QueryInterface|Query $query | ||
| 29 | * @param array $config | ||
| 30 | * @return QueryInterface | ||
| 31 | */ | ||
| 32 | public static function configure(QueryInterface $query, $config = []): QueryInterface | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Standard param parsing. | ||
| 56 | * | ||
| 57 | * @param $value | ||
| 58 | * @param $join | ||
| 59 | * @return bool | ||
| 60 | */ | ||
| 61 | public static function parseBaseParam(&$value, &$join): bool | ||
| 79 | |||
| 80 | /** | ||
| 81 | * Attempt to resolve a param value by the value. | ||
| 82 | * Return false if a 'handle' or other string identifier is detected. | ||
| 83 | * | ||
| 84 | * @param $value | ||
| 85 | * @param $operator | ||
| 86 | * @return bool | ||
| 87 | */ | ||
| 88 | public static function findParamValue(&$value, &$operator): bool | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Format the param value so that we return a string w/ a prepended operator. | ||
| 117 | * | ||
| 118 | * @param $value | ||
| 119 | * @param $operator | ||
| 120 | * @return string | ||
| 121 | */ | ||
| 122 | public static function assembleParamValue($value, $operator) | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Attempt to resolve a param value by the value. | ||
| 139 | * Return false if a 'handle' or other string identifier is detected. | ||
| 140 | * | ||
| 141 | * @param $value | ||
| 142 | * @param $operator | ||
| 143 | * @return bool | ||
| 144 | */ | ||
| 145 | public static function prepParamValue(&$value, &$operator): bool | ||
| 170 | |||
| 171 | /** | ||
| 172 | * @param $value | ||
| 173 | * @param string $default | ||
| 174 | * @return mixed|string | ||
| 175 | */ | ||
| 176 | private static function getJoinType(&$value, $default = 'or') | ||
| 193 | |||
| 194 | /** | ||
| 195 | * Attempt to get a numeric value from an object array. | ||
| 196 | * @param $value | ||
| 197 | * @param null $operator | ||
| 198 | * @return mixed|string | ||
| 199 | */ | ||
| 200 | private static function findIdFromObjectArray($value, $operator = null) | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Prepend the operator to a value | ||
| 211 | * | ||
| 212 | * @param $value | ||
| 213 | * @param null $operator | ||
| 214 | * @return string|array | ||
| 215 | */ | ||
| 216 | private static function prependOperator($value, $operator = null) | ||
| 239 | |||
| 240 | /** | ||
| 241 | * Normalizes “empty” values. | ||
| 242 | * | ||
| 243 | * @param string &$value The param value. | ||
| 244 | */ | ||
| 245 | private static function normalizeEmptyValue(&$value) | ||
| 255 | |||
| 256 | /** | ||
| 257 | * Extracts the operator from a DB param and returns it. | ||
| 258 | * | ||
| 259 | * @param string &$value Te param value. | ||
| 260 | * | ||
| 261 | * @return string The operator. | ||
| 262 | */ | ||
| 263 | private static function parseParamOperator(&$value) | ||
| 287 | } | ||
| 288 | 
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: