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 | * Standard param parsing. |
||
| 75 | * |
||
| 76 | * @param $value |
||
| 77 | * @param $join |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public static function parseBaseParam(&$value, &$join): bool |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Attempt to resolve a param value by the value. |
||
| 101 | * Return false if a 'handle' or other string identifier is detected. |
||
| 102 | * |
||
| 103 | * @param $value |
||
| 104 | * @param $operator |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public static function findParamValue(&$value, &$operator): bool |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Format the param value so that we return a string w/ a prepended operator. |
||
| 135 | * |
||
| 136 | * @param $value |
||
| 137 | * @param $operator |
||
| 138 | * @param string|int|mixed $defaultValue |
||
| 139 | * @return array|string |
||
| 140 | */ |
||
| 141 | public static function assembleParamValue($value, $operator, $defaultValue = ':default:') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Attempt to resolve a param value by the value. |
||
| 160 | * Return false if a 'handle' or other string identifier is detected. |
||
| 161 | * |
||
| 162 | * @param $value |
||
| 163 | * @param $operator |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public static function prepParamValue(&$value, &$operator): bool |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $value |
||
| 194 | * @param string $default |
||
| 195 | * @return mixed|string |
||
| 196 | */ |
||
| 197 | private static function getJoinType(&$value, $default = 'or') |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Attempt to get a numeric value from an object array. |
||
| 217 | * @param $value |
||
| 218 | * @param null $operator |
||
| 219 | * @return mixed|string |
||
| 220 | */ |
||
| 221 | private static function findIdFromObjectArray($value, $operator = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Prepend the operator to a value |
||
| 232 | * |
||
| 233 | * @param $value |
||
| 234 | * @param null $operator |
||
| 235 | * @return string|array |
||
| 236 | */ |
||
| 237 | private static function prependOperator($value, $operator = null) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Normalizes “empty” values. |
||
| 263 | * |
||
| 264 | * @param string &$value The param value. |
||
| 265 | */ |
||
| 266 | private static function normalizeEmptyValue(&$value) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Extracts the operator from a DB param and returns it. |
||
| 279 | * |
||
| 280 | * @param string &$value Te param value. |
||
| 281 | * |
||
| 282 | * @return string The operator. |
||
| 283 | */ |
||
| 284 | private static function parseParamOperator(&$value) |
||
| 285 | { |
||
| 286 | foreach (static::$operators as $testOperator) { |
||
| 287 | // Does the value start with this operator? |
||
| 288 | $operatorLength = strlen($testOperator); |
||
| 289 | |||
| 309 |