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 |
||
| 13 | class ContentMatcher extends RepositoryMatcher |
||
| 14 | { |
||
| 15 | use FlexibleKeyMatcherTrait; |
||
| 16 | |||
| 17 | const MATCH_CONTENT_ID = 'content_id'; |
||
| 18 | const MATCH_LOCATION_ID = 'location_id'; |
||
| 19 | const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
||
| 20 | const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
||
| 21 | const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
||
| 22 | const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
||
| 23 | const MATCH_CONTENT_TYPE_ID = 'contenttype_id'; |
||
| 24 | const MATCH_CONTENT_TYPE_IDENTIFIER = 'contenttype_identifier'; |
||
| 25 | |||
| 26 | protected $allowedConditions = array( |
||
| 27 | self::MATCH_AND, self::MATCH_OR, |
||
| 28 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
| 29 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, |
||
| 30 | // aliases |
||
| 31 | 'content_type', 'content_type_id', 'content_type_identifier' |
||
| 32 | ); |
||
| 33 | 3 | protected $returns = 'Content'; |
|
| 34 | |||
| 35 | 3 | /** |
|
| 36 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 37 | * @return ContentCollection |
||
| 38 | */ |
||
| 39 | public function match(array $conditions) |
||
| 43 | |||
| 44 | 3 | /** |
|
| 45 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 46 | 3 | * @return ContentCollection |
|
| 47 | */ |
||
| 48 | 3 | public function matchContent(array $conditions) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * When matching by key, we accept content Id and remote Id only |
||
| 105 | * @param int|string $key |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | protected function getConditionsFromKey($key) |
||
| 115 | 1 | ||
| 116 | /** |
||
| 117 | 1 | * @param int[] $contentIds |
|
| 118 | * @return Content[] |
||
| 119 | 1 | */ |
|
| 120 | 1 | View Code Duplication | protected function findContentsByContentIds(array $contentIds) |
| 132 | |||
| 133 | /** |
||
| 134 | * @param string[] $remoteContentIds |
||
| 135 | * @return Content[] |
||
| 136 | */ |
||
| 137 | View Code Duplication | protected function findContentsByContentRemoteIds(array $remoteContentIds) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param int[] $locationIds |
||
| 152 | * @return Content[] |
||
| 153 | */ |
||
| 154 | View Code Duplication | protected function findContentsByLocationIds(array $locationIds) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param string[] $remoteLocationIds |
||
| 169 | * @return Content[] |
||
| 170 | */ |
||
| 171 | View Code Duplication | protected function findContentsByLocationRemoteIds($remoteLocationIds) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param int[] $parentLocationIds |
||
| 186 | 2 | * @return Content[] |
|
| 187 | */ |
||
| 188 | 2 | protected function findContentsByParentLocationIds($parentLocationIds) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string[] $remoteParentLocationIds |
||
| 206 | * @return Content[] |
||
| 207 | */ |
||
| 208 | View Code Duplication | protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param string[] $contentTypeIdentifiers |
||
| 223 | * @return Content[] |
||
| 224 | */ |
||
| 225 | View Code Duplication | protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param int[] $contentTypeIds |
||
| 249 | * @return Content[] |
||
| 250 | */ |
||
| 251 | View Code Duplication | protected function findContentsByContentTypeIds(array $contentTypeIds) |
|
| 271 | } |
||
| 272 |