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 QueryWorker 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 QueryWorker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class QueryWorker |
||
8 | { |
||
9 | const CUSTOM_FILTERS_KEY = 'custom'; |
||
|
|||
10 | const DEFAULT_TABLE_ALIAS = 't'; |
||
11 | |||
12 | /** |
||
13 | * @var Doctrine\ORM\EntityRepository |
||
14 | */ |
||
15 | protected $repository; |
||
16 | |||
17 | /** |
||
18 | * @var Doctrine\ORM\QueryBuilder |
||
19 | */ |
||
20 | protected $queryBuilder; |
||
21 | |||
22 | /** |
||
23 | * @var Doctrine\ORM\Mapping\ClassMetadata |
||
24 | */ |
||
25 | protected $classMetadata; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $entitys = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $queryFields = []; |
||
36 | |||
37 | public function __construct($repository) |
||
43 | |||
44 | /** |
||
45 | * Retorna o nome da entity. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getEntityName() |
||
53 | |||
54 | /** |
||
55 | * Retorna a chave primária da entity. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getPrimaryKeyEntity() |
||
63 | |||
64 | /** |
||
65 | * @return Doctrine\ORM\Query |
||
66 | */ |
||
67 | public function getQuery() |
||
77 | |||
78 | /** |
||
79 | * Retorna um array com os objetos do resultado de $this->queryBuilder. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getResult() |
||
93 | |||
94 | /** |
||
95 | * Retorna um objeto do resultado de $this->queryBuilder. |
||
96 | * |
||
97 | * @return Bludata\Doctrine\ORM\Entities\BaseEntity | null |
||
98 | */ |
||
99 | public function getOneResult() |
||
103 | |||
104 | /** |
||
105 | * Converte os objetos de $this->getResult() em array. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function toArray(array $options = null) |
||
123 | |||
124 | /** |
||
125 | * @return Doctrine\ORM\Mapping\ClassMetadata |
||
126 | */ |
||
127 | public function getClassMetaData() |
||
131 | |||
132 | /** |
||
133 | * Retorna a quantidade de elementos em $this->getResult(). |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function count() |
||
141 | |||
142 | /** |
||
143 | * Aplica filtros em $this->queryBuilder. |
||
144 | * |
||
145 | * @param array $filters |
||
146 | * |
||
147 | * @return Bludata\Doctrine\ORM\Repositories\QueryWorker |
||
148 | */ |
||
149 | public function withFilters(array $filters = null) |
||
191 | |||
192 | /** |
||
193 | * Set the page with paginate attribute. |
||
194 | * |
||
195 | * @param int $page |
||
196 | * @param int $limit |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function paginate($limit = 25, $page = 0) |
||
212 | |||
213 | /** |
||
214 | * Add joins. |
||
215 | * |
||
216 | * @param string $field |
||
217 | * |
||
218 | * @return $array |
||
219 | */ |
||
220 | private function whereFieldJoin($field, $value = null, $operation = null) |
||
245 | |||
246 | /** |
||
247 | * Add a "and where" filter. |
||
248 | * |
||
249 | * @param string $field |
||
250 | * @param string $operation |
||
251 | * @param string $value |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | View Code Duplication | public function andWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
|
266 | |||
267 | /** |
||
268 | * Add a "or where" filter. |
||
269 | * |
||
270 | * @param string $field |
||
271 | * @param string $operation |
||
272 | * @param string $value |
||
273 | * |
||
274 | * @return $this |
||
275 | */ |
||
276 | View Code Duplication | public function orWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
|
287 | |||
288 | /** |
||
289 | * Add a join filter. |
||
290 | * |
||
291 | * @param array $meta |
||
292 | * @param string $alias |
||
293 | * |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function manyToManyJoin($meta, $alias, $defaultAlias = self::DEFAULT_TABLE_ALIAS) |
||
307 | |||
308 | /** |
||
309 | * Create an array of expressions. |
||
310 | * |
||
311 | * @param array $conditions |
||
312 | * |
||
313 | * @return $this |
||
314 | */ |
||
315 | private function makeExpressions($conditions, $alias = self::DEFAULT_TABLE_ALIAS) |
||
331 | |||
332 | /** |
||
333 | * Add a "group by" key. |
||
334 | * |
||
335 | * @param string $field |
||
336 | */ |
||
337 | public function addGroupBy($field) |
||
356 | |||
357 | /** |
||
358 | * Add a "and having" filter. |
||
359 | * |
||
360 | * @param string $field |
||
361 | * @param string $operation |
||
362 | * @param string $value |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function andHaving($field, $operation, $value = null) |
||
370 | |||
371 | /** |
||
372 | * Add a "or having" filter. |
||
373 | * |
||
374 | * @param string $field |
||
375 | * @param string $order |
||
376 | */ |
||
377 | public function orHaving($field, $operation, $value = null) |
||
381 | |||
382 | /** |
||
383 | * Add a "order by" filter. |
||
384 | * |
||
385 | * @param string $field |
||
386 | * @param string $order |
||
387 | */ |
||
388 | View Code Duplication | public function addOrderBy($field, $order = 'ASC') |
|
400 | |||
401 | /** |
||
402 | * Add a "order by" filter. |
||
403 | * |
||
404 | * @param string $field |
||
405 | * @param string $order |
||
406 | */ |
||
407 | public function fkAddOrderBy($field, $fkField, $order = 'ASC') |
||
415 | |||
416 | /** |
||
417 | * Add a select statement. |
||
418 | * |
||
419 | * @param associationField.fkField |
||
420 | * @param $field |
||
421 | */ |
||
422 | public function select($fields) |
||
449 | |||
450 | /** |
||
451 | * get the repository. |
||
452 | * |
||
453 | * @param associationField.fkField |
||
454 | */ |
||
455 | private function getPathRepository($newEntity) |
||
459 | |||
460 | /** |
||
461 | * get the class metadata. |
||
462 | * |
||
463 | * @param associationField.fkField |
||
464 | */ |
||
465 | private function getMetaRepository($entity) |
||
471 | |||
472 | /** |
||
473 | * Add association join and select fields. |
||
474 | * |
||
475 | * @param associationField.fkField |
||
476 | * @param $field |
||
477 | */ |
||
478 | public function associationQueryFields($value, $expression = 0) |
||
553 | |||
554 | /** |
||
555 | * Add a field or expression in the select array. |
||
556 | * |
||
557 | * @param string field |
||
558 | * @param mix expression |
||
559 | */ |
||
560 | private function addQueryField($campo, $expression = 0) |
||
578 | |||
579 | /** |
||
580 | * Add a join statement. |
||
581 | * |
||
582 | * @todo ignorar registros com deletedAt == true |
||
583 | * @todo validar a associação a partir do aluno: processos.ordensServico.servicoOrdemServico.itensServicoOrdemServico.itemServico |
||
584 | * |
||
585 | * @param $meta - getClassMetaData |
||
586 | * @param $fk |
||
587 | * @param $field |
||
588 | * @param $alias |
||
589 | * @param $defaultAlias |
||
590 | */ |
||
591 | public function fkAssociation($meta, $fk, $field, $alias, $defaultAlias) |
||
636 | |||
637 | /** |
||
638 | * Add joins and return the new field and alias. |
||
639 | * |
||
640 | * @param $meta - getClassMetaData |
||
641 | * @param $field |
||
642 | * @param $alias |
||
643 | * |
||
644 | * @return array |
||
645 | */ |
||
646 | public function fieldJoin($meta, $field, $alias) |
||
663 | |||
664 | /** |
||
665 | * @TODO GROUP_CONCAT() |
||
666 | * Add a join statement from array collection |
||
667 | * |
||
668 | * @param $meta - getClassMetaData |
||
669 | * @param $fk |
||
670 | * @param $field |
||
671 | * @param $alias |
||
672 | * @param $defaultAlias |
||
673 | * @param $targetEntity |
||
674 | */ |
||
675 | public function fkArrayAssociation($meta, $fk, $field, $alias, $defaultAlias, $targetEntity) |
||
689 | |||
690 | /** |
||
691 | * @param string $alias |
||
692 | * @param string $fk |
||
693 | * @param array $meta |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | public function getFkMetaAlias($alias, $fk, $meta = null) |
||
708 | |||
709 | /** |
||
710 | * @param mixed $field |
||
711 | * @param string $expression |
||
712 | * @param string $alias |
||
713 | */ |
||
714 | private function getSelectExpression($expression, $field, $alias, $fieldAlias = self::DEFAULT_TABLE_ALIAS) |
||
724 | |||
725 | /** |
||
726 | * @param $field |
||
727 | * @param string $alias |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | protected function getFullFieldName($field, $alias = self::DEFAULT_TABLE_ALIAS, $separator = '.') |
||
735 | |||
736 | /** |
||
737 | * @param $field |
||
738 | * @param $operation |
||
739 | * @param null $value |
||
740 | * @param string $alias |
||
741 | */ |
||
742 | protected function makeExpression($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
||
812 | |||
813 | /** |
||
814 | * @return string |
||
815 | */ |
||
816 | protected function tableAlias() |
||
820 | } |
||
821 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.