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 PdoFinder 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 PdoFinder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class PdoFinder implements FinderInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var Pdo |
||
31 | */ |
||
32 | private $conn; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $table; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $class; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $sorts = array(); |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $limit; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | private $offset = 0; |
||
58 | |||
59 | /** |
||
60 | * @var CompositeExpressionInterface[] |
||
61 | */ |
||
62 | private $conditions = array(); |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $joins = array(); |
||
68 | |||
69 | /** |
||
70 | * @var ParserInterface |
||
71 | */ |
||
72 | private $parser; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $quote; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | private $alias; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private $relations = array(); |
||
88 | |||
89 | /** |
||
90 | * Constructor. |
||
91 | * |
||
92 | * @param Pdo $conn |
||
93 | * @param string $table |
||
94 | * @param string $class |
||
95 | * @param ParserInterface $parser |
||
96 | * @param string $quote |
||
97 | * @param string|null $alias |
||
98 | */ |
||
99 | public function __construct(Pdo &$conn, $table, $class, $parser, $quote, $alias = null) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function sort(array $sorts) |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function limit($limit, $offset = 0) |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function expr() |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function where($expr) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function innerJoin($property, $alias) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function leftJoin($property, $alias) |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function rightJoin($property, $alias) |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function count() |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | public function first() |
||
232 | |||
233 | /** |
||
234 | * Fetch sets of data. |
||
235 | * |
||
236 | * @return Collection |
||
237 | */ |
||
238 | public function get() |
||
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getSQL() |
||
274 | |||
275 | /** |
||
276 | * Cast finder to string representation. |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function __toString() |
||
284 | |||
285 | /** |
||
286 | * Deserialize record to read model. |
||
287 | * |
||
288 | * @param array $record |
||
289 | * @param string $class |
||
290 | * |
||
291 | * @return ReadModelInterface |
||
292 | */ |
||
293 | protected function deserialize(array $record, $class) |
||
297 | |||
298 | /** |
||
299 | * @param string $table |
||
300 | * @param array $conditions |
||
301 | * @param string $class |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function buildRelations($table, array $conditions, $class) |
||
333 | |||
334 | /** |
||
335 | * @param string $class |
||
336 | * @param array $record |
||
337 | * @param array $related |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | protected function bindRelation($class, array $record, array $related) |
||
354 | |||
355 | /** |
||
356 | * @param string $property |
||
357 | * @param string $alias |
||
358 | * @param string $type |
||
359 | */ |
||
360 | protected function join($property, $alias, $type) |
||
384 | |||
385 | /** |
||
386 | * Compute query language. |
||
387 | * |
||
388 | * @param string $fields |
||
389 | * @param string $table |
||
390 | * @param string $alias |
||
391 | * @param array $conditions |
||
392 | * @param array $sorts |
||
393 | * @param array $joins |
||
394 | * @param int $limit |
||
395 | * @param int $offset |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function computeQuery( |
||
452 | |||
453 | /** |
||
454 | * Normalize conditions. |
||
455 | * |
||
456 | * @param array $conditions |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | protected function normalizeConditions(array $conditions) |
||
474 | |||
475 | /** |
||
476 | * Normalize sorts. |
||
477 | * |
||
478 | * @param array $sorts |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | protected function normalizeSorts(array $sorts) |
||
491 | |||
492 | /** |
||
493 | * Quote field. |
||
494 | * |
||
495 | * @param string $field |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | View Code Duplication | protected function quote($field) |
|
510 | |||
511 | /** |
||
512 | * Reset statements. |
||
513 | */ |
||
514 | protected function reset() |
||
523 | } |
||
524 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.