Complex classes like DeleteQueryBuilder 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 DeleteQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class DeleteQueryBuilder |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $mainKeyword = 'DELETE'; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $flags = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $tables = []; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $from; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $partitions = []; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $joins = []; |
||
54 | |||
55 | /** |
||
56 | * @var Expression |
||
57 | */ |
||
58 | private $where; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $orderBy = []; |
||
64 | |||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | private $limit; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $end = ';'; |
||
74 | |||
75 | /** |
||
76 | * @param Expression[]|string[] ...$tables |
||
77 | * @return DeleteQueryBuilder |
||
78 | */ |
||
79 | public static function make(...$tables): self |
||
87 | |||
88 | /** |
||
89 | * @param string $keyword |
||
90 | * @return DeleteQueryBuilder |
||
91 | */ |
||
92 | public function withMainKeyword(string $keyword): self |
||
98 | |||
99 | /** |
||
100 | * @param Expression[]|string[] ...$tables |
||
101 | * @return DeleteQueryBuilder |
||
102 | */ |
||
103 | public function deleteOnlyFromTables(...$tables): self |
||
109 | |||
110 | /** |
||
111 | * @param string[] ...$flags |
||
112 | * @return DeleteQueryBuilder |
||
113 | */ |
||
114 | public function withFlags(string ...$flags): self |
||
120 | |||
121 | /** |
||
122 | * @param string[] ...$flags |
||
123 | * @return DeleteQueryBuilder |
||
124 | */ |
||
125 | public function withAddedFlags(string ...$flags): self |
||
136 | |||
137 | /** |
||
138 | * @param string $table |
||
139 | * @return DeleteQueryBuilder |
||
140 | */ |
||
141 | public function from(string $table = null): self |
||
147 | |||
148 | /** |
||
149 | * @param string[] ...$partitions |
||
150 | * @return DeleteQueryBuilder |
||
151 | */ |
||
152 | public function withPartitions(string ...$partitions): self |
||
158 | |||
159 | /** |
||
160 | * @param string[] ...$partitions |
||
161 | * @return DeleteQueryBuilder |
||
162 | */ |
||
163 | public function withAddedPartitions(string ...$partitions): self |
||
173 | |||
174 | /** |
||
175 | * @param string $table |
||
176 | * @param string|Expression|null $expression |
||
177 | * @param array ...$values |
||
178 | * @return DeleteQueryBuilder |
||
179 | * @throws \InvalidArgumentException |
||
180 | */ |
||
181 | public function join(string $table, $expression = null, ...$values): self |
||
190 | |||
191 | /** |
||
192 | * @param string $table |
||
193 | * @param string|Expression|null $expression |
||
194 | * @param array ...$values |
||
195 | * @return DeleteQueryBuilder |
||
196 | * @throws \InvalidArgumentException |
||
197 | */ |
||
198 | public function innerJoin(string $table, $expression = null, ...$values): self |
||
207 | |||
208 | /** |
||
209 | * @param string $table |
||
210 | * @param string|Expression|null $expression |
||
211 | * @param array ...$values |
||
212 | * @return DeleteQueryBuilder |
||
213 | * @throws \InvalidArgumentException |
||
214 | */ |
||
215 | public function outerJoin(string $table, $expression = null, ...$values): self |
||
224 | |||
225 | /** |
||
226 | * @param string $table |
||
227 | * @param string|Expression|null $expression |
||
228 | * @param array ...$values |
||
229 | * @return DeleteQueryBuilder |
||
230 | * @throws \InvalidArgumentException |
||
231 | */ |
||
232 | public function leftJoin(string $table, $expression = null, ...$values): self |
||
241 | |||
242 | /** |
||
243 | * @param string $table |
||
244 | * @param string|Expression|null $expression |
||
245 | * @param array ...$values |
||
246 | * @return DeleteQueryBuilder |
||
247 | * @throws \InvalidArgumentException |
||
248 | */ |
||
249 | public function leftOuterJoin(string $table, $expression = null, ...$values): self |
||
258 | |||
259 | /** |
||
260 | * @param string $table |
||
261 | * @param string|Expression|null $expression |
||
262 | * @param array ...$values |
||
263 | * @return DeleteQueryBuilder |
||
264 | * @throws \InvalidArgumentException |
||
265 | */ |
||
266 | public function rightJoin(string $table, $expression = null, ...$values): self |
||
275 | |||
276 | /** |
||
277 | * @param string $table |
||
278 | * @param string|Expression|null $expression |
||
279 | * @param array ...$values |
||
280 | * @return DeleteQueryBuilder |
||
281 | * @throws \InvalidArgumentException |
||
282 | */ |
||
283 | public function rightOuterJoin(string $table, $expression = null, ...$values): self |
||
292 | |||
293 | /** |
||
294 | * @param string $table |
||
295 | * @param string|Expression|null $expression |
||
296 | * @param array ...$values |
||
297 | * @return DeleteQueryBuilder |
||
298 | * @throws \InvalidArgumentException |
||
299 | */ |
||
300 | public function fullJoin(string $table, $expression = null, ...$values): self |
||
309 | |||
310 | /** |
||
311 | * @param string $table |
||
312 | * @param string|Expression|null $expression |
||
313 | * @param array ...$values |
||
314 | * @return DeleteQueryBuilder |
||
315 | * @throws \InvalidArgumentException |
||
316 | */ |
||
317 | public function fullOuterJoin(string $table, $expression = null, ...$values): self |
||
326 | |||
327 | /** |
||
328 | * Reset all JOIN clauses. |
||
329 | * |
||
330 | * @return DeleteQueryBuilder |
||
331 | */ |
||
332 | public function resetJoins(): self |
||
338 | |||
339 | /** |
||
340 | * Remove a specific JOIN clause. |
||
341 | * |
||
342 | * @param string $table |
||
343 | * @return DeleteQueryBuilder |
||
344 | */ |
||
345 | public function withoutJoin(string $table) |
||
351 | |||
352 | /** |
||
353 | * @param string|Expression|null $expression |
||
354 | * @param array ...$values |
||
355 | * @return DeleteQueryBuilder |
||
356 | * @throws \InvalidArgumentException |
||
357 | */ |
||
358 | public function where($expression = null, ...$values): self |
||
364 | |||
365 | /** |
||
366 | * @param string|Expression $expression |
||
367 | * @param array ...$values |
||
368 | * @return DeleteQueryBuilder |
||
369 | * @throws \InvalidArgumentException |
||
370 | */ |
||
371 | public function andWhere($expression, ...$values): self |
||
380 | |||
381 | /** |
||
382 | * @param string|Expression $expression |
||
383 | * @param array ...$values |
||
384 | * @return DeleteQueryBuilder |
||
385 | * @throws \InvalidArgumentException |
||
386 | */ |
||
387 | public function orWhere($expression, ...$values): self |
||
396 | |||
397 | /** |
||
398 | * @param string[] ...$groupBy |
||
399 | * @return DeleteQueryBuilder |
||
400 | */ |
||
401 | public function orderBy(string ...$orderBy): self |
||
407 | |||
408 | /** |
||
409 | * @param string[] ...$groupBy |
||
410 | * @return DeleteQueryBuilder |
||
411 | */ |
||
412 | public function andOrderBy(string ...$orderBy): self |
||
418 | |||
419 | /** |
||
420 | * @param int|null $limit |
||
421 | * @return DeleteQueryBuilder |
||
422 | */ |
||
423 | public function limit(int $limit = null): self |
||
429 | |||
430 | /** |
||
431 | * @param string|null $end |
||
432 | * @return DeleteQueryBuilder |
||
433 | */ |
||
434 | public function end(string $end = null): self |
||
440 | |||
441 | /** |
||
442 | * @return array |
||
443 | */ |
||
444 | public function getValues(): array |
||
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | public function __toString(): string |
||
459 | |||
460 | /** |
||
461 | * Read-only properties. |
||
462 | * |
||
463 | * @param $property |
||
464 | * @return mixed |
||
465 | * @throws \InvalidArgumentException |
||
466 | */ |
||
467 | public function __get($property) |
||
474 | } |
||
475 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.