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 |
||
29 | View Code Duplication | trait EntitySpecificationRepositoryTrait |
|
|
|||
30 | { |
||
31 | /** |
||
32 | * @var string alias |
||
33 | */ |
||
34 | private $alias = 'e'; |
||
35 | |||
36 | /** |
||
37 | * Get results when you match with a Specification. |
||
38 | * |
||
39 | * @param Filter|QueryModifier $specification |
||
40 | * @param ResultModifier|null $modifier |
||
41 | * |
||
42 | * @return mixed[] |
||
43 | */ |
||
44 | public function match($specification, ResultModifier $modifier = null) |
||
50 | |||
51 | /** |
||
52 | * Get single result when you match with a Specification. |
||
53 | * |
||
54 | * @param Filter|QueryModifier $specification |
||
55 | * @param ResultModifier|null $modifier |
||
56 | * |
||
57 | * @throw Exception\NonUniqueException If more than one result is found |
||
58 | * @throw Exception\NoResultException If no results found |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function matchSingleResult($specification, ResultModifier $modifier = null) |
||
74 | |||
75 | /** |
||
76 | * Get single result or null when you match with a Specification. |
||
77 | * |
||
78 | * @param Filter|QueryModifier $specification |
||
79 | * @param ResultModifier|null $modifier |
||
80 | * |
||
81 | * @throw Exception\NonUniqueException If more than one result is found |
||
82 | * |
||
83 | * @return mixed|null |
||
84 | */ |
||
85 | public function matchOneOrNullResult($specification, ResultModifier $modifier = null) |
||
93 | |||
94 | /** |
||
95 | * Get single scalar result when you match with a Specification. |
||
96 | * |
||
97 | * @param Filter|QueryModifier $specification |
||
98 | * @param ResultModifier|null $modifier |
||
99 | * |
||
100 | * @throw Exception\NonUniqueException If more than one result is found |
||
101 | * @throw Exception\NoResultException If no results found |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function matchSingleScalarResult($specification, ResultModifier $modifier = null) |
||
115 | |||
116 | /** |
||
117 | * Get scalar result when you match with a Specification. |
||
118 | * |
||
119 | * @param Filter|QueryModifier $specification |
||
120 | * @param ResultModifier|null $modifier |
||
121 | * |
||
122 | * @throw Exception\NonUniqueException If more than one result is found |
||
123 | * @throw Exception\NoResultException If no results found |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function matchScalarResult($specification, ResultModifier $modifier = null) |
||
133 | |||
134 | /** |
||
135 | * Prepare a Query with a Specification. |
||
136 | * |
||
137 | * @param Filter|QueryModifier $specification |
||
138 | * @param ResultModifier|null $modifier |
||
139 | * |
||
140 | * @return Query |
||
141 | */ |
||
142 | public function getQuery($specification, ResultModifier $modifier = null) |
||
152 | |||
153 | /** |
||
154 | * @param Filter|QueryModifier $specification |
||
155 | * @param string|null $alias |
||
156 | * |
||
157 | * @return QueryBuilder |
||
158 | */ |
||
159 | public function getQueryBuilder($specification, $alias = null) |
||
166 | |||
167 | /** |
||
168 | * Iterate results when you match with a Specification. |
||
169 | * |
||
170 | * @param Filter|QueryModifier $specification |
||
171 | * @param ResultModifier|null $modifier |
||
172 | * |
||
173 | * @return \Traversable |
||
174 | */ |
||
175 | public function iterate($specification, ResultModifier $modifier = null) |
||
181 | |||
182 | /** |
||
183 | * @param string $alias |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setAlias($alias) |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getAlias() |
||
201 | |||
202 | /** |
||
203 | * @param QueryBuilder $queryBuilder |
||
204 | * @param Filter|QueryModifier|mixed|null $specification |
||
205 | * @param string $alias |
||
206 | * |
||
207 | * @throws \InvalidArgumentException |
||
208 | */ |
||
209 | protected function applySpecification(QueryBuilder $queryBuilder, $specification = null, $alias = null) |
||
235 | } |
||
236 |
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.