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 |
||
11 | class LocationMatcher extends QueryBasedMatcher implements SortingMatcherInterface |
||
12 | { |
||
13 | use FlexibleKeyMatcherTrait; |
||
14 | |||
15 | const MATCH_DEPTH = 'depth'; |
||
16 | const MATCH_IS_MAIN_LOCATION = 'is_main_location'; |
||
17 | const MATCH_PRIORITY = 'priority'; |
||
18 | |||
19 | protected $allowedConditions = array( |
||
20 | self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
21 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
22 | self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP, |
||
23 | self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER, |
||
24 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE, |
||
25 | self::MATCH_VISIBILITY, |
||
26 | // aliases |
||
27 | 'content_type', 'content_type_id', 'content_type_identifier', |
||
28 | // location-only |
||
29 | self::MATCH_DEPTH, self::MATCH_IS_MAIN_LOCATION, self::MATCH_PRIORITY, |
||
30 | ); |
||
31 | protected $returns = 'Location'; |
||
32 | |||
33 | /** |
||
34 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
35 | * @param array $sort |
||
36 | 12 | * @param int $offset |
|
37 | * @param int $limit |
||
38 | 12 | * @return LocationCollection |
|
39 | */ |
||
40 | public function match(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
||
44 | |||
45 | 2 | View Code Duplication | public function matchOne(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
54 | 2 | ||
55 | 2 | /** |
|
56 | * @param array $conditions key: condition, value: value: int / string / int[] / string[] |
||
57 | 2 | * @param array $sort |
|
58 | 2 | * @param int $offset |
|
59 | 2 | * @param int $limit |
|
60 | * @return LocationCollection |
||
61 | */ |
||
62 | 2 | public function matchLocation(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
|
86 | 2 | ||
87 | 1 | /** |
|
88 | 1 | * When matching by key, we accept location Id and remote Id only |
|
89 | 1 | * @param int|string $key |
|
90 | * @return array |
||
91 | */ |
||
92 | 1 | protected function getConditionsFromKey($key) |
|
99 | |||
100 | protected function getQueryCriterion($key, $values) |
||
136 | |||
137 | /** |
||
138 | * Returns all locations of a set of objects |
||
139 | * |
||
140 | * @param int[] $contentIds |
||
141 | * @return Location[] |
||
142 | * @deprecated |
||
143 | */ |
||
144 | View Code Duplication | protected function findLocationsByContentIds(array $contentIds) |
|
157 | |||
158 | /** |
||
159 | * Returns all locations of a set of objects |
||
160 | * |
||
161 | * @param int[] $remoteContentIds |
||
162 | * @return Location[] |
||
163 | * @deprecated |
||
164 | */ |
||
165 | View Code Duplication | protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
|
178 | |||
179 | /** |
||
180 | * @param int[] $locationIds |
||
181 | * @return Location[] |
||
182 | * @deprecated |
||
183 | */ |
||
184 | protected function findLocationsByLocationIds(array $locationIds) |
||
194 | |||
195 | /** |
||
196 | * @param int[] $locationRemoteIds |
||
197 | * @return Location[] |
||
198 | * @deprecated |
||
199 | */ |
||
200 | protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
||
211 | |||
212 | /** |
||
213 | * @param int[] $parentLocationIds |
||
214 | * @return Location[] |
||
215 | * @deprecated |
||
216 | */ |
||
217 | View Code Duplication | protected function findLocationsByParentLocationIds($parentLocationIds) |
|
234 | |||
235 | /** |
||
236 | * @param int[] $parentLocationRemoteIds |
||
237 | * @return Location[] |
||
238 | * @deprecated |
||
239 | */ |
||
240 | View Code Duplication | protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
251 | } |
||
252 |
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.