Completed
Push — 1.3 ( d5fcdd...2de017 )
by David
05:06 queued 40s
created

AbstractTwoOperandsOperator::toSql()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 7.6666
c 0
b 0
f 0
cc 10
nc 14
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SQLParser\Node;
4
5
use Mouf\Utils\Common\ConditionInterface\ConditionTrait;
6
use Doctrine\DBAL\Connection;
7
use Mouf\MoufManager;
8
use Mouf\MoufInstanceDescriptor;
9
use SQLParser\Node\Traverser\NodeTraverser;
10
use SQLParser\Node\Traverser\VisitorInterface;
11
12
/**
13
 * This class represents an operation that takes 2 operands (for instance =, <, >, etc...) in an SQL expression.
14
 *
15
 * @author David Négrier <[email protected]>
16
 */
17
abstract class AbstractTwoOperandsOperator implements NodeInterface
18
{
19
    use ConditionTrait;
20
21
    private $leftOperand;
22
23
    public function getLeftOperand()
24
    {
25
        return $this->leftOperand;
26
    }
27
28
    /**
29
     * Sets the leftOperand.
30
     *
31
     * @Important
32
     *
33
     * @param NodeInterface|NodeInterface[]|string $leftOperand
34
     */
35
    public function setLeftOperand($leftOperand)
36
    {
37
        $this->leftOperand = $leftOperand;
38
    }
39
40
    private $rightOperand;
41
42
    public function getRightOperand()
43
    {
44
        return $this->rightOperand;
45
    }
46
47
    /**
48
     * Sets the rightOperand.
49
     *
50
     * @Important
51
     *
52
     * @param string|NodeInterface|NodeInterface[] $rightOperand
53
     */
54
    public function setRightOperand($rightOperand)
55
    {
56
        $this->rightOperand = $rightOperand;
57
    }
58
59
    /**
60
     * Returns a Mouf instance descriptor describing this object.
61
     *
62
     * @param MoufManager $moufManager
63
     *
64
     * @return MoufInstanceDescriptor
65
     */
66
    public function toInstanceDescriptor(MoufManager $moufManager)
67
    {
68
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
69
        $instanceDescriptor->getProperty('leftOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->leftOperand, $moufManager));
70
        $instanceDescriptor->getProperty('rightOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->rightOperand, $moufManager));
71
72 View Code Duplication
        if ($this->leftOperand instanceof Parameter) {
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...
73
            // Let's add a condition on the parameter.
74
            $conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition');
75
            $conditionDescriptor->getProperty('parameterName')->setValue($this->leftOperand->getName());
76
            $instanceDescriptor->getProperty('condition')->setValue($conditionDescriptor);
77
        }
78
        // TODO: manage cases where both leftOperand and rightOperand are parameters.
79 View Code Duplication
        if ($this->rightOperand instanceof Parameter) {
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...
80
            // Let's add a condition on the parameter.
81
            $conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition');
82
            $conditionDescriptor->getProperty('parameterName')->setValue($this->rightOperand->getName());
83
            $instanceDescriptor->getProperty('condition')->setValue($conditionDescriptor);
84
        }
85
86
        return $instanceDescriptor;
87
    }
88
89
    /**
90
     * Renders the object as a SQL string.
91
     *
92
     * @param Connection $dbConnection
93
     * @param array      $parameters
94
     * @param number     $indent
95
     * @param int        $conditionsMode
96
     *
97
     * @return string
98
     */
99
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
100
    {
101
        if ($conditionsMode == self::CONDITION_GUESS) {
102
            $bypass = false;
103
            if ($this->leftOperand instanceof BypassableInterface && $this->leftOperand->canBeBypassed($parameters)) {
104
                $bypass = true;
105
            }
106
            if ($this->rightOperand instanceof BypassableInterface && $this->rightOperand->canBeBypassed($parameters)) {
107
                $bypass = true;
108
            }
109
            if ($bypass === true) {
110
                return;
111
            } else {
112
                $conditionsMode = self::CONDITION_IGNORE;
113
            }
114
        }
115
        if ($conditionsMode == self::CONDITION_IGNORE || !$this->condition || $this->condition->isOk($parameters)) {
116
            $sql = $this->getSql($parameters, $dbConnection, $indent, $conditionsMode, $extrapolateParameters);
117
        } else {
118
            $sql = null;
119
        }
120
121
        return $sql;
122
    }
123
124
    protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
125
    {
126
        $sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
127
        $sql .= ' '.$this->getOperatorSymbol().' ';
128
        $sql .= NodeFactory::toSql($this->rightOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
129
130
        return $sql;
131
    }
132
133
    /**
134
     * Walks the tree of nodes, calling the visitor passed in parameter.
135
     *
136
     * @param VisitorInterface $visitor
137
     */
138
    public function walk(VisitorInterface $visitor)
139
    {
140
        $node = $this;
141
        $result = $visitor->enterNode($node);
142
        if ($result instanceof NodeInterface) {
143
            $node = $result;
144
        }
145
        if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
146
            $result2 = $this->leftOperand->walk($visitor);
147 View Code Duplication
            if ($result2 === NodeTraverser::REMOVE_NODE) {
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...
148
                return NodeTraverser::REMOVE_NODE;
149
            } elseif ($result2 instanceof NodeInterface) {
150
                $this->leftOperand = $result2;
151
            }
152
153
            $result2 = $this->rightOperand->walk($visitor);
154
            if ($result2 === NodeTraverser::REMOVE_NODE) {
155
                return NodeTraverser::REMOVE_NODE;
156
            } elseif ($result2 instanceof NodeInterface) {
157
                $this->rightOperand = $result2;
158
            }
159
        }
160
161
        return $visitor->leaveNode($node);
162
    }
163
164
    /**
165
     * Returns the symbol for this operator.
166
     */
167
    abstract protected function getOperatorSymbol();
168
}
169