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 |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Format the param value so that we return a string w/ a prepended operator. |
||
| 116 | * |
||
| 117 | * @param $value |
||
| 118 | * @param $operator |
||
| 119 | * @param string|int|mixed $defaultValue |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public static function assembleParamValue($value, $operator, $defaultValue = ':default:') |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Attempt to resolve a param value by the value. |
||
| 141 | * Return false if a 'handle' or other string identifier is detected. |
||
| 142 | * |
||
| 143 | * @param $value |
||
| 144 | * @param $operator |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function prepParamValue(&$value, &$operator): bool |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param $value |
||
| 175 | * @param string $default |
||
| 176 | * @return mixed|string |
||
| 177 | */ |
||
| 178 | private static function getJoinType(&$value, $default = 'or') |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Attempt to get a numeric value from an object array. |
||
| 198 | * @param $value |
||
| 199 | * @param null $operator |
||
| 200 | * @return mixed|string |
||
| 201 | */ |
||
| 202 | private static function findIdFromObjectArray($value, $operator = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prepend the operator to a value |
||
| 213 | * |
||
| 214 | * @param $value |
||
| 215 | * @param null $operator |
||
| 216 | * @return string|array |
||
| 217 | */ |
||
| 218 | private static function prependOperator($value, $operator = null) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Normalizes “empty” values. |
||
| 244 | * |
||
| 245 | * @param string &$value The param value. |
||
| 246 | */ |
||
| 247 | private static function normalizeEmptyValue(&$value) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Extracts the operator from a DB param and returns it. |
||
| 260 | * |
||
| 261 | * @param string &$value Te param value. |
||
| 262 | * |
||
| 263 | * @return string The operator. |
||
| 264 | */ |
||
| 265 | private static function parseParamOperator(&$value) |
||
| 289 | } |
||
| 290 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: