Parenthesis   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 19.64 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 22
loc 112
rs 10
c 0
b 0
f 0
ccs 33
cts 33
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createWhereCondition() 0 4 1
A where() 0 4 1
A andWhere() 0 6 2
A orWhere() 0 6 2
A parenthesis() 0 4 1
A andParenthesis() 11 11 2
A orParenthesis() 11 11 2
A close() 0 4 1
A getExpression() 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 ORM\QueryBuilder;
4
5
/**
6
 * Class Parenthesis
7
 *
8
 * @package ORM
9
 * @author  Thomas Flori <[email protected]>
10
 */
11
class Parenthesis implements ParenthesisInterface
12
{
13
    /** Where conditions get concatenated with space
14
     * @var string[] */
15
    protected $where = [];
16
17
    /** Callback to close the parenthesis
18
     * @var callable */
19
    protected $onClose;
20
21
    /** Parent parenthesis or query
22
     * @var ParenthesisInterface */
23
    protected $parent;
24
25
    /**
26
     * Constructor
27
     *
28
     * Create a parenthesis inside another parenthesis or a query.
29
     *
30
     * @param callable             $onClose Callable that gets executed when the parenthesis get closed
31
     * @param ParenthesisInterface $parent  Parent where createWhereCondition get executed
32
     */
33 43
    public function __construct(
34
        callable $onClose,
35
        ParenthesisInterface $parent
36
    ) {
37 43
        $this->onClose = $onClose;
38 43
        $this->parent  = $parent;
39 43
    }
40
41
    /**
42
     * Create the where condition
43
     *
44
     * Calls createWhereCondition() from parent if there is a parent.
45
     *
46
     * @param string $column   Column or expression with placeholders
47
     * @param string $operator Operator, value or array of values
48
     * @param string $value    Value (required when used with operator)
49
     * @return string
50
     * @internal
51
     */
52 43
    public function createWhereCondition($column, $operator = null, $value = null)
53
    {
54 43
        return $this->parent->createWhereCondition($column, $operator, $value);
55
    }
56
57
    /** {@inheritdoc} */
58 122
    public function where($column, $operator = null, $value = null)
59
    {
60 122
        return $this->andWhere($column, $operator, $value);
61
    }
62
63
    /** {@inheritdoc} */
64 122
    public function andWhere($column, $operator = null, $value = null)
65
    {
66 122
        $this->where[] = (!empty($this->where) ? 'AND ' : '') . $this->createWhereCondition($column, $operator, $value);
67
68 121
        return $this;
69
    }
70
71
    /** {@inheritdoc} */
72 11
    public function orWhere($column, $operator = null, $value = null)
73
    {
74 11
        $this->where[] = (!empty($this->where) ? 'OR ' : '') . $this->createWhereCondition($column, $operator, $value);
75
76 11
        return $this;
77
    }
78
79
    /** {@inheritdoc} */
80 11
    public function parenthesis()
81
    {
82 11
        return $this->andParenthesis();
83
    }
84
85
    /** {@inheritdoc} */
86 30 View Code Duplication
    public function andParenthesis()
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...
87
    {
88 30
        return new Parenthesis(
89 30
            function (ParenthesisInterface $parenthesis) {
90 30
                $this->where[] = (!empty($this->where) ? 'AND ' : '') . $parenthesis->getExpression();
91
92 30
                return $this;
93 30
            },
94
            $this
95
        );
96
    }
97 30
98
    /** {@inheritdoc} */
99 View Code Duplication
    public function orParenthesis()
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...
100
    {
101 9
        return new Parenthesis(
102
            function (ParenthesisInterface $parenthesis) {
103 9
                $this->where[] = (!empty($this->where) ? 'OR ' : '') . $parenthesis->getExpression();
104 9
105 9
                return $this;
106
            },
107 9
            $this
108 9
        );
109
    }
110
111
    /** {@inheritdoc} */
112 9
    public function close()
113
    {
114
        return call_user_func($this->onClose, $this);
115
    }
116 43
117
    /** {@inheritdoc} */
118 43
    public function getExpression()
119
    {
120
        return '(' . implode(' ', $this->where) . ')';
121
    }
122
}
123