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 |
||
12 | class ContentVersionMatcher extends RepositoryMatcher implements MatcherInterface |
||
13 | { |
||
14 | const MATCH_STATUS_DRAFT = 'draft'; |
||
15 | const MATCH_STATUS_PUBLISHED = 'published'; |
||
16 | const MATCH_STATUS_ARCHIVED = 'archived'; |
||
17 | |||
18 | const MATCH_STATUS = 'version_status'; |
||
19 | const MATCH_VERSION = 'version'; |
||
20 | |||
21 | const STATUS_MAP = array( |
||
22 | self::MATCH_STATUS_DRAFT => VersionInfo::STATUS_DRAFT, |
||
23 | self::MATCH_STATUS_PUBLISHED => VersionInfo::STATUS_PUBLISHED, |
||
24 | self::MATCH_STATUS_ARCHIVED => VersionInfo::STATUS_ARCHIVED |
||
25 | ); |
||
26 | |||
27 | protected $allowedConditions = array( |
||
28 | self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
29 | self::MATCH_STATUS, self::MATCH_VERSION, |
||
30 | // aliases |
||
31 | 'status' |
||
32 | ); |
||
33 | protected $returns = 'VersionInfo'; |
||
34 | |||
35 | protected $contentMatcher; |
||
36 | |||
37 | 80 | public function __construct(Repository $repository, ContentMatcher $contentMatcher) |
|
42 | |||
43 | public function match(array $contentConditions, array $versionConditions = array(), $sort = array(), $offset = 0, $limit = 0) |
||
54 | |||
55 | /** |
||
56 | * Like match, but will throw an exception if there are 0 or more than 1 items matching |
||
57 | * |
||
58 | * @param array $conditions |
||
59 | * @return mixed |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | public function matchOne(array $contentConditions, array $versionConditions = array(), $sort = array(), $offset = 0) |
||
71 | |||
72 | /** |
||
73 | * @param array $versionConditions |
||
74 | * @param Content $content |
||
75 | * @return VersionInfo[] key: obj_id/version_no |
||
76 | */ |
||
77 | public function matchContentVersions(array $versionConditions, Content $content) |
||
109 | |||
110 | View Code Duplication | protected function matchAnd(array $conditionsArray, $content) |
|
128 | |||
129 | View Code Duplication | protected function matchOr(array $conditionsArray, $content) |
|
144 | |||
145 | /** |
||
146 | * @param Content $content |
||
147 | * @param string[] $values |
||
148 | * @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
||
149 | */ |
||
150 | protected function findContentVersionsByStatus(Content $content, array $values) |
||
163 | |||
164 | /** |
||
165 | * @param Content $content |
||
166 | * @param int[] $values |
||
167 | * @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
||
168 | */ |
||
169 | protected function findContentVersionsByVersionNo(Content $content, array $values) |
||
195 | |||
196 | /** |
||
197 | * @param Content $content |
||
198 | * @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
||
199 | */ |
||
200 | protected function findAllContentVersions(Content $content) |
||
212 | } |
||
213 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.