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 |
||
| 10 | class LocationMatcher extends QueryBasedMatcher |
||
| 11 | { |
||
| 12 | use FlexibleKeyMatcherTrait; |
||
| 13 | |||
| 14 | const MATCH_DEPTH = 'depth'; |
||
| 15 | const MATCH_IS_MAIN_LOCATION = 'is_main_location'; |
||
| 16 | const MATCH_PRIORITY = 'priority'; |
||
| 17 | |||
| 18 | protected $allowedConditions = array( |
||
| 19 | self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
| 20 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
| 21 | self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP, |
||
| 22 | self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER, |
||
| 23 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE, |
||
| 24 | self::MATCH_VISIBILITY, |
||
| 25 | // aliases |
||
| 26 | 'content_type', 'content_type_id', 'content_type_identifier', |
||
| 27 | // location-only |
||
| 28 | self::MATCH_DEPTH, self::MATCH_IS_MAIN_LOCATION, self::MATCH_PRIORITY, |
||
| 29 | ); |
||
| 30 | protected $returns = 'Location'; |
||
| 31 | |||
| 32 | 1 | /** |
|
| 33 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 34 | 1 | * @return LocationCollection |
|
| 35 | */ |
||
| 36 | public function match(array $conditions) |
||
| 37 | { |
||
| 38 | return $this->matchLocation($conditions); |
||
| 39 | } |
||
| 40 | |||
| 41 | 1 | /** |
|
| 42 | * @param array $conditions key: condition, value: value: int / string / int[] / string[] |
||
| 43 | 1 | * @return LocationCollection |
|
| 44 | */ |
||
| 45 | 1 | public function matchLocation(array $conditions) |
|
| 46 | { |
||
| 47 | 1 | $this->validateConditions($conditions); |
|
| 48 | 1 | ||
| 49 | 1 | View Code Duplication | if (count($conditions) === 1) { |
|
|
|||
| 50 | $condition = reset($conditions); |
||
| 51 | if (is_int($condition) || ctype_digit($condition)) { |
||
| 52 | 1 | return $this->repository->getLocationService()->loadLocation($condition); |
|
| 53 | } elseif (is_string($condition)) { |
||
| 54 | return $this->repository->getLocationService()->loadLocationByRemoteId($condition); |
||
| 55 | 1 | } |
|
| 56 | 1 | } |
|
| 57 | |||
| 58 | 1 | foreach ($conditions as $key => $values) { |
|
| 59 | |||
| 60 | $query = new LocationQuery(); |
||
| 61 | 1 | $query->limit = self::INT_MAX_16BIT; |
|
| 62 | 1 | if (isset($query->performCount)) $query->performCount = false; |
|
| 63 | $query->filter = $this->getQueryCriterion($key, $values); |
||
| 64 | $results = $this->repository->getSearchService()->findLocations($query); |
||
| 65 | |||
| 66 | $locations = []; |
||
| 67 | foreach ($results->searchHits as $result) { |
||
| 68 | $locations[$result->valueObject->id] = $result->valueObject; |
||
| 69 | } |
||
| 70 | |||
| 71 | return new LocationCollection($locations); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * When matching by key, we accept location Id and remote Id only |
||
| 77 | * @param int|string $key |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | protected function getConditionsFromKey($key) |
||
| 87 | |||
| 88 | protected function getQueryCriterion($key, $values) |
||
| 89 | { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns all locations of a set of objects |
||
| 127 | * |
||
| 128 | 1 | * @param int[] $contentIds |
|
| 129 | * @return Location[] |
||
| 130 | 1 | * @deprecated |
|
| 131 | */ |
||
| 132 | 1 | View Code Duplication | protected function findLocationsByContentIds(array $contentIds) |
| 145 | |||
| 146 | /** |
||
| 147 | * Returns all locations of a set of objects |
||
| 148 | * |
||
| 149 | * @param int[] $remoteContentIds |
||
| 150 | * @return Location[] |
||
| 151 | * @deprecated |
||
| 152 | */ |
||
| 153 | View Code Duplication | protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param int[] $locationIds |
||
| 169 | * @return Location[] |
||
| 170 | * @deprecated |
||
| 171 | */ |
||
| 172 | protected function findLocationsByLocationIds(array $locationIds) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param int[] $locationRemoteIds |
||
| 185 | * @return Location[] |
||
| 186 | * @deprecated |
||
| 187 | */ |
||
| 188 | protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param int[] $parentLocationIds |
||
| 202 | * @return Location[] |
||
| 203 | * @deprecated |
||
| 204 | */ |
||
| 205 | View Code Duplication | protected function findLocationsByParentLocationIds($parentLocationIds) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param int[] $parentLocationRemoteIds |
||
| 225 | * @return Location[] |
||
| 226 | * @deprecated |
||
| 227 | */ |
||
| 228 | View Code Duplication | protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
| 239 | } |
||
| 240 |
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.