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 ReadQuery 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 ReadQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ReadQuery extends AbstractQuery implements ReadQueryInterface |
||
31 | { |
||
32 | const EVENT_BEFORE = 'read.before'; |
||
33 | const EVENT_AFTER = 'read.after'; |
||
34 | |||
35 | use GetTypeTrait; |
||
36 | |||
37 | protected $queryString; |
||
38 | protected $queryParams = []; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $casts = []; |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * |
||
48 | * @param Connection $connection |
||
49 | * @param ModelInterface $model |
||
50 | * @param RelationFactoryInterface $factory |
||
51 | * @param AccessorInterface $accessor |
||
52 | * @param EventDispatcherInterface $dispatcher |
||
53 | */ |
||
54 | View Code Duplication | public function __construct(Connection $connection, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher) |
|
61 | |||
62 | /** |
||
63 | * Sets query instance with delete operation and table |
||
64 | */ |
||
65 | protected function setQuery() |
||
71 | |||
72 | /** |
||
73 | * Sets field names which will be read |
||
74 | * |
||
75 | * @param array $fields |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | View Code Duplication | public function fields($fields = []) |
|
98 | |||
99 | /** |
||
100 | * Adds field to query |
||
101 | * |
||
102 | * @param string $field |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function field($field) |
||
112 | |||
113 | /** |
||
114 | * Assigns field to query |
||
115 | * |
||
116 | * @param FieldInterface $field |
||
117 | */ |
||
118 | protected function assignField(FieldInterface $field) |
||
134 | |||
135 | /** |
||
136 | * Adds where condition to query |
||
137 | * |
||
138 | * @param string|array $field |
||
139 | * @param mixed $value |
||
140 | * @param string $comparison |
||
141 | * @param string $logical |
||
142 | * |
||
143 | * @return $this |
||
144 | * @throws QueryException |
||
145 | */ |
||
146 | public function where($field, $value, $comparison = '=', $logical = 'and') |
||
160 | |||
161 | /** |
||
162 | * Adds where condition to query |
||
163 | * |
||
164 | * @param string|array $field |
||
165 | * @param mixed $value |
||
166 | * @param string $comparison |
||
167 | * @param string $logical |
||
168 | * |
||
169 | * @return string |
||
170 | * @throws QueryException |
||
171 | */ |
||
172 | public function condition($field, $value, $comparison, $logical) |
||
183 | |||
184 | /** |
||
185 | * Builds condition for singular field |
||
186 | * |
||
187 | * @param string $field |
||
188 | * @param mixed $value |
||
189 | * @param string $comparison |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function buildSingularFieldCondition($field, $value, $comparison) |
||
203 | |||
204 | /** |
||
205 | * Builds conditions for multiple fields |
||
206 | * |
||
207 | * @param array $fields |
||
208 | * @param mixed $value |
||
209 | * @param string $comparison |
||
210 | * @param string $logical |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | protected function buildMultipleFieldsCondition($fields, $value, $comparison, $logical) |
||
234 | |||
235 | /** |
||
236 | * Builds condition string |
||
237 | * |
||
238 | * @param string $field |
||
239 | * @param string|array $bind |
||
240 | * @param string $operator |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function buildConditionString($field, $bind, $operator) |
||
267 | |||
268 | /** |
||
269 | * Asserts correct comparison operator |
||
270 | * |
||
271 | * @param string $operator |
||
272 | * |
||
273 | * @return string |
||
274 | * @throws QueryException |
||
275 | */ |
||
276 | protected function normalizeComparison($operator) |
||
316 | |||
317 | /** |
||
318 | * Asserts correct logical operation |
||
319 | * |
||
320 | * @param string $operator |
||
321 | * |
||
322 | * @return string |
||
323 | * @throws QueryException |
||
324 | */ |
||
325 | protected function normalizeLogical($operator) |
||
338 | |||
339 | /** |
||
340 | * Binds condition value to key |
||
341 | * |
||
342 | * @param string $name |
||
343 | * @param string $type |
||
344 | * @param mixed $values |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function bindValues($name, $type, $values) |
||
360 | |||
361 | /** |
||
362 | * Adds sorting to query |
||
363 | * |
||
364 | * @param string $field |
||
365 | * @param string $order |
||
366 | * |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function order($field, $order = 'desc') |
||
378 | |||
379 | /** |
||
380 | * Asserts correct order |
||
381 | * |
||
382 | * @param string $order |
||
383 | * |
||
384 | * @return string |
||
385 | * @throws QueryException |
||
386 | */ |
||
387 | protected function normalizeOrder($order) |
||
399 | |||
400 | /** |
||
401 | * Sets limits to query |
||
402 | * |
||
403 | * @param int $limit |
||
404 | * @param null|int $offset |
||
405 | * |
||
406 | * @return $this |
||
407 | */ |
||
408 | public function limit($limit, $offset = null) |
||
418 | |||
419 | /** |
||
420 | * Returns number of entities that will be read |
||
421 | * |
||
422 | * @return int |
||
423 | */ |
||
424 | public function count() |
||
436 | |||
437 | /** |
||
438 | * Sets custom query to be executed instead of one based on entity structure |
||
439 | * |
||
440 | * @param string $query |
||
441 | * @param array $params |
||
442 | * |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function query($query, array $params = []) |
||
452 | |||
453 | /** |
||
454 | * Executes query |
||
455 | * After execution query is reset |
||
456 | * |
||
457 | * @return mixed |
||
458 | */ |
||
459 | public function execute() |
||
475 | |||
476 | /** |
||
477 | * Executes query - from builder or custom |
||
478 | * |
||
479 | * @return Statement |
||
480 | * @throws \Doctrine\DBAL\DBALException |
||
481 | */ |
||
482 | protected function executeQuery() |
||
490 | |||
491 | /** |
||
492 | * Fetches result as associative array, mostly for pivot tables |
||
493 | * |
||
494 | * @param Statement $stmt |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | protected function fetchAsAssoc(Statement $stmt) |
||
504 | |||
505 | /** |
||
506 | * Fetches result as entity object |
||
507 | * |
||
508 | * @param Statement $stmt |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | protected function fetchAsObject(Statement $stmt) |
||
524 | |||
525 | /** |
||
526 | * Restores entity values from their stored representation |
||
527 | * |
||
528 | * @param object $entity |
||
529 | * @param array $restore |
||
530 | * @param \ReflectionClass $ref |
||
531 | * |
||
532 | * @return mixed |
||
533 | */ |
||
534 | protected function restoreObject($entity, array $restore, \ReflectionClass $ref) |
||
565 | |||
566 | /** |
||
567 | * Converts read value to its php representation |
||
568 | * |
||
569 | * @param mixed $value |
||
570 | * @param string $type |
||
571 | * |
||
572 | * @return mixed |
||
573 | * @throws \Doctrine\DBAL\DBALException |
||
574 | */ |
||
575 | protected function convertToPHPValue($value, $type) |
||
579 | |||
580 | /** |
||
581 | * Resets adapter |
||
582 | * |
||
583 | * @return $this |
||
584 | */ |
||
585 | public function reset() |
||
599 | } |
||
600 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.