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 | private $defaultQueryParameters = null; |
||
92 | |||
93 | /** |
||
94 | * QueryOperations constructor. |
||
95 | * |
||
96 | * @param RecordWrapper $wrapper |
||
97 | * @param DataOperations $dataOperations |
||
98 | * @param Driver $driver |
||
99 | * |
||
100 | * @internal param DriverAdapter $adapter |
||
101 | */ |
||
102 | 30 | public function __construct(RecordWrapper $wrapper, DataOperations $dataOperations, Driver $driver) |
|
109 | |||
110 | /** |
||
111 | * Fetches items from the database. |
||
112 | * |
||
113 | * @param int|array|QueryParameters $query |
||
114 | * |
||
115 | * @return RecordWrapper |
||
|
|||
116 | * @throws \ReflectionException |
||
117 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
118 | * @throws exceptions\NibiiException |
||
119 | */ |
||
120 | 20 | public function doFetch($query = null) |
|
133 | |||
134 | public function setDefaultQueryParameters(QueryParameters $queryParameters) |
||
138 | |||
139 | /** |
||
140 | * The method takes multiple types of arguments and converts it to a QueryParametersObject. |
||
141 | * When this method receives null, it returns a new instance of QueryParameters. When it receives an integer, it |
||
142 | * returns a QueryParameters object that points the primary key to the integer. When it receives an associative |
||
143 | * array, it builds a series of conditions with array key-value pairs. |
||
144 | * |
||
145 | * @param int|array|QueryParameters $arg |
||
146 | * @param bool $instantiate |
||
147 | * |
||
148 | * @return QueryParameters |
||
149 | * @throws \ReflectionException |
||
150 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
151 | * @throws exceptions\NibiiException |
||
152 | */ |
||
153 | 22 | private function buildFetchQueryParameters($arg, $instantiate = true) |
|
174 | |||
175 | /** |
||
176 | * Creates a new instance of the QueryParameters if required or just returns an already instance. |
||
177 | * |
||
178 | * @param bool $forceInstantiation |
||
179 | * |
||
180 | * @return QueryParameters |
||
181 | */ |
||
182 | 28 | private function getQueryParameters($forceInstantiation = true) |
|
190 | |||
191 | /** |
||
192 | * Clears up the query parameters. |
||
193 | */ |
||
194 | 8 | private function resetQueryParameters() |
|
198 | |||
199 | /** |
||
200 | * Performs the fetch operation and returns just the first item. |
||
201 | * |
||
202 | * @param mixed $id |
||
203 | * |
||
204 | * @return RecordWrapper |
||
205 | * @throws \ReflectionException |
||
206 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
207 | * @throws exceptions\NibiiException |
||
208 | */ |
||
209 | 10 | public function doFetchFirst($id = null) |
|
215 | |||
216 | /** |
||
217 | * Set the fields that should be returned for each record. |
||
218 | * |
||
219 | * @return RecordWrapper |
||
220 | */ |
||
221 | 12 | public function doFields() |
|
236 | |||
237 | 8 | public function doFix($query) |
|
241 | |||
242 | /** |
||
243 | * Sort the query by a given field in a given directory. |
||
244 | * |
||
245 | * @param string $field |
||
246 | * @param string $direction |
||
247 | */ |
||
248 | public function doSortBy($field, $direction = 'ASC') |
||
252 | |||
253 | /** |
||
254 | * @param mixed $arguments |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 10 | private function getFilter($arguments) |
|
270 | |||
271 | 6 | public function doFilter() |
|
285 | |||
286 | 4 | public function doFilterBy() |
|
294 | |||
295 | 6 | public function doUpdate($data) |
|
303 | |||
304 | 2 | public function doDelete($args = null) |
|
330 | |||
331 | 10 | public function runDynamicMethod($arguments) |
|
353 | |||
354 | 10 | public function initDynamicMethod($method) |
|
368 | |||
369 | public function doCount($query = null) |
||
373 | |||
374 | public function doLimit($numItems) |
||
380 | |||
381 | public function doOffset($offset) |
||
387 | |||
388 | public function doWith($model) |
||
397 | } |
||
398 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.