Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
35 | class Query implements \IteratorAggregate |
||
36 | { |
||
37 | use Macroable { |
||
38 | Macroable::__call as __macroableCall; |
||
39 | Macroable::__callStatic as __macroableCallStatic; |
||
40 | } |
||
41 | |||
42 | use ModeProvider; |
||
43 | use AliasProvider; |
||
44 | use WhereProvider; |
||
45 | use OrderProvider; |
||
46 | use SelectProvider; |
||
47 | use GroupByProvider; |
||
48 | use RelationProvider; |
||
49 | use RepositoryProvider; |
||
50 | use ExecutionsProvider; |
||
51 | use LimitAndOffsetProvider; |
||
52 | |||
53 | /** |
||
54 | * Contains the status of the download. Before any request, |
||
55 | * you need to make sure that all the runtime is loaded. |
||
56 | * |
||
57 | * It is this perennial one that indicates if at least one |
||
58 | * query has already been created in order to load the |
||
59 | * necessary functions. |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | private static $booted = false; |
||
64 | |||
65 | /** |
||
66 | * A set of query criteria in a given execution context. |
||
67 | * |
||
68 | * @var CriterionInterface[] |
||
69 | */ |
||
70 | protected $criteria = []; |
||
71 | |||
72 | /** |
||
73 | * A set of scopes (classes and objects) that have access to be |
||
74 | * able to create a query from a set of methods defined |
||
75 | * in the specified scopes. |
||
76 | * |
||
77 | * @var array|ObjectRepository[] |
||
78 | */ |
||
79 | protected $scopes = []; |
||
80 | |||
81 | /** |
||
82 | * @param ObjectRepository|null $repository |
||
83 | */ |
||
84 | 69 | public function __construct(ObjectRepository $repository = null) |
|
90 | |||
91 | /** |
||
92 | * Method for creating native DB queries or query parts. |
||
93 | * |
||
94 | * @param string $stmt |
||
95 | * @return string |
||
96 | */ |
||
97 | public static function raw(string $stmt): string |
||
101 | |||
102 | /** |
||
103 | * The method checks for the presence of the required criterion inside the query. |
||
104 | * |
||
105 | * TODO Add callable argument support (like filter). |
||
106 | * |
||
107 | * @param string $criterion |
||
108 | * @return bool |
||
109 | */ |
||
110 | 20 | public function has(string $criterion): bool |
|
120 | |||
121 | /** |
||
122 | * Provides the ability to directly access methods without specifying parentheses. |
||
123 | * |
||
124 | * TODO 1) Add High Order Messaging for methods like `->field->where(23)` instead `->where('field', 23)` |
||
125 | * TODO 2) Allow inner access `->embedded->field->where(23)` instead `->where('embedded.field', 23)` |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @return null |
||
129 | */ |
||
130 | 19 | public function __get(string $name) |
|
138 | |||
139 | /** |
||
140 | * Creates the ability to directly access the table's column. |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @return string |
||
144 | */ |
||
145 | public function column(string $name): string |
||
152 | |||
153 | /** |
||
154 | * @internal For internal use only |
||
155 | * @return ClassMetadata |
||
156 | */ |
||
157 | public function getMetadata(): ClassMetadata |
||
161 | |||
162 | /** |
||
163 | * @internal For internal use only |
||
164 | * @return EntityManagerInterface |
||
165 | */ |
||
166 | public function getEntityManager(): EntityManagerInterface |
||
170 | |||
171 | /** |
||
172 | * @internal For internal use only |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getClassName(): string |
||
179 | |||
180 | /** |
||
181 | * @param string $method |
||
182 | * @param array $parameters |
||
183 | * @return mixed|$this|Query |
||
184 | */ |
||
185 | 8 | public function __call(string $method, array $parameters) |
|
193 | |||
194 | /** |
||
195 | * @param string $method |
||
196 | * @param array $parameters |
||
197 | * @return null|Query|mixed |
||
198 | */ |
||
199 | 8 | private function callScopes(string $method, array $parameters = []) |
|
216 | |||
217 | /** |
||
218 | * Copies a set of Criteria from the child query to the parent. |
||
219 | * |
||
220 | * @param Query $query |
||
221 | * @return Query |
||
222 | */ |
||
223 | 4 | public function merge(Query $query): Query |
|
231 | |||
232 | /** |
||
233 | * Returns a list of selection criteria. |
||
234 | * |
||
235 | * @return \Generator|CriterionInterface[] |
||
236 | */ |
||
237 | 67 | public function getCriteria(): \Generator |
|
241 | |||
242 | /** |
||
243 | * @param Query $query |
||
244 | * @return Query |
||
245 | */ |
||
246 | 4 | public function attach(Query $query): Query |
|
254 | |||
255 | /** |
||
256 | * Creates a new query (alias to the constructor). |
||
257 | * |
||
258 | * @param CriterionInterface $criterion |
||
259 | * @return Query|$this |
||
260 | */ |
||
261 | 65 | public function add(CriterionInterface $criterion): self |
|
271 | |||
272 | /** |
||
273 | * @return Query |
||
274 | */ |
||
275 | 4 | public function clone(): Query |
|
291 | |||
292 | /** |
||
293 | * Creates a new query using the current set of scopes. |
||
294 | * |
||
295 | * @return Query |
||
296 | */ |
||
297 | 9 | public function create(): Query |
|
307 | |||
308 | /** |
||
309 | * Adds the specified set of scopes (method groups) to the query. |
||
310 | * |
||
311 | * @param object|string ...$scopes |
||
312 | * @return Query|$this |
||
313 | */ |
||
314 | 37 | public function scope(...$scopes): self |
|
320 | |||
321 | /** |
||
322 | * Creates a new query (alias to the constructor). |
||
323 | * |
||
324 | * @param ObjectRepository|null $repository |
||
325 | * @return Query |
||
326 | */ |
||
327 | 69 | public static function new(ObjectRepository $repository = null): Query |
|
331 | |||
332 | /** |
||
333 | * Returns a set of scopes for the specified query. |
||
334 | * |
||
335 | * @return array|ObjectRepository[] |
||
336 | */ |
||
337 | 9 | public function getScopes(): array |
|
341 | |||
342 | /** |
||
343 | * @return void |
||
344 | * @throws \LogicException |
||
345 | */ |
||
346 | public function __clone() |
||
352 | |||
353 | /** |
||
354 | * @param string|\Closure $filter |
||
355 | * @return Query |
||
356 | */ |
||
357 | public function except($filter): Query |
||
369 | |||
370 | /** |
||
371 | * @param string|\Closure $filter |
||
372 | * @return Query |
||
373 | */ |
||
374 | public function only($filter): Query |
||
390 | |||
391 | /** |
||
392 | * @param string|callable $filter |
||
393 | * @return callable |
||
394 | */ |
||
395 | private function createFilter($filter): callable |
||
409 | |||
410 | /** |
||
411 | * @return \Generator |
||
412 | */ |
||
413 | public function getIterator(): \Generator |
||
419 | |||
420 | /** |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function isEmpty(): bool |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | public function dump(): string |
||
435 | |||
436 | /** |
||
437 | * @return void |
||
438 | */ |
||
439 | 30 | private function bootIfNotBooted(): void |
|
448 | } |
||
449 |
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: