Test Failed
Push — master ( 31f356...780ea5 )
by Alexey
02:13
created

UsesBlockParser::isBrace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Reflection\ClassUseStatements;
6
7
/**
8
 * Class UsesBlockParser
9
 * @package Reflection
10
 */
11
class UsesBlockParser {
12
13
    /**
14
     * @const string
15
     */
16
    const USE_STATEMENT_TYPE = 'use';
17
18
    /**
19
     * @const string
20
     */
21
    const ALIAS_STATEMENT_TYPE = 'alias';
22
23
    /**
24
     * @var string
25
     */
26
    private $usesBlock;
27
28
    /**
29
     * @var boolean
30
     */
31
    private $isUseStatementBuilding = false;
32
33
    /**
34
     * @var string
35
     */
36
    private $currentUseStatement = '';
37
38
    /**
39
     * @var string
40
     */
41
    private $committedUseStatement = '';
42
43
    /**
44
     * @var string
45
     */
46
    private $currentAliasStatement = '';
47
48
    /**
49
     * @var string
50
     */
51
    private $currentStatementType = '';
52
53
    /**
54
     * @var bool
55
     */
56
    private $isBrace = false;
57
58
    /**
59
     * UsesBlockParser constructor.
60
     * @param string $usesBlock
61
     */
62 1
    public function __construct(string $usesBlock) {
63 1
        $this->setUsesBlock($usesBlock);
64 1
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getUsesBlock(): string {
70 1
        return $this->usesBlock;
71
    }
72
73
    /**
74
     * @param string $usesBlock
75
     * @return UsesBlockParser
76
     */
77 1
    public function setUsesBlock(string $usesBlock): UsesBlockParser {
78 1
        $this->usesBlock = $usesBlock;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * @return boolean
85
     */
86 1
    protected function isUseStatementBuilding(): bool {
87 1
        return $this->isUseStatementBuilding;
88
    }
89
90
    /**
91
     * @return UsesBlockParser
92
     */
93 1
    protected function setUseStatementIsBuilding(): UsesBlockParser {
94 1
        $this->isUseStatementBuilding = true;
95
        
96 1
        return $this;
97
    }
98
99
    /**
100
     * @return UsesBlockParser
101
     */
102 1
    protected function setUseStatementIsNotBuilding(): UsesBlockParser {
103 1
        $this->isUseStatementBuilding = false;
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 1
    protected function getCurrentUseStatement(): string {
112 1
        if ($this->isBrace()) {
113 1
            return $this->getCommittedUseStatement() . $this->currentUseStatement;
114
        }
115
116 1
        return $this->currentUseStatement;
117
    }
118
119
    /**
120
     * @param string $statement
121
     * @return UsesBlockParser
122
     */
123 1
    protected function setCurrentUseStatement(string $statement): UsesBlockParser {
124 1
        $this->currentUseStatement .= $statement;
125
        
126 1
        return $this;
127
    }
128
129
    /**
130
     * @return UsesBlockParser
131
     */
132 1
    protected function clearCurrentUseStatement(): UsesBlockParser {
133 1
        $this->currentUseStatement = '';
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * @return string
140
     */
141 1
    protected function getCommittedUseStatement(): string {
142 1
        return $this->committedUseStatement;
143
    }
144
145
    /**
146
     * @return UsesBlockParser
147
     */
148 1
    protected function commitUseStatement(): UsesBlockParser {
149 1
        $this->committedUseStatement = $this->currentUseStatement;
150
151 1
        return $this;
152
    }
153
154
    /**
155
     * @return string
156
     */
157 1
    protected function getCurrentAliasStatement(): string {
158 1
        return $this->currentAliasStatement;
159
    }
160
161
    /**
162
     * @param string $statement
163
     * @return UsesBlockParser
164
     */
165 1
    protected function setCurrentAliasStatement(string $statement): UsesBlockParser {
166 1
        $this->currentAliasStatement = $statement;
167
        
168 1
        return $this;
169
    }
170
171
    /**
172
     * @return string
173
     */
174 1
    protected function getCurrentStatementType(): string {
175 1
        return $this->currentStatementType;
176
    }
177
178
    /**
179
     * @param string $currentStatementType
180
     * @return UsesBlockParser
181
     */
182 1
    protected function setCurrentStatementType(string $currentStatementType): UsesBlockParser {
183 1
        $this->currentStatementType = $currentStatementType;
184
        
185 1
        return $this;
186
    }
187
188
    /**
189
     * @return UsesBlockParser
190
     */
191 1
    protected function setIsBrace(): UsesBlockParser {
192 1
        $this->isBrace = true;
193
194 1
        return $this->commitUseStatement()
195 1
            ->clearCurrentUseStatement();
196
    }
197
198
    /**
199
     * @return UsesBlockParser
200
     */
201 1
    protected function setIsNotBrace(): UsesBlockParser {
202 1
        $this->isBrace = false;
203 1
        $this->committedUseStatement = '';
204
205 1
        return $this;
206
    }
207
208
    /**
209
     * @return bool
210
     */
211 1
    protected function isBrace(): bool {
212 1
        return $this->isBrace;
213
    }
214
    
215
    /**
216
     * @return UseStatements
217
     */
218 1
    public function getUseStatements(): UseStatements {
219 1
        $useStatements = new UseStatements();
220
        
221 1
        foreach ($this->getTokens() as $token) {
222 1
            if (is_array($token)) {
223 1
                if ($token[0] === T_USE) {
224 1
                    $this->setUseStatementIsBuilding()
225 1
                        ->setCurrentStatementType(self::USE_STATEMENT_TYPE);
226
227 1
                    continue;
228
                }
229
230 1
                if ($token[0] === T_AS) {
231 1
                    $this->setCurrentStatementType(self::ALIAS_STATEMENT_TYPE);
232
233 1
                    continue;
234
                }
235
236 1
                if ($this->isUseStatementBuilding() && $this->getCurrentStatementType()) {
237 1
                    switch ($token[0]) {
238 1
                        case T_NS_SEPARATOR:
239 1
                        case T_STRING:
240 1
                            $this->setCurrentStatement($token[1]);
241
242 1
                            break;
243
                    }
244
                }
245
            } else {
246 1
                if (($token === ';' || $token === ',') && $this->isUseStatementBuilding()) {
247 1
                    $useStatements->add(new UseStatement(
248 1
                        $this->getCurrentUseStatement(),
249 1
                        $this->getCurrentAliasStatement()
250
                    ));
251
252 1
                    $this->clearCurrentStatements();
253
254 1
                    if ($token === ';') {
255 1
                        $this->setUseStatementIsNotBuilding();
256
                    }
257
258 1
                    if ($token === ',') {
259 1
                        $this->setCurrentStatementType(self::USE_STATEMENT_TYPE);
260
                    }
261
                }
262
263 1
                if ($token === '{') {
264 1
                    $this->setIsBrace();
265
                }
266
267 1
                if ($token === ';') {
268 1
                    $this->setIsNotBrace();
269
                }
270
            }
271
        }
272
273 1
        return $useStatements;
274
    }
275
    
276
    /**
277
     * @return array
278
     */
279 1
    private function getTokens(): array {
280 1
        return token_get_all($this->getUsesBlock());
281
    }
282
283
    /**
284
     * @param string $value
285
     * @return UsesBlockParser
286
     */
287 1
    private function setCurrentStatement(string $value): UsesBlockParser {
288 1
        $type = $this->getCurrentStatementType();
289
290 1
        $setter = 'setCurrent' . ucfirst($type) . 'Statement';
291
292 1
        return $this->$setter($value);
293
    }
294
295
    /**
296
     * @return UsesBlockParser
297
     */
298 1
    protected function clearCurrentStatements(): UsesBlockParser {
299 1
        return $this->clearCurrentUseStatement()
300 1
            ->setCurrentAliasStatement('');
301
    }
302
    
303
}