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 |
||
| 13 | class LocationMatcher 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 | |||
| 24 | protected $allowedConditions = array( |
||
| 25 | self::MATCH_AND, self::MATCH_OR, |
||
| 26 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
| 27 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID |
||
| 28 | ); |
||
| 29 | protected $returns = 'Location'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | 1 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
|
| 33 | * @return LocationCollection |
||
| 34 | 1 | */ |
|
| 35 | public function match(array $conditions) |
||
| 39 | |||
| 40 | /** |
||
| 41 | 1 | * @param array $conditions key: condition, value: value: int / string / int[] / string[] |
|
| 42 | * @return LocationCollection |
||
| 43 | 1 | */ |
|
| 44 | public function matchLocation(array $conditions) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * When matching by key, we accept location Id and remote Id only |
||
| 84 | * @param int|string $key |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | protected function getConditionsFromKey($key) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns all locations of a set of objects |
||
| 97 | * |
||
| 98 | * @param int[] $contentIds |
||
| 99 | * @return Location[] |
||
| 100 | */ |
||
| 101 | View Code Duplication | protected function findLocationsByContentIds(array $contentIds) |
|
| 112 | |||
| 113 | 1 | /** |
|
| 114 | * Returns all locations of a set of objects |
||
| 115 | 1 | * |
|
| 116 | * @param int[] $remoteContentIds |
||
| 117 | 1 | * @return Location[] |
|
| 118 | 1 | */ |
|
| 119 | 1 | View Code Duplication | protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
| 130 | 1 | ||
| 131 | /** |
||
| 132 | 1 | * @param int[] $locationIds |
|
| 133 | 1 | * @return Location[] |
|
| 134 | 1 | */ |
|
| 135 | protected function findLocationsByLocationIds(array $locationIds) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int[] $locationRemoteIds |
||
| 148 | * @return Location[] |
||
| 149 | */ |
||
| 150 | protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param int[] $parentLocationIds |
||
| 163 | * @return Location[] |
||
| 164 | */ |
||
| 165 | protected function findLocationsByParentLocationIds($parentLocationIds) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int[] $parentLocationRemoteIds |
||
| 184 | * @return Location[] |
||
| 185 | */ |
||
| 186 | View Code Duplication | protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
| 197 | } |
||
| 198 |