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 |
||
21 | class SubtreeIn extends CriterionVisitor |
||
22 | { |
||
23 | /** |
||
24 | * Check if visitor is applicable to current criterion. |
||
25 | * |
||
26 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
27 | * |
||
28 | * @return bool |
||
29 | */ |
||
30 | public function canVisit(Criterion $criterion) |
||
31 | { |
||
32 | return |
||
33 | $criterion instanceof Criterion\Subtree && |
||
34 | ( |
||
35 | ($criterion->operator ?: Operator::IN) === Operator::IN || |
||
36 | $criterion->operator === Operator::EQ |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Returns nested condition common for filter and query contexts. |
||
42 | * |
||
43 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | View Code Duplication | protected function getCondition(Criterion $criterion) |
|
61 | |||
62 | /** |
||
63 | * Map field value to a proper Elasticsearch filter representation. |
||
64 | * |
||
65 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
66 | * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher |
||
67 | * @param array $languageFilter |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function visitFilter(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter) |
||
82 | |||
83 | /** |
||
84 | * Map field value to a proper Elasticsearch query representation. |
||
85 | * |
||
86 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
87 | * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher |
||
88 | * @param array $languageFilter |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function visitQuery(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter) |
||
106 | } |
||
107 |
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.