SimpleFunction   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 169
Duplicated Lines 11.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
c 2
b 0
f 0
lcom 1
cbo 2
dl 20
loc 169
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseExpression() 0 4 1
A setBaseExpression() 0 4 1
A getSubTree() 0 4 1
A setSubTree() 0 5 1
A getAlias() 0 4 1
A setAlias() 0 4 1
A getDiretion() 0 4 1
A setDirection() 0 4 1
A hasBrackets() 0 4 1
A toInstanceDescriptor() 0 10 1
B toSql() 0 20 5
B walk() 20 20 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
/**
4
 * expression-types.php.
5
 *
6
 *
7
 * Copyright (c) 2010-2013, Justin Swanhart
8
 * with contributions by André Rothe <[email protected], [email protected]>
9
 * and David Négrier <[email protected]>
10
 *
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification,
14
 * are permitted provided that the following conditions are met:
15
 *
16
 *   * Redistributions of source code must retain the above copyright notice,
17
 *     this list of conditions and the following disclaimer.
18
 *   * Redistributions in binary form must reproduce the above copyright notice,
19
 *     this list of conditions and the following disclaimer in the documentation
20
 *     and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31
 * DAMAGE.
32
 */
33
namespace SQLParser\Node;
34
35
use Doctrine\DBAL\Connection;
36
use Mouf\MoufInstanceDescriptor;
37
use Mouf\MoufManager;
38
use SQLParser\Node\Traverser\NodeTraverser;
39
use SQLParser\Node\Traverser\VisitorInterface;
40
41
/**
42
 * This class represents a node that is an SQL function call.
43
 *
44
 * @author David Négrier <[email protected]>
45
 */
46
class SimpleFunction implements NodeInterface
47
{
48
    private $baseExpression;
49
50
    /**
51
     * Returns the base expression (the string that generated this expression).
52
     *
53
     * @return string
54
     */
55
    public function getBaseExpression()
56
    {
57
        return $this->baseExpression;
58
    }
59
60
    /**
61
     * Sets the base expression (the string that generated this expression).
62
     *
63
     * @param string $baseExpression
64
     */
65
    public function setBaseExpression($baseExpression)
66
    {
67
        $this->baseExpression = $baseExpression;
68
    }
69
70
    private $subTree;
71
72
    public function getSubTree()
73
    {
74
        return $this->subTree;
75
    }
76
77
    /**
78
     * Sets the subtree.
79
     *
80
     * @Important
81
     *
82
     * @param array<NodeInterface>|NodeInterface $subTree
83
     */
84
    public function setSubTree($subTree)
85
    {
86
        $this->subTree = $subTree;
87
        $this->subTree = NodeFactory::simplify($this->subTree);
88
    }
89
90
    private $alias;
91
92
    public function getAlias()
93
    {
94
        return $this->alias;
95
    }
96
97
    /**
98
     * Sets the alias.
99
     *
100
     * @Important
101
     *
102
     * @param string $alias
103
     */
104
    public function setAlias($alias)
105
    {
106
        $this->alias = $alias;
107
    }
108
109
    private $direction;
110
111
    public function getDiretion()
112
    {
113
        return $this->direction;
114
    }
115
116
    /**
117
     * Sets the direction.
118
     *
119
     * @Important
120
     *
121
     * @param string $direction
122
     */
123
    public function setDirection($direction)
124
    {
125
        $this->direction = $direction;
126
    }
127
128
    private $brackets = false;
129
130
    /**
131
     * Returns true if the expression is between brackets.
132
     *
133
     * @return bool
134
     */
135
    public function hasBrackets()
136
    {
137
        return $this->brackets;
138
    }
139
140
    /**
141
     * Returns a Mouf instance descriptor describing this object.
142
     *
143
     * @param MoufManager $moufManager
144
     *
145
     * @return MoufInstanceDescriptor
146
     */
147
    public function toInstanceDescriptor(MoufManager $moufManager)
148
    {
149
        $instanceDescriptor = $moufManager->createInstance(get_called_class());
150
        $instanceDescriptor->getProperty('baseExpression')->setValue(NodeFactory::nodeToInstanceDescriptor($this->baseExpression, $moufManager));
151
        $instanceDescriptor->getProperty('subTree')->setValue(NodeFactory::nodeToInstanceDescriptor($this->subTree, $moufManager));
152
        $instanceDescriptor->getProperty('alias')->setValue(NodeFactory::nodeToInstanceDescriptor($this->alias, $moufManager));
153
        $instanceDescriptor->getProperty('direction')->setValue(NodeFactory::nodeToInstanceDescriptor($this->direction, $moufManager));
154
155
        return $instanceDescriptor;
156
    }
157
158
    /**
159
     * Renders the object as a SQL string.
160
     *
161
     * @param Connection $dbConnection
162
     * @param array      $parameters
163
     * @param number     $indent
164
     * @param int        $conditionsMode
165
     *
166
     * @return string
167
     */
168
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
169
    {
170
        $sql = '';
171
        if (!empty($this->baseExpression)) {
172
            $sql .= $this->baseExpression.'(';
173
        }
174
        $sql .= NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ',', false, $indent, $conditionsMode);
175
        if (!empty($this->baseExpression)) {
176
            $sql .= ')';
177
        }
178
179
        if ($this->alias) {
180
            $sql .= ' AS '.$this->alias;
181
        }
182
        if ($this->direction) {
183
            $sql .= ' '.$this->direction;
184
        }
185
186
        return $sql;
187
    }
188
189
    /**
190
     * Walks the tree of nodes, calling the visitor passed in parameter.
191
     *
192
     * @param VisitorInterface $visitor
193
     */
194 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...
195
    {
196
        $node = $this;
197
        $result = $visitor->enterNode($node);
198
        if ($result instanceof NodeInterface) {
199
            $node = $result;
200
        }
201
        if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
202
            foreach ($this->subTree as $key => $operand) {
0 ignored issues
show
Bug introduced by
The expression $this->subTree of type array<integer,object<SQL...ser\Node\NodeInterface> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
203
                $result2 = $operand->walk($visitor);
204
                if ($result2 === NodeTraverser::REMOVE_NODE) {
205
                    unset($this->subTree[$key]);
206
                } elseif ($result2 instanceof NodeInterface) {
207
                    $this->subTree[$key] = $result2;
208
                }
209
            }
210
        }
211
212
        return $visitor->leaveNode($node);
213
    }
214
}
215