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:
Complex classes like ContentMatcher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentMatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class ContentMatcher extends QueryBasedMatcher implements SortingMatcherInterface |
||
11 | { |
||
12 | use FlexibleKeyMatcherTrait; |
||
13 | |||
14 | const MATCH_RELATES_TO = 'relates_to'; |
||
15 | const MATCH_RELATED_FROM = 'related_from'; |
||
16 | |||
17 | protected $allowedConditions = array( |
||
18 | self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
19 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
20 | self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP, |
||
21 | self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER, |
||
22 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE, |
||
23 | self::MATCH_VISIBILITY, |
||
24 | // aliases |
||
25 | 'content_type', 'content_type_id', 'content_type_identifier', |
||
26 | // content-only |
||
27 | self::MATCH_RELATES_TO, self::MATCH_RELATED_FROM |
||
28 | ); |
||
29 | protected $returns = 'Content'; |
||
30 | |||
31 | /** |
||
32 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
33 | * @param array $sort |
||
34 | 16 | * @param int $offset |
|
35 | * @param int $limit |
||
36 | 16 | * @return ContentCollection |
|
37 | */ |
||
38 | public function match(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
||
42 | |||
43 | 13 | View Code Duplication | public function matchOne(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
52 | |||
53 | /** |
||
54 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
55 | * @param array $sort |
||
56 | * @param int $offset |
||
57 | * @param int $limit |
||
58 | * @return ContentCollection |
||
59 | */ |
||
60 | public function matchContent(array $conditions, array $sort = array(), $offset = 0, $limit = 0) |
||
137 | |||
138 | /** |
||
139 | * When matching by key, we accept content Id and remote Id only |
||
140 | * @param int|string $key |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function getConditionsFromKey($key) |
||
150 | |||
151 | /** |
||
152 | * @param int[] $contentIds |
||
153 | * @return Content[] |
||
154 | * @deprecated |
||
155 | */ |
||
156 | View Code Duplication | protected function findContentsByContentIds(array $contentIds) |
|
168 | |||
169 | /** |
||
170 | * @param string[] $remoteContentIds |
||
171 | * @return Content[] |
||
172 | * @deprecated |
||
173 | */ |
||
174 | View Code Duplication | protected function findContentsByContentRemoteIds(array $remoteContentIds) |
|
186 | |||
187 | /** |
||
188 | * @param int[] $locationIds |
||
189 | * @return Content[] |
||
190 | * @deprecated |
||
191 | */ |
||
192 | View Code Duplication | protected function findContentsByLocationIds(array $locationIds) |
|
204 | |||
205 | /** |
||
206 | * @param string[] $remoteLocationIds |
||
207 | * @return Content[] |
||
208 | * @deprecated |
||
209 | */ |
||
210 | View Code Duplication | protected function findContentsByLocationRemoteIds($remoteLocationIds) |
|
222 | |||
223 | /** |
||
224 | * @param int[] $parentLocationIds |
||
225 | * @return Content[] |
||
226 | * @deprecated |
||
227 | */ |
||
228 | View Code Duplication | protected function findContentsByParentLocationIds($parentLocationIds) |
|
244 | |||
245 | /** |
||
246 | * @param string[] $remoteParentLocationIds |
||
247 | * @return Content[] |
||
248 | * @deprecated |
||
249 | */ |
||
250 | View Code Duplication | protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds) |
|
262 | |||
263 | /** |
||
264 | * @param string[] $contentTypeIdentifiers |
||
265 | * @return Content[] |
||
266 | * @deprecated |
||
267 | */ |
||
268 | View Code Duplication | protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers) |
|
290 | |||
291 | /** |
||
292 | * @param int[] $contentTypeIds |
||
293 | * @return Content[] |
||
294 | * @deprecated |
||
295 | */ |
||
296 | View Code Duplication | protected function findContentsByContentTypeIds(array $contentTypeIds) |
|
317 | |||
318 | } |
||
319 |
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.