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 |
||
28 | trait RepositoryTrait |
||
29 | { |
||
30 | /** |
||
31 | * @var ResourceInterface |
||
32 | */ |
||
33 | private $resource; |
||
34 | |||
35 | /*** |
||
36 | * @param DocumentManager $dm |
||
37 | * @param UnitOfWork $uow |
||
38 | * @param ClassMetadata $class |
||
39 | * @param ResourceInterface $resource |
||
40 | */ |
||
41 | 16 | public function __construct( |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 1 | public function findForIndex(array $criteria, array $orderBy = []) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 1 | public function findForShow(array $criteria, array $orderBy = []) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 1 | public function findForUpdate(array $criteria, array $orderBy = []) |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 1 | public function findForDelete(array $criteria, array $orderBy = []) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 6 | public function findOneBy(array $criteria, array $orderBy = []) |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 1 | View Code Duplication | public function findBy(array $criteria, array $orderBy = [], $limit = null, $offset = null) |
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 4 | public function createQueryBuilderForCollection() |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 1 | public function createDataSourceBuilder(array $options = []) |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 9 | public function getProperty($property, $root = null) |
|
137 | |||
138 | /** |
||
139 | * @param mixed[] $criteria |
||
140 | * @param string[] $orderBy |
||
141 | * @param bool $collection |
||
142 | * |
||
143 | * @return Builder |
||
144 | */ |
||
145 | 8 | View Code Duplication | protected function buildQueryBuilder(array $criteria, array $orderBy, $collection = false) |
154 | |||
155 | /** |
||
156 | * @param Builder $queryBuilder |
||
157 | * @param mixed[] $criteria |
||
158 | */ |
||
159 | 8 | private function applyCriteria(Builder $queryBuilder, array $criteria = null) |
|
171 | |||
172 | /** |
||
173 | * @param Builder $queryBuilder |
||
174 | * @param string[] $orderBy |
||
175 | */ |
||
176 | 8 | View Code Duplication | private function applySorting(Builder $queryBuilder, array $orderBy = []) |
184 | } |
||
185 |
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.