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 |
||
| 9 | class ContentMatcher extends AbstractMatcher |
||
| 10 | { |
||
| 11 | const MATCH_CONTENT_ID = 'content_id'; |
||
| 12 | const MATCH_LOCATION_ID = 'location_id'; |
||
| 13 | const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
||
| 14 | const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
||
| 15 | const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
||
| 16 | const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
||
| 17 | const MATCH_CONTENT_TYPE_IDENTIFIER = 'content_type'; |
||
| 18 | |||
| 19 | protected $allowedConditions = array( |
||
| 20 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
| 21 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER |
||
| 22 | ); |
||
| 23 | protected $returns = 'Content'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 27 | * @return ContentCollection |
||
| 28 | */ |
||
| 29 | public function matchContent(array $conditions) |
||
| 30 | { |
||
| 31 | $conditions = $this->validateConditions($conditions); |
||
|
|
|||
| 32 | |||
| 33 | foreach ($conditions as $key => $values) { |
||
| 34 | |||
| 35 | if (!is_array($values)) { |
||
| 36 | $values = array($values); |
||
| 37 | } |
||
| 38 | |||
| 39 | switch ($key) { |
||
| 40 | case self::MATCH_CONTENT_ID: |
||
| 41 | return new ContentCollection($this->findContentsByContentIds($values)); |
||
| 42 | |||
| 43 | case self::MATCH_LOCATION_ID: |
||
| 44 | return new ContentCollection($this->findContentsByLocationIds($values)); |
||
| 45 | |||
| 46 | case self::MATCH_CONTENT_REMOTE_ID: |
||
| 47 | return new ContentCollection($this->findContentsByContentRemoteIds($values)); |
||
| 48 | |||
| 49 | case self::MATCH_LOCATION_REMOTE_ID: |
||
| 50 | return new ContentCollection($this->findContentsByLocationRemoteIds($values)); |
||
| 51 | |||
| 52 | case self::MATCH_PARENT_LOCATION_ID: |
||
| 53 | return new ContentCollection($this->findContentsByParentLocationIds($values)); |
||
| 54 | |||
| 55 | case self::MATCH_PARENT_LOCATION_REMOTE_ID: |
||
| 56 | return new ContentCollection($this->findContentsByParentLocationRemoteIds($values)); |
||
| 57 | |||
| 58 | case self::MATCH_CONTENT_TYPE_IDENTIFIER: |
||
| 59 | return new ContentCollection($this->findContentsByContentTypeIdentifiers($values)); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @param int[] $contentIds |
||
| 67 | * @return Content[] |
||
| 68 | */ |
||
| 69 | protected function findContentsByContentIds(array $contentIds) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string[] $remoteContentIds |
||
| 82 | * @return Content[] |
||
| 83 | */ |
||
| 84 | protected function findContentsByContentRemoteIds(array $remoteContentIds) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param int[] $locationIds |
||
| 97 | * @return Content[] |
||
| 98 | */ |
||
| 99 | View Code Duplication | protected function findContentsByLocationIds(array $locationIds) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param string[] $remoteLocationIds |
||
| 113 | * @return Content[] |
||
| 114 | */ |
||
| 115 | View Code Duplication | protected function findContentsByLocationRemoteIds($remoteLocationIds) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param int[] $parentLocationIds |
||
| 129 | * @return Content[] |
||
| 130 | */ |
||
| 131 | View Code Duplication | protected function findContentsByParentLocationIds($parentLocationIds) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string[] $remoteParentLocationIds |
||
| 148 | * @return Content[] |
||
| 149 | */ |
||
| 150 | View Code Duplication | protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param string[] $contentTypeIdentifiers |
||
| 164 | * @return Content[] |
||
| 165 | */ |
||
| 166 | View Code Duplication | protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers) |
|
| 180 | } |
||
| 181 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.