Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Query implements \IteratorAggregate |
||
32 | { |
||
33 | use Macroable { |
||
34 | Macroable::__call as __macroableCall; |
||
35 | Macroable::__callStatic as __macroableCallStatic; |
||
36 | } |
||
37 | |||
38 | use ModeProvider; |
||
39 | use AliasProvider; |
||
40 | use WhereProvider; |
||
41 | use OrderProvider; |
||
42 | use SelectProvider; |
||
43 | use GroupByProvider; |
||
44 | use RelationProvider; |
||
45 | use RepositoryProvider; |
||
46 | use ExecutionsProvider; |
||
47 | use LimitAndOffsetProvider; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private static $booted = false; |
||
53 | |||
54 | /** |
||
55 | * @var CriterionInterface[] |
||
56 | */ |
||
57 | protected $criteria = []; |
||
58 | |||
59 | /** |
||
60 | * @var array|ObjectRepository[] |
||
61 | */ |
||
62 | protected $scopes = []; |
||
63 | |||
64 | /** |
||
65 | * Query constructor. |
||
66 | * @param ObjectRepository|null $repository |
||
67 | */ |
||
68 | 63 | public function __construct(ObjectRepository $repository = null) |
|
74 | |||
75 | /** |
||
76 | * @param string $stmt |
||
77 | * @return string |
||
78 | */ |
||
79 | public static function raw(string $stmt): string |
||
83 | |||
84 | /** |
||
85 | * @param string $method |
||
86 | * @param array $parameters |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public static function __callStatic(string $method, array $parameters = []) |
||
95 | |||
96 | /** |
||
97 | * @param string $criterion |
||
98 | * @return bool |
||
99 | */ |
||
100 | 16 | public function has(string $criterion): bool |
|
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @return null |
||
114 | */ |
||
115 | 19 | public function __get(string $name) |
|
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getClassName(): string |
||
131 | |||
132 | /** |
||
133 | * @return EntityManagerInterface |
||
134 | */ |
||
135 | public function getEntityManager(): EntityManagerInterface |
||
139 | |||
140 | /** |
||
141 | * @return ClassMetadata |
||
142 | */ |
||
143 | public function getMetadata(): ClassMetadata |
||
147 | |||
148 | /** |
||
149 | * @param string $name |
||
150 | * @return string |
||
151 | */ |
||
152 | public function column(string $name): string |
||
159 | |||
160 | /** |
||
161 | * @param string $method |
||
162 | * @param array $parameters |
||
163 | * @return mixed|$this|Query |
||
164 | */ |
||
165 | public function __call(string $method, array $parameters = []) |
||
180 | |||
181 | /** |
||
182 | * Returns a list of selection criteria. |
||
183 | * |
||
184 | * @return \Generator|CriterionInterface[] |
||
185 | */ |
||
186 | 63 | public function getCriteria(): \Generator |
|
190 | |||
191 | /** |
||
192 | * Creates a new query (alias to the constructor). |
||
193 | * |
||
194 | * @param CriterionInterface $criterion |
||
195 | * @return Query|$this |
||
196 | */ |
||
197 | 61 | public function add(CriterionInterface $criterion): self |
|
207 | |||
208 | /** |
||
209 | * Creates a new query using the current set of scopes. |
||
210 | * |
||
211 | * @return Query |
||
212 | */ |
||
213 | 5 | public function create(): Query |
|
223 | |||
224 | /** |
||
225 | * Adds the specified set of scopes (method groups) to the query. |
||
226 | * |
||
227 | * @param object|string ...$scopes |
||
228 | * @return Query|$this |
||
229 | */ |
||
230 | 29 | public function scope(...$scopes): self |
|
236 | |||
237 | /** |
||
238 | * Creates a new query (alias to the constructor). |
||
239 | * |
||
240 | * @param ObjectRepository|null $repository |
||
241 | * @return Query |
||
242 | */ |
||
243 | 63 | public static function new(ObjectRepository $repository = null): Query |
|
247 | |||
248 | /** |
||
249 | * Returns a set of scopes for the specified query. |
||
250 | * |
||
251 | * @return array|ObjectRepository[] |
||
252 | */ |
||
253 | 5 | public function getScopes(): array |
|
257 | |||
258 | /** |
||
259 | * Copies a set of Criteria from the child query to the parent. |
||
260 | * |
||
261 | * @param Query $query |
||
262 | * @return Query |
||
263 | */ |
||
264 | public function merge(Query $query): Query |
||
272 | |||
273 | /** |
||
274 | * @param Query $query |
||
275 | * @return Query |
||
276 | */ |
||
277 | public function attach(Query $query): Query |
||
285 | |||
286 | /** |
||
287 | * @return void |
||
288 | * @throws \LogicException |
||
289 | */ |
||
290 | public function __clone() |
||
296 | |||
297 | /** |
||
298 | * @param string|\Closure $filter |
||
299 | * @return Query |
||
300 | */ |
||
301 | public function only($filter): Query |
||
327 | |||
328 | /** |
||
329 | * @param string|\Closure $filter |
||
330 | * @return Query |
||
331 | */ |
||
332 | public function except($filter): Query |
||
344 | |||
345 | /** |
||
346 | * @return Query |
||
347 | */ |
||
348 | public function clone(): Query |
||
364 | |||
365 | /** |
||
366 | * @return \Generator |
||
367 | */ |
||
368 | public function getIterator(): \Generator |
||
374 | |||
375 | /** |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function isEmpty(): bool |
||
382 | |||
383 | /** |
||
384 | * @return void |
||
385 | */ |
||
386 | 26 | private function bootIfNotBooted(): void |
|
395 | |||
396 | /** |
||
397 | * @return string |
||
398 | */ |
||
399 | public function dump(): string |
||
403 | } |
||
404 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: