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

SqlExpressionVisitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 0
loc 73
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 __construct() 0 4 1
A walkCompositeExpression() 0 17 4
A walkValue() 0 3 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 function implode;
15
use function in_array;
16
use function is_object;
17
18
/**
19
 * Visit Expressions and generate SQL WHERE conditions from them.
20
 */
21
class SqlExpressionVisitor extends ExpressionVisitor
22
{
23
    /** @var BasicEntityPersister */
24
    private $persister;
25
26
    /** @var ClassMetadata */
27
    private $classMetadata;
28
29 38
    public function __construct(BasicEntityPersister $persister, ClassMetadata $classMetadata)
30
    {
31 38
        $this->persister     = $persister;
32 38
        $this->classMetadata = $classMetadata;
33 38
    }
34
35
    /**
36
     * Converts a comparison expression into the target query language output.
37
     *
38
     * @return mixed
39
     */
40 38
    public function walkComparison(Comparison $comparison)
41
    {
42 38
        $field    = $comparison->getField();
43 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...
44 38
        $property = $this->classMetadata->getProperty($field);
45
46 38
        if ($property instanceof AssociationMetadata &&
47 38
            $value !== null &&
48 38
            ! is_object($value) &&
49 38
            ! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true)) {
50 1
            throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation(
51 1
                $this->classMetadata->getClassName(),
52 1
                $field
53
            );
54
        }
55
56 37
        return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator());
57
    }
58
59
    /**
60
     * Converts a composite expression into the target query language output.
61
     *
62
     * @return mixed
63
     *
64
     * @throws \RuntimeException
65
     */
66 4
    public function walkCompositeExpression(CompositeExpression $expr)
67
    {
68 4
        $expressionList = [];
69
70 4
        foreach ($expr->getExpressionList() as $child) {
71 4
            $expressionList[] = $this->dispatch($child);
72
        }
73
74 4
        switch ($expr->getType()) {
75 4
            case CompositeExpression::TYPE_AND:
76 4
                return '(' . implode(' AND ', $expressionList) . ')';
77
78
            case CompositeExpression::TYPE_OR:
79
                return '(' . implode(' OR ', $expressionList) . ')';
80
81
            default:
82
                throw new \RuntimeException('Unknown composite ' . $expr->getType());
83
        }
84
    }
85
86
    /**
87
     * Converts a value expression into the target query language part.
88
     *
89
     * @return mixed
90
     */
91
    public function walkValue(Value $value)
92
    {
93
        return '?';
94
    }
95
}
96