Complex classes like UpdateQueryBuilder 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 UpdateQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class UpdateQueryBuilder |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $mainKeyword = 'UPDATE'; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $flags = []; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $from; |
||
37 | |||
38 | /** |
||
39 | * @var Expression |
||
40 | */ |
||
41 | private $set; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $joins = []; |
||
47 | |||
48 | /** |
||
49 | * @var Expression |
||
50 | */ |
||
51 | private $where; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $orderBy = []; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | private $limit; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | private $end = ';'; |
||
67 | |||
68 | /** |
||
69 | * @param string $table |
||
70 | * @return UpdateQueryBuilder |
||
71 | */ |
||
72 | public static function make(string $table): self |
||
78 | |||
79 | /** |
||
80 | * @param string $table |
||
81 | * @return UpdateQueryBuilder |
||
82 | */ |
||
83 | public function table(string $table): self |
||
89 | |||
90 | /** |
||
91 | * @param string $keyword |
||
92 | * @return UpdateQueryBuilder |
||
93 | */ |
||
94 | public function withMainKeyword(string $keyword): self |
||
100 | |||
101 | /** |
||
102 | * @param string[] ...$flags |
||
103 | * @return UpdateQueryBuilder |
||
104 | */ |
||
105 | public function withFlags(string ...$flags): self |
||
111 | |||
112 | /** |
||
113 | * @param string[] ...$flags |
||
114 | * @return UpdateQueryBuilder |
||
115 | */ |
||
116 | public function withAddedFlags(string ...$flags): self |
||
127 | |||
128 | /** |
||
129 | * @param string $table |
||
130 | * @param string|Expression|null $expression |
||
131 | * @param array ...$values |
||
132 | * @return UpdateQueryBuilder |
||
133 | * @throws \InvalidArgumentException |
||
134 | */ |
||
135 | public function join(string $table, $expression = null, ...$values): self |
||
144 | |||
145 | /** |
||
146 | * @param string $table |
||
147 | * @param string|Expression|null $expression |
||
148 | * @param array ...$values |
||
149 | * @return UpdateQueryBuilder |
||
150 | * @throws \InvalidArgumentException |
||
151 | */ |
||
152 | public function innerJoin(string $table, $expression = null, ...$values): self |
||
161 | |||
162 | /** |
||
163 | * @param string $table |
||
164 | * @param string|Expression|null $expression |
||
165 | * @param array ...$values |
||
166 | * @return UpdateQueryBuilder |
||
167 | * @throws \InvalidArgumentException |
||
168 | */ |
||
169 | public function outerJoin(string $table, $expression = null, ...$values): self |
||
178 | |||
179 | /** |
||
180 | * @param string $table |
||
181 | * @param string|Expression|null $expression |
||
182 | * @param array ...$values |
||
183 | * @return UpdateQueryBuilder |
||
184 | * @throws \InvalidArgumentException |
||
185 | */ |
||
186 | public function leftJoin(string $table, $expression = null, ...$values): self |
||
195 | |||
196 | /** |
||
197 | * @param string $table |
||
198 | * @param string|Expression|null $expression |
||
199 | * @param array ...$values |
||
200 | * @return UpdateQueryBuilder |
||
201 | * @throws \InvalidArgumentException |
||
202 | */ |
||
203 | public function leftOuterJoin(string $table, $expression = null, ...$values): self |
||
212 | |||
213 | /** |
||
214 | * @param string $table |
||
215 | * @param string|Expression|null $expression |
||
216 | * @param array ...$values |
||
217 | * @return UpdateQueryBuilder |
||
218 | * @throws \InvalidArgumentException |
||
219 | */ |
||
220 | public function rightJoin(string $table, $expression = null, ...$values): self |
||
229 | |||
230 | /** |
||
231 | * @param string $table |
||
232 | * @param string|Expression|null $expression |
||
233 | * @param array ...$values |
||
234 | * @return UpdateQueryBuilder |
||
235 | * @throws \InvalidArgumentException |
||
236 | */ |
||
237 | public function rightOuterJoin(string $table, $expression = null, ...$values): self |
||
246 | |||
247 | /** |
||
248 | * @param string $table |
||
249 | * @param string|Expression|null $expression |
||
250 | * @param array ...$values |
||
251 | * @return UpdateQueryBuilder |
||
252 | * @throws \InvalidArgumentException |
||
253 | */ |
||
254 | public function fullJoin(string $table, $expression = null, ...$values): self |
||
263 | |||
264 | /** |
||
265 | * @param string $table |
||
266 | * @param string|Expression|null $expression |
||
267 | * @param array ...$values |
||
268 | * @return UpdateQueryBuilder |
||
269 | * @throws \InvalidArgumentException |
||
270 | */ |
||
271 | public function fullOuterJoin(string $table, $expression = null, ...$values): self |
||
280 | |||
281 | /** |
||
282 | * Reset all JOIN clauses. |
||
283 | * |
||
284 | * @return UpdateQueryBuilder |
||
285 | */ |
||
286 | public function resetJoins(): self |
||
292 | |||
293 | /** |
||
294 | * Remove a specific JOIN clause. |
||
295 | * |
||
296 | * @param string $table |
||
297 | * @return UpdateQueryBuilder |
||
298 | */ |
||
299 | public function withoutJoin(string $table) |
||
305 | |||
306 | /** |
||
307 | * @param null $expression |
||
308 | * @param array ...$values |
||
309 | * @return UpdateQueryBuilder |
||
310 | * @throws \InvalidArgumentException |
||
311 | */ |
||
312 | public function set($expression = null, ...$values): self |
||
318 | |||
319 | /** |
||
320 | * @param null $expression |
||
321 | * @param array ...$values |
||
322 | * @return UpdateQueryBuilder |
||
323 | * @throws \InvalidArgumentException |
||
324 | */ |
||
325 | public function andSet($expression = null, ...$values): self |
||
334 | |||
335 | /** |
||
336 | * @param string|Expression|null $expression |
||
337 | * @param array ...$values |
||
338 | * @return UpdateQueryBuilder |
||
339 | * @throws \InvalidArgumentException |
||
340 | */ |
||
341 | public function where($expression = null, ...$values): self |
||
347 | |||
348 | /** |
||
349 | * @param string|Expression $expression |
||
350 | * @param array ...$values |
||
351 | * @return UpdateQueryBuilder |
||
352 | * @throws \InvalidArgumentException |
||
353 | */ |
||
354 | public function andWhere($expression, ...$values): self |
||
363 | |||
364 | /** |
||
365 | * @param string|Expression $expression |
||
366 | * @param array ...$values |
||
367 | * @return UpdateQueryBuilder |
||
368 | * @throws \InvalidArgumentException |
||
369 | */ |
||
370 | public function orWhere($expression, ...$values): self |
||
379 | |||
380 | /** |
||
381 | * @param string[] ...$groupBy |
||
382 | * @return UpdateQueryBuilder |
||
383 | */ |
||
384 | public function orderBy(string ...$orderBy): self |
||
390 | |||
391 | /** |
||
392 | * @param string[] ...$groupBy |
||
393 | * @return UpdateQueryBuilder |
||
394 | */ |
||
395 | public function andOrderBy(string ...$orderBy): self |
||
401 | |||
402 | /** |
||
403 | * @param int|null $limit |
||
404 | * @return UpdateQueryBuilder |
||
405 | */ |
||
406 | public function limit(int $limit = null): self |
||
412 | |||
413 | /** |
||
414 | * @param string|null $end |
||
415 | * @return UpdateQueryBuilder |
||
416 | */ |
||
417 | public function end(string $end = null): self |
||
423 | |||
424 | /** |
||
425 | * @return string |
||
426 | */ |
||
427 | public function __toString(): string |
||
431 | |||
432 | /** |
||
433 | * @return array |
||
434 | */ |
||
435 | public function getValues(): array |
||
442 | |||
443 | /** |
||
444 | * Read-only properties. |
||
445 | * |
||
446 | * @param $property |
||
447 | * @return mixed |
||
448 | * @throws \InvalidArgumentException |
||
449 | */ |
||
450 | public function __get($property) |
||
457 | } |
||
458 |
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.