CollectionsVisitor   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 82
Duplicated Lines 25.61 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 21
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C walkComparison() 0 44 13
A walkComposite() 13 13 3
A dispatch() 8 13 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections;
6
7
use Doctrine\Common\Collections\ExpressionBuilder;
8
use Psi\Component\ObjectAgent\Exception\BadMethodCallException;
9
use Psi\Component\ObjectAgent\Query\Comparison;
10
use Psi\Component\ObjectAgent\Query\Composite;
11
use Psi\Component\ObjectAgent\Query\Expression;
12
13
class CollectionsVisitor
14
{
15
    private $expressionBuilder;
16
17
    public function __construct(ExpressionBuilder $expressionBuilder)
18
    {
19
        $this->expressionBuilder = $expressionBuilder;
20
    }
21
22
    public function dispatch(Expression $expr)
23
    {
24 View Code Duplication
        switch (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
25
            case $expr instanceof Comparison:
26
                return $this->walkComparison($expr);
27
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
28
29
            case $expr instanceof Composite:
30
                return $this->walkComposite($expr);
31
        }
32
33
        throw new \RuntimeException('Unknown Expression: ' . get_class($expr));
34
    }
35
36
    private function walkComparison(Comparison $comparison)
37
    {
38
        $field = $comparison->getField();
39
        $value = $comparison->getValue();
40
41
        switch ($comparison->getComparator()) {
42
            case Comparison::EQUALS:
43
                return $this->expressionBuilder->eq($field, $value);
44
45
            case Comparison::NOT_EQUALS:
46
                return $this->expressionBuilder->neq($field, $value);
47
48
            case Comparison::LESS_THAN:
49
                return $this->expressionBuilder->lt($field, $value);
50
51
            case Comparison::LESS_THAN_EQUAL:
52
                return $this->expressionBuilder->lte($field, $value);
53
54
            case Comparison::GREATER_THAN:
55
                return $this->expressionBuilder->gt($field, $value);
56
57
            case Comparison::GREATER_THAN_EQUAL:
58
                return $this->expressionBuilder->gte($field, $value);
59
60
            case Comparison::IN:
61
                return $this->expressionBuilder->in($field, $value);
62
63
            case Comparison::NOT_IN:
64
                return $this->expressionBuilder->notIn($field, $value);
65
66
            case Comparison::CONTAINS:
67
                return $this->expressionBuilder->contains($field, $value);
68
69
            case Comparison::NOT_CONTAINS:
70
                throw BadMethodCallException::comparisonNotSupported(Comparison::NOT_CONTAINS);
71
            case Comparison::NULL:
72
                return $this->expressionBuilder->isNull($field, $value);
0 ignored issues
show
Unused Code introduced by
The call to ExpressionBuilder::isNull() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
74
            case Comparison::NOT_NULL:
75
                throw BadMethodCallException::comparisonNotSupported(Comparison::NOT_NULL);
76
        }
77
78
        throw new \RuntimeException('Unknown comparator: ' . $comparison->getComparator());
79
    }
80
81 View Code Duplication
    private function walkComposite(Composite $expression)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
    {
83
        $expressions = $expression->getExpressions();
84
85
        $ormExpressions = [];
86
        foreach ($expressions as $index => $childExpression) {
87
            $ormExpressions[] = $this->dispatch($childExpression);
88
        }
89
90
        $method = $expression->getType() == Composite::AND ? 'andX' : 'orX';
91
92
        return call_user_func_array([$this->expressionBuilder, $method], $ormExpressions);
93
    }
94
}
95