Skip to content

Commit c268e88

Browse files
authored
Merge pull request #64 from greg0ire/token-type
2 parents 90d8024 + ad0c04d commit c268e88

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

lib/Doctrine/Common/Lexer/AbstractLexer.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* Base class for writing simple lexers, i.e. for creating small DSLs.
21+
*
22+
* @psalm-type Token = array{value: int|string, type:string|int|null, position:int}
2123
*/
2224
abstract class AbstractLexer
2325
{
@@ -38,7 +40,7 @@ abstract class AbstractLexer
3840
* - 'position' : the position of the token in the input string
3941
*
4042
* @var mixed[][]
41-
* @psalm-var list<array{value: string, type: string|int|null, position: int}>
43+
* @psalm-var list<Token>
4244
*/
4345
private $tokens = [];
4446

@@ -60,15 +62,15 @@ abstract class AbstractLexer
6062
* The next token in the input.
6163
*
6264
* @var mixed[]|null
63-
* @psalm-var array{value: string, type: string|int|null, position: int}|null
65+
* @psalm-var Token|null
6466
*/
6567
public $lookahead;
6668

6769
/**
6870
* The last matched/seen token.
6971
*
7072
* @var mixed[]|null
71-
* @psalm-var array{value: string, type: string|int|null, position: int}|null
73+
* @psalm-var Token|null
7274
*/
7375
public $token;
7476

@@ -148,25 +150,25 @@ public function getInputUntilPosition($position)
148150
/**
149151
* Checks whether a given token matches the current lookahead.
150152
*
151-
* @param int|string $token
153+
* @param int|string $type
152154
*
153155
* @return bool
154156
*/
155-
public function isNextToken($token)
157+
public function isNextToken($type)
156158
{
157-
return $this->lookahead !== null && $this->lookahead['type'] === $token;
159+
return $this->lookahead !== null && $this->lookahead['type'] === $type;
158160
}
159161

160162
/**
161163
* Checks whether any of the given tokens matches the current lookahead.
162164
*
163-
* @param string[] $tokens
165+
* @param list<int|string> $types
164166
*
165167
* @return bool
166168
*/
167-
public function isNextTokenAny(array $tokens)
169+
public function isNextTokenAny(array $types)
168170
{
169-
return $this->lookahead !== null && in_array($this->lookahead['type'], $tokens, true);
171+
return $this->lookahead !== null && in_array($this->lookahead['type'], $types, true);
170172
}
171173

172174
/**
@@ -215,7 +217,7 @@ public function isA($value, $token)
215217
* Moves the lookahead token forward.
216218
*
217219
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
218-
* @psalm-return array{value: string, type: string|int|null, position: int}|null
220+
* @psalm-return Token|null
219221
*/
220222
public function peek()
221223
{
@@ -230,7 +232,7 @@ public function peek()
230232
* Peeks at the next token, returns it and immediately resets the peek.
231233
*
232234
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
233-
* @psalm-return array{value: string, type: string|int|null, position: int}|null
235+
* @psalm-return Token|null
234236
*/
235237
public function glimpse()
236238
{

tests/Doctrine/Common/Lexer/AbstractLexerTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use const LC_ALL;
1414

15+
/**
16+
* @psalm-type ValidToken = array{value: string|int, type: string|int, position: int}
17+
*/
1518
class AbstractLexerTest extends TestCase
1619
{
1720
/** @var ConcreteLexer */
@@ -28,7 +31,7 @@ public function tearDown(): void
2831
}
2932

3033
/**
31-
* @psalm-return list<array{string, list<array{value: string|int, type: string, position: int}>}>
34+
* @psalm-return list<array{string, list<ValidToken>}>
3235
*/
3336
public function dataProvider(): array
3437
{
@@ -120,7 +123,7 @@ public function testResetPosition(): void
120123
}
121124

122125
/**
123-
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
126+
* @psalm-param list<ValidToken> $expectedTokens
124127
*
125128
* @dataProvider dataProvider
126129
*/
@@ -172,7 +175,7 @@ public function testUtf8Mismatch(): void
172175
}
173176

174177
/**
175-
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
178+
* @psalm-param list<ValidToken> $expectedTokens
176179
*
177180
* @dataProvider dataProvider
178181
*/
@@ -187,7 +190,7 @@ public function testPeek(string $input, array $expectedTokens): void
187190
}
188191

189192
/**
190-
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
193+
* @psalm-param list<ValidToken> $expectedTokens
191194
*
192195
* @dataProvider dataProvider
193196
*/
@@ -227,7 +230,7 @@ public function testGetInputUntilPosition(
227230
}
228231

229232
/**
230-
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
233+
* @psalm-param list<ValidToken> $expectedTokens
231234
*
232235
* @dataProvider dataProvider
233236
*/
@@ -243,7 +246,7 @@ public function testIsNextToken(string $input, array $expectedTokens): void
243246
}
244247

245248
/**
246-
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
249+
* @psalm-param list<ValidToken> $expectedTokens
247250
*
248251
* @dataProvider dataProvider
249252
*/

0 commit comments

Comments
 (0)