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 |
||
27 | trait RepositoryTrait |
||
28 | { |
||
29 | /** |
||
30 | * @var ResourceInterface |
||
31 | */ |
||
32 | private $resource; |
||
33 | |||
34 | /** |
||
35 | * @param EntityManager $em |
||
36 | * @param ClassMetadata $class |
||
37 | * @param ResourceInterface $resource |
||
38 | */ |
||
39 | 22 | public function __construct($em, ClassMetadata $class, ResourceInterface $resource) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 1 | public function findForIndex(array $criteria, array $orderBy = []) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 1 | public function findForShow(array $criteria, array $orderBy = []) |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 1 | public function findForUpdate(array $criteria, array $orderBy = []) |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 1 | public function findForDelete(array $criteria, array $orderBy = []) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 7 | public function findOneBy(array $criteria, array $orderBy = []) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 1 | View Code Duplication | public function findBy(array $criteria, array $orderBy = [], $limit = null, $offset = null) |
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 18 | public function createQueryBuilder($alias = null, $indexBy = null) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 5 | public function createQueryBuilderForCollection($alias = null, $indexBy = null) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 1 | public function createDataSourceBuilder(array $options = []) |
|
131 | |||
132 | /** |
||
133 | * @param string $property |
||
134 | * @param QueryBuilder|string|null $root |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 11 | public function getProperty($property, $root = null) |
|
142 | |||
143 | /** |
||
144 | * @param mixed[] $criteria |
||
145 | * @param string[] $orderBy |
||
146 | * @param bool $collection |
||
147 | * |
||
148 | * @return QueryBuilder |
||
149 | */ |
||
150 | 9 | View Code Duplication | protected function buildQueryBuilder(array $criteria, array $orderBy, $collection = false) |
159 | |||
160 | /** |
||
161 | * @param QueryBuilder $queryBuilder |
||
162 | * @param mixed[] $criteria |
||
163 | */ |
||
164 | 9 | private function applyCriteria(QueryBuilder $queryBuilder, array $criteria = null) |
|
188 | |||
189 | /** |
||
190 | * @param QueryBuilder $queryBuilder |
||
191 | * @param string[] $orderBy |
||
192 | */ |
||
193 | 9 | View Code Duplication | private function applySorting(QueryBuilder $queryBuilder, array $orderBy = []) |
201 | |||
202 | /** |
||
203 | * @param string $property |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 8 | private function createParameter($property) |
|
211 | |||
212 | /** |
||
213 | * @param string $parameter |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | 8 | private function createPlaceholder($parameter) |
|
221 | |||
222 | /** |
||
223 | * @param QueryBuilder|string|null $root |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 11 | private function getRootAlias($root) |
|
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | 16 | private function getAlias() |
|
243 | } |
||
244 |
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.