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 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_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
| 28 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, |
||
| 29 | // aliases |
||
| 30 | 'content_type', 'content_type_id', 'content_type_identifier' |
||
| 31 | ); |
||
| 32 | protected $returns = 'Content'; |
||
| 33 | 3 | ||
| 34 | /** |
||
| 35 | 3 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
|
| 36 | * @return ContentCollection |
||
| 37 | */ |
||
| 38 | public function match(array $conditions) |
||
| 42 | 3 | ||
| 43 | /** |
||
| 44 | 3 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
|
| 45 | * @return ContentCollection |
||
| 46 | 3 | */ |
|
| 47 | public function matchContent(array $conditions) |
||
| 48 | 3 | { |
|
| 49 | 3 | $this->validateConditions($conditions); |
|
| 50 | 3 | ||
| 51 | foreach ($conditions as $key => $values) { |
||
| 52 | |||
| 53 | 3 | if (!is_array($values)) { |
|
| 54 | 3 | $values = array($values); |
|
| 55 | } |
||
| 56 | 2 | ||
| 57 | 1 | // BC support |
|
| 58 | if ($key == 'content_type') { |
||
| 59 | 2 | if (ctype_digit($key)) { |
|
| 60 | $key = self::MATCH_CONTENT_TYPE_ID; |
||
| 61 | } else { |
||
| 62 | 2 | $key = self::MATCH_CONTENT_TYPE_IDENTIFIER; |
|
| 63 | } |
||
| 64 | } |
||
| 65 | 2 | ||
| 66 | switch ($key) { |
||
| 67 | case self::MATCH_CONTENT_ID: |
||
| 68 | 2 | return new ContentCollection($this->findContentsByContentIds($values)); |
|
| 69 | |||
| 70 | case self::MATCH_LOCATION_ID: |
||
| 71 | 2 | return new ContentCollection($this->findContentsByLocationIds($values)); |
|
| 72 | 2 | ||
| 73 | case self::MATCH_CONTENT_REMOTE_ID: |
||
| 74 | return new ContentCollection($this->findContentsByContentRemoteIds($values)); |
||
| 75 | |||
| 76 | case self::MATCH_LOCATION_REMOTE_ID: |
||
| 77 | return new ContentCollection($this->findContentsByLocationRemoteIds($values)); |
||
| 78 | |||
| 79 | case self::MATCH_PARENT_LOCATION_ID: |
||
| 80 | return new ContentCollection($this->findContentsByParentLocationIds($values)); |
||
| 81 | 3 | ||
| 82 | case self::MATCH_PARENT_LOCATION_REMOTE_ID: |
||
| 83 | 3 | return new ContentCollection($this->findContentsByParentLocationRemoteIds($values)); |
|
| 84 | |||
| 85 | 3 | case 'content_type_id': |
|
| 86 | case self::MATCH_CONTENT_TYPE_ID: |
||
| 87 | 3 | return new ContentCollection($this->findContentsByContentTypeIds($values)); |
|
| 88 | 3 | ||
| 89 | 3 | case 'content_type_identifier': |
|
| 90 | case self::MATCH_CONTENT_TYPE_IDENTIFIER: |
||
| 91 | 3 | return new ContentCollection($this->findContentsByContentTypeIdentifiers($values)); |
|
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * When matching by key, we accept content Id and remote Id only |
||
| 98 | * @param int|string $key |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | protected function getConditionsFromKey($key) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param int[] $contentIds |
||
| 111 | * @return Content[] |
||
| 112 | */ |
||
| 113 | View Code Duplication | protected function findContentsByContentIds(array $contentIds) |
|
| 125 | 1 | ||
| 126 | /** |
||
| 127 | * @param string[] $remoteContentIds |
||
| 128 | * @return Content[] |
||
| 129 | */ |
||
| 130 | View Code Duplication | protected function findContentsByContentRemoteIds(array $remoteContentIds) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param int[] $locationIds |
||
| 145 | * @return Content[] |
||
| 146 | */ |
||
| 147 | View Code Duplication | protected function findContentsByLocationIds(array $locationIds) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string[] $remoteLocationIds |
||
| 162 | * @return Content[] |
||
| 163 | */ |
||
| 164 | View Code Duplication | protected function findContentsByLocationRemoteIds($remoteLocationIds) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param int[] $parentLocationIds |
||
| 179 | * @return Content[] |
||
| 180 | */ |
||
| 181 | protected function findContentsByParentLocationIds($parentLocationIds) |
||
| 196 | 2 | ||
| 197 | 2 | /** |
|
| 198 | * @param string[] $remoteParentLocationIds |
||
| 199 | 2 | * @return Content[] |
|
| 200 | 2 | */ |
|
| 201 | View Code Duplication | protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param string[] $contentTypeIdentifiers |
||
| 216 | * @return Content[] |
||
| 217 | */ |
||
| 218 | View Code Duplication | protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param int[] $contentTypeIds |
||
| 242 | * @return Content[] |
||
| 243 | */ |
||
| 244 | View Code Duplication | protected function findContentsByContentTypeIds(array $contentTypeIds) |
|
| 264 | } |
||
| 265 |
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.