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 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 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) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Map field value to a proper Elasticsearch query representation. |
||
| 80 | * |
||
| 81 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
| 82 | * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher |
||
| 83 | * @param array $languageFilter |
||
| 84 | * |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function visitQuery(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter) |
||
| 96 | } |
||
| 97 |
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.