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) |
|
101 | { |
||
102 | 34 | $this->wrapper = $wrapper; |
|
103 | 34 | $this->adapter = $wrapper->getAdapter(); |
|
104 | 34 | $this->dataOperations = $dataOperations; |
|
105 | 34 | $this->driver = $driver; |
|
106 | 34 | } |
|
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) |
|
116 | { |
||
117 | 24 | $parameters = $this->buildFetchQueryParameters($query); |
|
118 | 24 | $data = $this->adapter->select($parameters); |
|
119 | 24 | $this->wrapper->setData($data); |
|
120 | 24 | $this->resetQueryParameters(); |
|
121 | |||
122 | 24 | return $this->wrapper; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * The method takes multiple types of arguments and converts it to a QueryParametersObject. |
||
127 | * When this method receives null, it returns a new instance of QueryParameters. When it receives an integer, it |
||
128 | * returns a QueryParameters object that points the primary key to the integer. When it receives an associative |
||
129 | * array, it builds a series of conditions with array key-value pairs. |
||
130 | * |
||
131 | * @param int|array|QueryParameters $arg |
||
132 | * @param bool $instantiate |
||
133 | * |
||
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 | * |
||
162 | * @return QueryParameters |
||
163 | */ |
||
164 | 32 | private function getQueryParameters($forceInstantiation = true) |
|
172 | |||
173 | /** |
||
174 | * Clears up the query parameters. |
||
175 | */ |
||
176 | 32 | private function resetQueryParameters() |
|
180 | |||
181 | /** |
||
182 | * Performs the fetch operation and returns just the first item. |
||
183 | * |
||
184 | * @param mixed $id |
||
185 | * |
||
186 | * @return RecordWrapper |
||
187 | */ |
||
188 | 10 | public function doFetchFirst($id = null) |
|
194 | |||
195 | /** |
||
196 | * Set the fields that should be returned for each record. |
||
197 | * |
||
198 | * @return RecordWrapper |
||
199 | */ |
||
200 | 12 | public function doFields() |
|
215 | |||
216 | /** |
||
217 | * Sort the query by a given field in a given directory. |
||
218 | * |
||
219 | * @param string $field |
||
220 | * @param string $direction |
||
221 | */ |
||
222 | public function doSortBy($field, $direction = 'ASC') |
||
226 | |||
227 | /** |
||
228 | * @param mixed $arguments |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | 10 | private function getFilter($arguments) |
|
244 | |||
245 | 6 | public function doFilter() |
|
259 | |||
260 | 4 | public function doFilterBy() |
|
268 | |||
269 | 6 | public function doUpdate($data) |
|
277 | |||
278 | 2 | public function doDelete($args = null) |
|
304 | |||
305 | 10 | public function runDynamicMethod($arguments) |
|
327 | |||
328 | 10 | public function initDynamicMethod($method) |
|
342 | |||
343 | public function doCount($query = null) |
||
347 | |||
348 | public function doLimit($numItems) |
||
354 | |||
355 | public function doOffset($offset) |
||
361 | |||
362 | public function doWith($model) |
||
371 | } |
||
372 |
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: