Completed
Push — master ( e41e91...2b4213 )
by Aleh
01:47 queued 01:39
created

Token::add()   D

Complexity

Conditions 25
Paths 52

Size

Total Lines 48
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
rs 4.8642
cc 25
eloc 42
nc 52
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Padawan\Domain\Completion;
4
5
class Token
6
{
7
    public function __construct($code, $symbol)
8
    {
9
        if (empty($code) && empty($symbol)) {
10
            $this->type = self::T_EMPTY;
11
            return;
12
        }
13
        $this->add($code, $symbol);
14
    }
15
16
    public function add($code, $symbol)
17
    {
18
        switch ($code) {
19
        case T_WHITESPACE:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
20
            $this->addType(self::T_WHITESPACE);
21
        case T_NS_SEPARATOR:
22
            $this->addType(self::T_CONTINUE_PROCESS);
23
            break;
24
        case T_STRING:
25
            $this->addType(self::T_STRING);
26
            $this->addType(self::T_CONTINUE_PROCESS);
27
            break;
28
        case T_VARIABLE:
29
        case T_DOUBLE_COLON:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
30
            if ($this->isWhitespace()) {
31
                $this->resetType(self::T_UNKNOWN);
32
                break;
33
            }
34
        case T_OBJECT_OPERATOR:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
35
            if ($this->hasString() && $this->hasWhitespace()) {
36
                $this->resetType(self::T_UNKNOWN);
37
                break;
38
            }
39
        case T_NAMESPACE:
40
        case T_USE:
41
        case T_NEW:
42
        case T_EXTENDS:
43
        case T_IMPLEMENTS:
44
        case '$':
45
        case '(':
46
            $this->resetType(self::$MAP[$code]);
47
            break;
48
        case ';':
49
        case ',':
50
        case '-':
51
        case ':':
52
        case '=':
53
        case ')':
54
        case ']':
55
            $this->resetType(self::T_TERMINATE);
56
            break;
57
        default:
58
            $this->addType(self::T_UNKNOWN);
59
        }
60
        if (!$this->isReady()) {
61
            $this->symbol = $symbol . $this->symbol;
62
        }
63
    }
64
65
    public function getSymbol()
66
    {
67
        return $this->symbol;
68
    }
69
70
    public function getType()
71
    {
72
        return $this->type;
73
    }
74
75
    public function isUnknown()
76
    {
77
        return (bool) ($this->type & self::T_UNKNOWN);
78
    }
79
80
    public function isReady()
81
    {
82
        return !((bool) ($this->type & self::T_CONTINUE_PROCESS));
83
    }
84
85
    public function isTerminate()
86
    {
87
        return (bool) ($this->type & self::T_TERMINATE);
88
    }
89
90
    public function isObjectOperator()
91
    {
92
        return (bool) ($this->type & self::T_OBJECT_OPERATOR);
93
    }
94
95
    public function isStaticOperator()
96
    {
97
        return (bool) ($this->type & self::T_STATIC_OPERATOR);
98
    }
99
100
    public function isUseOperator()
101
    {
102
        return (bool) ($this->type & self::T_USE_OPERATOR);
103
    }
104
105
    public function isNamespaceOperator()
106
    {
107
        return (bool) ($this->type & self::T_NAMESPACE_OPERATOR);
108
    }
109
110
    public function isExtendsOperator()
111
    {
112
        return (bool) ($this->type & self::T_EXTENDS_OPERATOR);
113
    }
114
115
    public function isImplementsOperator()
116
    {
117
        return (bool) ($this->type & self::T_IMPLEMENTS_OPERATOR);
118
    }
119
120
    public function isNewOperator()
121
    {
122
        return (bool) ($this->type & self::T_NEW_OPERATOR);
123
    }
124
125
    public function isVar()
126
    {
127
        return (bool) ($this->type & self::T_VAR);
128
    }
129
130
    public function isWhitespace()
131
    {
132
        return (bool) ($this->type & self::T_WHITESPACE);
133
    }
134
135
    public function hasWhitespace()
136
    {
137
        return $this->isWhitespace();
138
    }
139
140
    public function hasString()
141
    {
142
        return (bool) ($this->type & self::T_STRING);
143
    }
144
145
    public function isString()
146
    {
147
        return $this->type === self::T_STRING
148
            || $this->type === (self::T_STRING | self::T_CONTINUE_PROCESS);
149
    }
150
151
    public function isMethodCall()
152
    {
153
        return (bool) ($this->type & self::T_METHOD_CALL);
154
    }
155
156
    public function isEmpty()
157
    {
158
        return (bool) ($this->type & self::T_EMPTY);
159
    }
160
161
    protected function resetType($type = 0)
162
    {
163
        $this->type = $type;
164
    }
165
    protected function addType($type)
166
    {
167
        $this->type |= $type;
168
    }
169
170
    /**
171
     * @param integer $type
172
     */
173
    protected function removeType($type)
174
    {
175
        if((bool) ($this->type & $type)){
176
            $this->type ^= $type;
177
        }
178
    }
179
180
    const T_UNKNOWN             = 0;
181
    const T_CONTINUE_PROCESS    = 1;
182
    const T_TERMINATE           = 2;
183
    const T_OBJECT_OPERATOR     = 4;
184
    const T_STATIC_OPERATOR     = 8;
185
    const T_USE_OPERATOR        = 16;
186
    const T_NAMESPACE_OPERATOR  = 32;
187
    const T_EXTENDS_OPERATOR    = 64;
188
    const T_IMPLEMENTS_OPERATOR = 128;
189
    const T_NEW_OPERATOR        = 256;
190
    const T_VAR                 = 512;
191
    const T_WHITESPACE          = 1024;
192
    const T_METHOD_CALL         = 2048;
193
    const T_STRING              = 4096;
194
    const T_EMPTY               = 8192;
195
196
    protected static $MAP = [
197
        T_VARIABLE              => Token::T_VAR,
198
        T_OBJECT_OPERATOR       => Token::T_OBJECT_OPERATOR,
199
        T_DOUBLE_COLON          => Token::T_STATIC_OPERATOR,
200
        T_USE                   => Token::T_USE_OPERATOR,
201
        T_NAMESPACE             => Token::T_NAMESPACE_OPERATOR,
202
        T_NEW                   => Token::T_NEW_OPERATOR,
203
        T_EXTENDS               => Token::T_EXTENDS_OPERATOR,
204
        T_IMPLEMENTS            => Token::T_IMPLEMENTS_OPERATOR,
205
        '$'                     => Token::T_VAR,
206
        '('                     => Token::T_METHOD_CALL
207
    ];
208
209
    private $symbol = "";
210
    private $type   = 0;
211
212
}
213