ExpressionToken   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 154
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
c 2
b 0
f 0
lcom 3
cbo 0
dl 11
loc 154
rs 9.8

26 Methods

Rating   Name   Duplication   Size   Complexity  
A addToken() 0 4 1
A isEnclosedWithinParenthesis() 0 4 2
A setSubTree() 0 4 1
A getSubTree() 0 4 1
A getUpper() 0 4 2
A getTrim() 0 4 2
A getToken() 0 4 2
A setTokenType() 0 4 1
A isWhitespaceToken() 0 4 1
A isCommaToken() 0 4 1
A isVariableToken() 0 4 1
A isSubQueryToken() 0 4 1
A isExpression() 0 4 1
A isBracketExpression() 0 4 1
A isOperator() 0 4 1
A isInList() 0 4 1
A isFunction() 0 4 1
A isUnspecified() 0 4 1
A isAggregateFunction() 0 4 1
A isColumnReference() 0 4 1
A isConstant() 0 4 1
A isSign() 0 4 1
A isSubQuery() 0 4 1
A toArray() 0 4 1
A __construct() 0 10 1
A endsWith() 11 11 2

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;
4
5
class ExpressionToken
6
{
7
    private $subTree;
8
    private $expression;
9
    private $key;
10
    private $token;
11
    private $tokenType;
12
    private $trim;
13
    private $upper;
14
15
    public function __construct($key = '', $token = '')
16
    {
17
        $this->subTree = false;
18
        $this->expression = '';
19
        $this->key = $key;
20
        $this->token = $token;
21
        $this->tokenType = false;
22
        $this->trim = trim($token);
23
        $this->upper = strtoupper($this->trim);
24
    }
25
26
    # TODO: we could replace it with a constructor new ExpressionToken(this, "*")
27
    public function addToken($string)
28
    {
29
        $this->token .= $string;
30
    }
31
32
    public function isEnclosedWithinParenthesis()
33
    {
34
        return ($this->upper[0] === '(' && substr($this->upper, -1) === ')');
35
    }
36
37
    public function setSubTree($tree)
38
    {
39
        $this->subTree = $tree;
40
    }
41
42
    public function getSubTree()
43
    {
44
        return $this->subTree;
45
    }
46
47
    public function getUpper($idx = false)
48
    {
49
        return $idx !== false ? $this->upper[$idx] : $this->upper;
50
    }
51
52
    public function getTrim($idx = false)
53
    {
54
        return $idx !== false ? $this->trim[$idx] : $this->trim;
55
    }
56
57
    public function getToken($idx = false)
58
    {
59
        return $idx !== false ? $this->token[$idx] : $this->token;
60
    }
61
62
    public function setTokenType($type)
63
    {
64
        $this->tokenType = $type;
65
    }
66
67 View Code Duplication
    public function endsWith($needle)
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...
68
    {
69
        $length = strlen($needle);
70
        if ($length == 0) {
71
            return true;
72
        }
73
74
        $start = $length * -1;
75
76
        return (substr($this->token, $start) === $needle);
77
    }
78
79
    public function isWhitespaceToken()
80
    {
81
        return ($this->trim === '');
82
    }
83
84
    public function isCommaToken()
85
    {
86
        return ($this->trim === ',');
87
    }
88
89
    public function isVariableToken()
90
    {
91
        return $this->upper[0] === '@';
92
    }
93
94
    public function isSubQueryToken()
95
    {
96
        return preg_match('/^\\(\\s*SELECT/i', $this->trim);
97
    }
98
99
    public function isExpression()
100
    {
101
        return $this->tokenType === ExpressionType::EXPRESSION;
102
    }
103
104
    public function isBracketExpression()
105
    {
106
        return $this->tokenType === ExpressionType::BRACKET_EXPRESSION;
107
    }
108
109
    public function isOperator()
110
    {
111
        return $this->tokenType === ExpressionType::OPERATOR;
112
    }
113
114
    public function isInList()
115
    {
116
        return $this->tokenType === ExpressionType::IN_LIST;
117
    }
118
119
    public function isFunction()
120
    {
121
        return $this->tokenType === ExpressionType::SIMPLE_FUNCTION;
122
    }
123
124
    public function isUnspecified()
125
    {
126
        return ($this->tokenType === false);
127
    }
128
129
    public function isAggregateFunction()
130
    {
131
        return $this->tokenType === ExpressionType::AGGREGATE_FUNCTION;
132
    }
133
134
    public function isColumnReference()
135
    {
136
        return $this->tokenType === ExpressionType::COLREF;
137
    }
138
139
    public function isConstant()
140
    {
141
        return $this->tokenType === ExpressionType::CONSTANT;
142
    }
143
144
    public function isSign()
145
    {
146
        return $this->tokenType === ExpressionType::SIGN;
147
    }
148
149
    public function isSubQuery()
150
    {
151
        return $this->tokenType === ExpressionType::SUBQUERY;
152
    }
153
154
    public function toArray()
155
    {
156
        return array('expr_type' => $this->tokenType, 'base_expr' => $this->token, 'sub_tree' => $this->subTree);
157
    }
158
}
159