Failed Conditions
Pull Request — master (#6743)
by Grégoire
14:09
created

SqlExpressionVisitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A walkCompositeExpression() 0 17 4
A walkValue() 0 3 1
A __construct() 0 4 1
B walkComparison() 0 17 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Persisters;
6
7
use Doctrine\Common\Collections\Expr\Comparison;
8
use Doctrine\Common\Collections\Expr\CompositeExpression;
9
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
10
use Doctrine\Common\Collections\Expr\Value;
11
use Doctrine\ORM\Mapping\AssociationMetadata;
12
use Doctrine\ORM\Mapping\ClassMetadata;
13
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
14
use Doctrine\ORM\Persisters\MatchingAssociationFieldRequiresObject;
15
use function implode;
16
use function in_array;
17
use function is_object;
18
19
/**
20
 * Visit Expressions and generate SQL WHERE conditions from them.
21
 */
22
class SqlExpressionVisitor extends ExpressionVisitor
23
{
24
    /**
25
     * @var BasicEntityPersister
26
     */
27
    private $persister;
28
29
    /**
30
     * @var ClassMetadata
31
     */
32
    private $classMetadata;
33
34 38
    public function __construct(BasicEntityPersister $persister, ClassMetadata $classMetadata)
35
    {
36 38
        $this->persister     = $persister;
37 38
        $this->classMetadata = $classMetadata;
38 38
    }
39
40
    /**
41
     * Converts a comparison expression into the target query language output.
42
     *
43
     * @return mixed
44
     */
45 38
    public function walkComparison(Comparison $comparison)
46
    {
47 38
        $field    = $comparison->getField();
48 38
        $value    = $comparison->getValue()->getValue(); // shortcut for walkValue()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49 38
        $property = $this->classMetadata->getProperty($field);
50
51 38
        if ($property instanceof AssociationMetadata &&
52 38
            $value !== null &&
53 38
            ! is_object($value) &&
54 38
            ! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true)) {
55 1
            throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation(
56 1
                $this->classMetadata->getClassName(),
57 1
                $field
58
            );
59
        }
60
61 37
        return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator());
62
    }
63
64
    /**
65
     * Converts a composite expression into the target query language output.
66
     *
67
     * @return mixed
68
     *
69
     * @throws \RuntimeException
70
     */
71 4
    public function walkCompositeExpression(CompositeExpression $expr)
72
    {
73 4
        $expressionList = [];
74
75 4
        foreach ($expr->getExpressionList() as $child) {
76 4
            $expressionList[] = $this->dispatch($child);
77
        }
78
79 4
        switch ($expr->getType()) {
80 4
            case CompositeExpression::TYPE_AND:
81 4
                return '(' . implode(' AND ', $expressionList) . ')';
82
83
            case CompositeExpression::TYPE_OR:
84
                return '(' . implode(' OR ', $expressionList) . ')';
85
86
            default:
87
                throw new \RuntimeException('Unknown composite ' . $expr->getType());
88
        }
89
    }
90
91
    /**
92
     * Converts a value expression into the target query language part.
93
     *
94
     * @return mixed
95
     */
96
    public function walkValue(Value $value)
97
    {
98
        return '?';
99
    }
100
}
101