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 |
||
39 | class QueryOperations |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * An instance of the record wrapper being used. |
||
44 | * |
||
45 | * @var RecordWrapper |
||
46 | */ |
||
47 | private $wrapper; |
||
48 | |||
49 | /** |
||
50 | * An instance of the driver adapter used in the database connection. |
||
51 | * |
||
52 | * @var DriverAdapter |
||
53 | */ |
||
54 | private $adapter; |
||
55 | |||
56 | /** |
||
57 | * An instance of query parameters used to perform the various queries. |
||
58 | * |
||
59 | * @var QueryParameters |
||
60 | */ |
||
61 | private $queryParameters; |
||
62 | |||
63 | /** |
||
64 | * The name of a method initialized through a dynamic method waiting to be executed. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $pendingMethod; |
||
69 | |||
70 | /** |
||
71 | * Regular expressions for matching dynamic methods. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $dynamicMethods = [ |
||
76 | "/(?<method>filterBy)(?<variable>[A-Z][A-Za-z]+){1}/", |
||
77 | "/(?<method>sort)(?<direction>Asc|Desc)?(By)(?<variable>[A-Z][A-Za-z]+){1}/", |
||
78 | "/(?<method>fetch)(?<first>First)?(With)(?<variable>[A-Za-z]+)/" |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * An instance of the DataOperations class used for filtered deletes. |
||
83 | * |
||
84 | * @var DataOperations |
||
85 | */ |
||
86 | private $dataOperations; |
||
87 | |||
88 | /** |
||
89 | * An instance of the Driver class used for establishing database connections. |
||
90 | * |
||
91 | * @var Driver |
||
92 | */ |
||
93 | private $driver; |
||
94 | |||
95 | /** |
||
96 | * QueryOperations constructor |
||
97 | * |
||
98 | * @param RecordWrapper $wrapper |
||
99 | * @param DataOperations $dataOperations |
||
100 | * @param Driver $driver |
||
101 | * @internal param DriverAdapter $adapter |
||
102 | */ |
||
103 | 34 | public function __construct(RecordWrapper $wrapper, DataOperations $dataOperations, Driver $driver) |
|
110 | |||
111 | /** |
||
112 | * Fetches items from the database. |
||
113 | * |
||
114 | * @param int|array|QueryParameters $query |
||
115 | * @return RecordWrapper |
||
116 | */ |
||
117 | 24 | public function doFetch($query = null) |
|
125 | |||
126 | /** |
||
127 | * The method takes multiple types of arguments and converts it to a QueryParametersObject. |
||
128 | * When this method receives null, it returns a new instance of QueryParameters. When it receives an integer, it |
||
129 | * returns a QueryParameters object that points the primary key to the integer. When it receives an associative |
||
130 | * array, it builds a series of conditions with array key-value pairs. |
||
131 | * |
||
132 | * @param int|array|QueryParameters $arg |
||
133 | * @param bool $instantiate |
||
134 | * @return QueryParameters |
||
135 | */ |
||
136 | 26 | private function buildFetchQueryParameters($arg, $instantiate = true) |
|
156 | |||
157 | /** |
||
158 | * Creates a new instance of the QueryParameters if required or just returns an already instance. |
||
159 | * |
||
160 | * @param bool $forceInstantiation |
||
161 | * @return QueryParameters |
||
162 | */ |
||
163 | 32 | private function getQueryParameters($forceInstantiation = true) |
|
170 | |||
171 | /** |
||
172 | * Clears up the query parameters. |
||
173 | */ |
||
174 | 32 | private function resetQueryParameters() |
|
178 | |||
179 | /** |
||
180 | * Performs the fetch operation and returns just the first item. |
||
181 | * |
||
182 | * @param mixed $id |
||
183 | * @return RecordWrapper |
||
184 | */ |
||
185 | 10 | public function doFetchFirst($id = null) |
|
190 | |||
191 | /** |
||
192 | * Set the fields that should be returned for each record. |
||
193 | * |
||
194 | * @return RecordWrapper |
||
195 | */ |
||
196 | 12 | public function doFields() |
|
210 | |||
211 | /** |
||
212 | * Sort the query by a given field in a given directory. |
||
213 | * |
||
214 | * @param string $field |
||
215 | * @param string $direction |
||
216 | */ |
||
217 | public function doSortBy($field, $direction = 'ASC') |
||
221 | |||
222 | /** |
||
223 | * |
||
224 | * |
||
225 | * @param mixed $arguments |
||
226 | * @return array |
||
227 | */ |
||
228 | 10 | private function getFilter($arguments) |
|
239 | |||
240 | 6 | public function doFilter() |
|
253 | |||
254 | 4 | public function doFilterBy() |
|
261 | |||
262 | 6 | public function doUpdate($data) |
|
270 | |||
271 | 2 | public function doDelete($args = null) |
|
297 | |||
298 | 10 | public function runDynamicMethod($arguments) |
|
317 | |||
318 | 10 | public function initDynamicMethod($method) |
|
332 | |||
333 | public function doCount($query = null) |
||
334 | { |
||
335 | return $this->adapter->count($this->buildFetchQueryParameters($query)); |
||
336 | } |
||
337 | |||
338 | public function doLimit($numItems) |
||
343 | |||
344 | public function doOffset($offset) |
||
349 | |||
350 | public function doWith($model) |
||
358 | |||
359 | } |
||
360 |
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: