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 |
||
| 37 | class QueryOperations |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * An instance of the record wrapper being used. |
||
| 42 | * |
||
| 43 | * @var RecordWrapper |
||
| 44 | */ |
||
| 45 | private $wrapper; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * An instance of the driver adapter used in the database connection. |
||
| 49 | * |
||
| 50 | * @var DriverAdapter |
||
| 51 | */ |
||
| 52 | private $adapter; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * An instance of query parameters used to perform the various queries. |
||
| 56 | * |
||
| 57 | * @var QueryParameters |
||
| 58 | */ |
||
| 59 | private $queryParameters; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The name of a method initialized through a dynamic method waiting to be executed. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $pendingMethod; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Regular expressions for matching dynamic methods. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $dynamicMethods = [ |
||
| 74 | "/(?<method>filterBy)(?<variable>[A-Z][A-Za-z]+){1}/", |
||
| 75 | "/(?<method>sort)(?<direction>Asc|Desc)?(By)(?<variable>[A-Z][A-Za-z]+){1}/", |
||
| 76 | "/(?<method>fetch)(?<first>First)?(With)(?<variable>[A-Za-z]+)/" |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * An instance of the DataOperations class used for filtered deletes. |
||
| 81 | * |
||
| 82 | * @var DataOperations |
||
| 83 | */ |
||
| 84 | private $dataOperations; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * An instance of the Driver class used for establishing database connections. |
||
| 88 | * |
||
| 89 | * @var Driver |
||
| 90 | */ |
||
| 91 | private $driver; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * QueryOperations constructor |
||
| 95 | * |
||
| 96 | * @param RecordWrapper $wrapper |
||
| 97 | * @param DataOperations $dataOperations |
||
| 98 | * @param Driver $driver |
||
| 99 | * @internal param DriverAdapter $adapter |
||
| 100 | */ |
||
| 101 | 34 | public function __construct(RecordWrapper $wrapper, DataOperations $dataOperations, Driver $driver) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Fetches items from the database. |
||
| 111 | * |
||
| 112 | * @param int|array|QueryParameters $query |
||
| 113 | * @return RecordWrapper |
||
| 114 | */ |
||
| 115 | 24 | public function doFetch($query = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * The method takes multiple types of arguments and converts it to a QueryParametersObject. |
||
| 126 | * When this method receives null, it returns a new instance of QueryParameters. When it receives an integer, it |
||
| 127 | * returns a QueryParameters object that points the primary key to the integer. When it receives an associative |
||
| 128 | * array, it builds a series of conditions with array key-value pairs. |
||
| 129 | * |
||
| 130 | * @param int|array|QueryParameters $arg |
||
| 131 | * @param bool $instantiate |
||
| 132 | * @return QueryParameters |
||
| 133 | */ |
||
| 134 | 26 | private function buildFetchQueryParameters($arg, $instantiate = true) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Creates a new instance of the QueryParameters if required or just returns an already instance. |
||
| 157 | * |
||
| 158 | * @param bool $forceInstantiation |
||
| 159 | * @return QueryParameters |
||
| 160 | */ |
||
| 161 | 32 | private function getQueryParameters($forceInstantiation = true) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Clears up the query parameters. |
||
| 171 | */ |
||
| 172 | 32 | private function resetQueryParameters() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Performs the fetch operation and returns just the first item. |
||
| 179 | * |
||
| 180 | * @param mixed $id |
||
| 181 | * @return RecordWrapper |
||
| 182 | */ |
||
| 183 | 10 | public function doFetchFirst($id = null) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Set the fields that should be returned for each record. |
||
| 191 | * |
||
| 192 | * @return RecordWrapper |
||
| 193 | */ |
||
| 194 | 12 | public function doFields() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Sort the query by a given field in a given directory. |
||
| 211 | * |
||
| 212 | * @param string $field |
||
| 213 | * @param string $direction |
||
| 214 | */ |
||
| 215 | public function doSortBy($field, $direction = 'ASC') |
||
| 219 | |||
| 220 | /** |
||
| 221 | * |
||
| 222 | * |
||
| 223 | * @param mixed $arguments |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 10 | private function getFilter($arguments) |
|
| 237 | |||
| 238 | 6 | public function doFilter() |
|
| 251 | |||
| 252 | 4 | public function doFilterBy() |
|
| 259 | |||
| 260 | 6 | public function doUpdate($data) |
|
| 268 | |||
| 269 | 2 | public function doDelete($args = null) |
|
| 295 | |||
| 296 | 10 | public function runDynamicMethod($arguments) |
|
| 315 | |||
| 316 | 10 | public function initDynamicMethod($method) |
|
| 330 | |||
| 331 | 4 | public function doCount($query = null) |
|
| 335 | |||
| 336 | public function doLimit($numItems) |
||
| 341 | |||
| 342 | public function doOffset($offset) |
||
| 347 | |||
| 348 | public function doWith($model) |
||
| 353 | |||
| 354 | } |
||
| 355 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: