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

Union   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 16.19 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 17
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toInstanceDescriptor() 7 7 1
A overwriteInstanceDescriptor() 0 8 1
A toSql() 0 10 1
A walk() 0 13 3
B walkChildren() 10 15 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function walk(VisitorInterface $visitor)
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->selects, $visitor);
100
        }
101
102
        return $visitor->leaveNode($node);
103
    }
104
105
    /**
106
     * @param Select[] $children
107
     * @param VisitorInterface $visitor
108
     */
109
    private function walkChildren(array &$children, VisitorInterface $visitor)
110
    {
111
        if ($children) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $children of type SQLParser\Query\Select[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112 View Code Duplication
            foreach ($children as $key => $operand) {
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...
113
                if ($operand) {
114
                    $result2 = $operand->walk($visitor);
115
                    if ($result2 === NodeTraverser::REMOVE_NODE) {
116
                        unset($children[$key]);
117
                    } elseif ($result2 instanceof NodeInterface) {
118
                        $children[$key] = $result2;
119
                    }
120
                }
121
            }
122
        }
123
    }
124
}
125