Completed
Pull Request — 1.4 (#60)
by David
01:39
created

AbstractInListOperator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSql() 0 21 4
A refactorParameterToExpression() 0 10 2
A getParameter() 0 14 4
1
<?php
2
3
namespace SQLParser\Node;
4
use Doctrine\DBAL\Connection;
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Mouf\Database\MagicQueryException;
7
8
/**
9
 * This class represents an In operation in an SQL expression.
10
 *
11
 * @author David Négrier <[email protected]>
12
 */
13
abstract class AbstractInListOperator extends AbstractTwoOperandsOperator
14
{
15
    protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
16
    {
17
        $rightOperand = $this->getRightOperand();
18
19
        $rightOperand = $this->refactorParameterToExpression($rightOperand);
20
21
        $this->setRightOperand($rightOperand);
22
23
        $parameterNode = $this->getParameter($rightOperand);
24
25
        if ($parameterNode !== null) {
26
            if (!isset($parameters[$parameterNode->getName()])) {
27
                throw new MagicQueryException("Missing parameter '" . $parameterNode->getName() . "' for 'IN' operand.");
28
            }
29
            if ($parameters[$parameterNode->getName()] === []) {
30
                return "FALSE";
31
            }
32
        }
33
34
        return parent::getSql($parameters, $platform, $indent, $conditionsMode, $extrapolateParameters);
35
    }
36
37
    protected function refactorParameterToExpression(NodeInterface $rightOperand): NodeInterface
38
    {
39
        if ($rightOperand instanceof Parameter) {
40
            $expression = new Expression();
41
            $expression->setSubTree([$rightOperand]);
42
            $expression->setBrackets(true);
43
            return $expression;
44
        }
45
        return $rightOperand;
46
    }
47
48
    protected function getParameter(NodeInterface $operand): ?Parameter
49
    {
50
        if (!$operand instanceof Expression) {
51
            return null;
52
        }
53
        $subtree = $operand->getSubTree();
54
        if (!isset($subtree[0])) {
55
            return null;
56
        }
57
        if ($subtree[0] instanceof Parameter) {
58
            return $subtree[0];
59
        }
60
        return null;
61
    }
62
}
63