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:
1 | <?php |
||
23 | class CriteriaQueryBuilder implements CriteriaQueryBuilderInterface |
||
24 | { |
||
25 | /** @var Criteria */ |
||
26 | protected $criteria; |
||
27 | |||
28 | /** @var \Doctrine\ORM\QueryBuilder */ |
||
29 | protected $qb; |
||
30 | |||
31 | /** @var array */ |
||
32 | private $filterParams; |
||
33 | |||
34 | /** @var array */ |
||
35 | private $sortingFields; |
||
36 | |||
37 | /** @var array */ |
||
38 | private $defaultOrder; |
||
39 | |||
40 | /** @var ServiceRegistry|HandlerInterface[]|AbstractHandler[] */ |
||
41 | private $handlers; |
||
42 | |||
43 | /** @var bool */ |
||
44 | private $applied = false; |
||
45 | |||
46 | /** |
||
47 | * @param EntityRepository $repository |
||
48 | * @param array $filterParams |
||
49 | * @param array $sortingFields |
||
50 | * |
||
51 | * @internal param ApiManagerInterface $manager |
||
52 | */ |
||
53 | 4 | public function __construct(EntityRepository $repository, array $filterParams = [], array $sortingFields = []) |
|
63 | |||
64 | 4 | protected function initDefaultHandlers(): void |
|
69 | |||
70 | /** |
||
71 | * @param FilterInterface $filter |
||
72 | * |
||
73 | * @return CriteriaQueryBuilderInterface |
||
74 | */ |
||
75 | 4 | View Code Duplication | public function addFilter(FilterInterface $filter): CriteriaQueryBuilderInterface |
95 | |||
96 | 4 | protected function resetApply():void |
|
100 | |||
101 | /** |
||
102 | * @param SortInterface $sort |
||
103 | * |
||
104 | * @return CriteriaQueryBuilderInterface |
||
105 | */ |
||
106 | View Code Duplication | public function addSorting(SortInterface $sort): CriteriaQueryBuilderInterface |
|
126 | |||
127 | /** |
||
128 | * @return \Doctrine\ORM\QueryBuilder |
||
129 | * @throws \Doctrine\ORM\Query\QueryException |
||
130 | */ |
||
131 | 3 | public function getQb(): \Doctrine\ORM\QueryBuilder |
|
138 | |||
139 | /** |
||
140 | * It caused changes in qb and criteria |
||
141 | */ |
||
142 | 4 | protected function apply(): void |
|
157 | |||
158 | /** |
||
159 | * @return Criteria |
||
160 | */ |
||
161 | 4 | public function getCriteria(): Criteria |
|
167 | |||
168 | 4 | private function applyFilters() |
|
178 | |||
179 | 4 | private function applySorting() |
|
189 | |||
190 | /** |
||
191 | * @return array |
||
192 | */ |
||
193 | 4 | public function getDefaultOrder(): array |
|
197 | |||
198 | /** |
||
199 | * @param array $defaultOrder |
||
200 | */ |
||
201 | 4 | public function setDefaultOrder(array $defaultOrder): void |
|
205 | |||
206 | /** |
||
207 | * @param array $filterParams |
||
208 | */ |
||
209 | 4 | public function setFilterParams(array $filterParams) |
|
215 | |||
216 | /** |
||
217 | * @param array $sortingFields |
||
218 | */ |
||
219 | 4 | public function setSortingFields(array $sortingFields) |
|
225 | |||
226 | /** |
||
227 | * @return ServiceRegistry |
||
228 | */ |
||
229 | public function getHandlers():ServiceRegistry |
||
233 | |||
234 | /** |
||
235 | * @param string $identifier |
||
236 | * @param HandlerInterface $handler |
||
237 | */ |
||
238 | public function addHandler(string $identifier, HandlerInterface $handler):void |
||
242 | } |
||
243 |
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.