Complex classes like QueryOperations 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 QueryOperations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class QueryOperations |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * An instance of the record wrapper being used. |
||
| 40 | * |
||
| 41 | * @var RecordWrapper |
||
| 42 | */ |
||
| 43 | private $wrapper; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * An instance of the driver adapter used in the database connection. |
||
| 47 | * |
||
| 48 | * @var DriverAdapter |
||
| 49 | */ |
||
| 50 | private $adapter; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * An instance of query parameters used to perform the various queries. |
||
| 54 | * |
||
| 55 | * @var QueryParameters |
||
| 56 | */ |
||
| 57 | private $queryParameters; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The name of a method initialized through a dynamic method waiting to be executed. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $pendingMethod; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Regular expressions for matching dynamic methods. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $dynamicMethods = [ |
||
| 72 | '/(?<method>filterBy)(?<variable>[A-Z][A-Za-z]+){1}/', |
||
| 73 | '/(?<method>sort)(?<direction>Asc|Desc)?(By)(?<variable>[A-Z][A-Za-z]+){1}/', |
||
| 74 | '/(?<method>fetch)(?<first>First)?(With)(?<variable>[A-Za-z]+)/', |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * An instance of the DataOperations class used for filtered deletes. |
||
| 79 | * |
||
| 80 | * @var DataOperations |
||
| 81 | */ |
||
| 82 | private $dataOperations; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * An instance of the Driver class used for establishing database connections. |
||
| 86 | * |
||
| 87 | * @var Driver |
||
| 88 | */ |
||
| 89 | private $driver; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * QueryOperations constructor. |
||
| 93 | * |
||
| 94 | * @param RecordWrapper $wrapper |
||
| 95 | * @param DataOperations $dataOperations |
||
| 96 | * @param Driver $driver |
||
| 97 | * |
||
| 98 | * @internal param DriverAdapter $adapter |
||
| 99 | */ |
||
| 100 | 34 | public function __construct(RecordWrapper $wrapper, DataOperations $dataOperations, Driver $driver) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Fetches items from the database. |
||
| 110 | * |
||
| 111 | * @param int|array|QueryParameters $query |
||
| 112 | * |
||
| 113 | * @return RecordWrapper |
||
|
|
|||
| 114 | */ |
||
| 115 | 24 | public function doFetch($query = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * The method takes multiple types of arguments and converts it to a QueryParametersObject. |
||
| 131 | * When this method receives null, it returns a new instance of QueryParameters. When it receives an integer, it |
||
| 132 | * returns a QueryParameters object that points the primary key to the integer. When it receives an associative |
||
| 133 | * array, it builds a series of conditions with array key-value pairs. |
||
| 134 | * |
||
| 135 | * @param int|array|QueryParameters $arg |
||
| 136 | * @param bool $instantiate |
||
| 137 | * |
||
| 138 | * @return QueryParameters |
||
| 139 | */ |
||
| 140 | 26 | private function buildFetchQueryParameters($arg, $instantiate = true) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Creates a new instance of the QueryParameters if required or just returns an already instance. |
||
| 163 | * |
||
| 164 | * @param bool $forceInstantiation |
||
| 165 | * |
||
| 166 | * @return QueryParameters |
||
| 167 | */ |
||
| 168 | 32 | private function getQueryParameters($forceInstantiation = true) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Clears up the query parameters. |
||
| 179 | */ |
||
| 180 | 28 | private function resetQueryParameters() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Performs the fetch operation and returns just the first item. |
||
| 187 | * |
||
| 188 | * @param mixed $id |
||
| 189 | * |
||
| 190 | * @return RecordWrapper |
||
| 191 | */ |
||
| 192 | 10 | public function doFetchFirst($id = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Set the fields that should be returned for each record. |
||
| 201 | * |
||
| 202 | * @return RecordWrapper |
||
| 203 | */ |
||
| 204 | 12 | public function doFields() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Sort the query by a given field in a given directory. |
||
| 222 | * |
||
| 223 | * @param string $field |
||
| 224 | * @param string $direction |
||
| 225 | */ |
||
| 226 | public function doSortBy($field, $direction = 'ASC') |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param mixed $arguments |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 10 | private function getFilter($arguments) |
|
| 248 | |||
| 249 | 6 | public function doFilter() |
|
| 263 | |||
| 264 | 4 | public function doFilterBy() |
|
| 272 | |||
| 273 | 6 | public function doUpdate($data) |
|
| 281 | |||
| 282 | 2 | public function doDelete($args = null) |
|
| 308 | |||
| 309 | 10 | public function runDynamicMethod($arguments) |
|
| 331 | |||
| 332 | 10 | public function initDynamicMethod($method) |
|
| 346 | |||
| 347 | public function doCount($query = null) |
||
| 351 | |||
| 352 | public function doLimit($numItems) |
||
| 358 | |||
| 359 | public function doOffset($offset) |
||
| 365 | |||
| 366 | public function doWith($model) |
||
| 375 | } |
||
| 376 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.