Completed
Pull Request — 1.1 (#22)
by David
10:58
created

WhenConditions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 2
dl 3
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 4 1
A setValue() 0 4 1
A toSql() 3 17 4
A getOperatorSymbol() 0 4 1

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\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 a set of ... WHEN ... THEN ... construct (inside a CASE).
14
 *
15
 * @author David Négrier <[email protected]>
16
 */
17
class WhenConditions extends AbstractManyInstancesOperator
18
{
19
20
    private $value;
21
22
    public function getValue()
23
    {
24
        return $this->value;
25
    }
26
27
    /**
28
     * Sets the value.
29
     *
30
     * @Important
31
     *
32
     * @param NodeInterface|NodeInterface[]|string $value
33
     */
34
    public function setValue($value)
35
    {
36
        $this->value = $value;
37
    }
38
39
    /**
40
     * Renders the object as a SQL string.
41
     *
42
     * @param Connection $dbConnection
43
     * @param array      $parameters
44
     * @param number     $indent
45
     * @param int        $conditionsMode
46
     *
47
     * @return string
48
     */
49
    public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
50
    {
51
        $fullSql = '';
52
53 View Code Duplication
        if ($this->value) {
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...
54
            $fullSql = NodeFactory::toSql($this->value, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
55
        }
56
57
        foreach ($this->getOperands() as $operand) {
58
            $sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
59
            if ($sql != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $sql of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
60
                $fullSql .= "\n".str_repeat(' ', $indent).'WHEN '.$sql;
61
            }
62
        }
63
64
        return $fullSql;
65
    }
66
67
    /**
68
     * Returns the symbol for this operator.
69
     *
70
     * @return string
71
     */
72
    protected function getOperatorSymbol()
73
    {
74
        return 'WHEN';
75
    }
76
}
77