Completed
Pull Request — 1.2 (#41)
by David
02:06
created

Union::walk()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace SQLParser\Query;
4
5
use Doctrine\DBAL\Connection;
6
use Mouf\MoufInstanceDescriptor;
7
use SQLParser\Node\NodeFactory;
8
use Mouf\MoufManager;
9
use SQLParser\Node\NodeInterface;
10
use SQLParser\Node\Traverser\NodeTraverser;
11
use SQLParser\Node\Traverser\VisitorInterface;
12
13
/**
14
 * This class represents a <code>UNION</code> query. You can use it to generate a SQL query statement
15
 * using the <code>toSql</code> method.
16
 * You can use the <code>QueryResult</code> class if you want to run the query directly.
17
 *
18
 * @author David Négrier <[email protected]>
19
 */
20
class Union implements StatementInterface, NodeInterface
21
{
22
    /**
23
     * @var array|Select[]
24
     */
25
    private $selects;
26
27
    /**
28
     * Union constructor.
29
     * @param Select[] $selects
30
     */
31
    public function __construct(array $selects)
32
    {
33
        $this->selects = $selects;
34
    }
35
36
    /**
37
     * @param MoufManager $moufManager
38
     *
39
     * @return MoufInstanceDescriptor
40
     */
41 View Code Duplication
    public function toInstanceDescriptor(MoufManager $moufManager)
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...
42
    {
43
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
44
        $instanceDescriptor->getProperty('selects')->setValue(NodeFactory::nodeToInstanceDescriptor($this->selects, $moufManager));
45
46
        return $instanceDescriptor;
47
    }
48
49
    /**
50
     * Configure the $instanceDescriptor describing this object (it must already exist as a Mouf instance).
51
     *
52
     * @param MoufManager $moufManager
53
     *
54
     * @return MoufInstanceDescriptor
55
     */
56
    public function overwriteInstanceDescriptor($name, MoufManager $moufManager)
57
    {
58
        //$name = $moufManager->findInstanceName($this);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
59
        $instanceDescriptor = $moufManager->getInstanceDescriptor($name);
60
        $instanceDescriptor->getProperty('selects')->setValue(NodeFactory::nodeToInstanceDescriptor($this->selects, $moufManager));
61
62
        return $instanceDescriptor;
63
    }
64
65
    /**
66
     * Renders the object as a SQL string.
67
     *
68
     * @param array      $parameters
69
     * @param Connection $dbConnection
70
     * @param int|number $indent
71
     * @param int        $conditionsMode
72
     *
73
     * @return string
74
     */
75
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
76
    {
77
        $selectsSql = array_map(function(Select $select) use ($parameters, $dbConnection, $indent, $conditionsMode) {
78
            return $select->toSql($parameters, $dbConnection, $indent, $conditionsMode);
79
        }, $this->selects);
80
81
        $sql = implode(' UNION ', $selectsSql);
82
83
        return $sql;
84
    }
85
86
    /**
87
     * Walks the tree of nodes, calling the visitor passed in parameter.
88
     *
89
     * @param VisitorInterface $visitor
90
     */
91 View Code Duplication
    public function walk(VisitorInterface $visitor)
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...
92
    {
93
        $node = $this;
94
        $result = $visitor->enterNode($node);
95
        if ($result instanceof NodeInterface) {
96
            $node = $result;
97
        }
98
        if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
99
            $this->walkChildren($this->columns, $visitor);
0 ignored issues
show
Bug introduced by
The property columns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
            $this->walkChildren($this->from, $visitor);
0 ignored issues
show
Bug introduced by
The property from does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
101
            $this->walkChildren($this->where, $visitor);
0 ignored issues
show
Bug introduced by
The property where does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
102
            $this->walkChildren($this->group, $visitor);
0 ignored issues
show
Bug introduced by
The property group does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
            $this->walkChildren($this->having, $visitor);
0 ignored issues
show
Bug introduced by
The property having does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
104
            $this->walkChildren($this->order, $visitor);
0 ignored issues
show
Bug introduced by
The property order does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
105
        }
106
107
        return $visitor->leaveNode($node);
108
    }
109
110 View Code Duplication
    private function walkChildren(&$children, VisitorInterface $visitor)
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...
111
    {
112
        if ($children) {
113
            if (is_array($children)) {
114
                foreach ($children as $key => $operand) {
115
                    if ($operand) {
116
                        $result2 = $operand->walk($visitor);
117
                        if ($result2 === NodeTraverser::REMOVE_NODE) {
118
                            unset($children[$key]);
119
                        } elseif ($result2 instanceof NodeInterface) {
120
                            $children[$key] = $result2;
121
                        }
122
                    }
123
                }
124
            } else {
125
                $result2 = $children->walk($visitor);
126
                if ($result2 === NodeTraverser::REMOVE_NODE) {
127
                    $children = null;
128
                } elseif ($result2 instanceof NodeInterface) {
129
                    $children = $result2;
130
                }
131
            }
132
        }
133
    }
134
}
135