|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright 2018 Aleksander Stelmaczonek <[email protected]> |
|
4
|
|
|
* @license MIT License, see license file distributed with this source code |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Koriit\PHPDeps\Tokenizer; |
|
8
|
|
|
|
|
9
|
|
|
use ArrayIterator; |
|
10
|
|
|
use Koriit\PHPDeps\Tokenizer\Exceptions\UnexpectedEndOfTokens; |
|
11
|
|
|
use Koriit\PHPDeps\Tokenizer\Exceptions\WrongPosition; |
|
12
|
|
|
|
|
13
|
|
|
class TokensIterator extends ArrayIterator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param mixed $token |
|
17
|
|
|
* @param int $tokenType |
|
18
|
|
|
* |
|
19
|
|
|
* @return bool |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function isToken($token, $tokenType) |
|
22
|
|
|
{ |
|
23
|
|
|
return \is_array($token) && $token[0] === $tokenType; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param mixed $token |
|
28
|
|
|
* @param int[] $tokenTypes |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function isOneOfTokens($token, array $tokenTypes) |
|
33
|
|
|
{ |
|
34
|
|
|
return \is_array($token) && \in_array($token[0], $tokenTypes, true); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $contents Contents of PHP file to tokenize |
|
39
|
|
|
* |
|
40
|
|
|
* @return TokensIterator |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function fromContents($contents) |
|
43
|
|
|
{ |
|
44
|
|
|
return new static(\token_get_all($contents)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $filePath Path to PHP file to tokenize |
|
49
|
|
|
* |
|
50
|
|
|
* @return TokensIterator |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function fromFile($filePath) |
|
53
|
|
|
{ |
|
54
|
|
|
return static::fromContents(\file_get_contents($filePath)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @throws UnexpectedEndOfTokens |
|
59
|
|
|
*/ |
|
60
|
|
|
public function skipNextBlock() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->findNextBlock(); |
|
63
|
|
|
$this->skipBlock(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws UnexpectedEndOfTokens |
|
68
|
|
|
*/ |
|
69
|
|
|
public function findNextBlock() |
|
70
|
|
|
{ |
|
71
|
|
|
while ($this->valid()) { |
|
72
|
|
|
if ($this->current() === '{') { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->next(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
throw new UnexpectedEndOfTokens(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws UnexpectedEndOfTokens |
|
84
|
|
|
*/ |
|
85
|
|
|
public function skipBlock() |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->valid() || $this->current() !== '{') { |
|
88
|
|
|
throw new WrongPosition('Not at block beginning position.'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$bracesCounter = 1; |
|
92
|
|
|
$this->next(); |
|
93
|
|
|
while ($this->valid() && $bracesCounter > 0) { |
|
94
|
|
|
if ($this->current() === '{') { |
|
95
|
|
|
$bracesCounter++; |
|
96
|
|
|
} elseif ($this->current() === '}') { |
|
97
|
|
|
$bracesCounter--; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->next(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($bracesCounter != 0) { |
|
104
|
|
|
throw new UnexpectedEndOfTokens(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function skipWhitespaces() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->skipTokensIfPresent([T_WHITESPACE]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function skipTokensIfPresent(array $tokenTypes) |
|
114
|
|
|
{ |
|
115
|
|
|
while ($this->valid() && $this->currentIsOneOf($tokenTypes)) { |
|
116
|
|
|
$this->next(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param int $tokenType |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function currentIs($tokenType) |
|
126
|
|
|
{ |
|
127
|
|
|
return static::isToken($this->current(), $tokenType); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param int[] $tokenTypes |
|
132
|
|
|
* |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
|
|
public function currentIsOneOf(array $tokenTypes) |
|
136
|
|
|
{ |
|
137
|
|
|
return static::isOneOfTokens($this->current(), $tokenTypes); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|