Skip to content

Commit e29955f

Browse files
authored
Merge pull request #465 from greg0ire/drop-lexer-1
Drop Lexer 1
2 parents ad78521 + 54d06be commit e29955f

File tree

5 files changed

+45
-50
lines changed

5 files changed

+45
-50
lines changed

UPGRADE.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Upgrade from 1.0.x to 2.0.x
2+
3+
`DocLexer::peek()` and `DocLexer::glimpse` now return
4+
`Doctrine\Common\Lexer\Token` objects. When using `doctrine/lexer` 2, these
5+
implement `ArrayAccess` as a way for you to still be able to treat them as
6+
arrays in some ways.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require": {
3535
"php": "^7.1 || ^8.0",
3636
"ext-tokenizer": "*",
37-
"doctrine/lexer": "^1 || ^2",
37+
"doctrine/lexer": "^2",
3838
"psr/cache": "^1 || ^2 || ^3"
3939
},
4040
"require-dev": {

lib/Doctrine/Common/Annotations/DocLexer.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function nextTokenIsAdjacent(): bool
7070
{
7171
return $this->token === null
7272
|| ($this->lookahead !== null
73-
&& ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
73+
&& ($this->lookahead->position - $this->token->position) === strlen($this->token->value));
7474
}
7575

7676
/**
@@ -128,16 +128,4 @@ protected function getType(&$value)
128128

129129
return $type;
130130
}
131-
132-
/** @return array{value: int|string, type:self::T_*|null, position:int} */
133-
public function peek(): ?array
134-
{
135-
$token = parent::peek();
136-
137-
if ($token === null) {
138-
return null;
139-
}
140-
141-
return (array) $token;
142-
}
143131
}

lib/Doctrine/Common/Annotations/DocParser.php

+20-20
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private function syntaxError(string $expected, ?array $token = null): Annotation
454454
$message = sprintf('Expected %s, got ', $expected);
455455
$message .= $this->lexer->lookahead === null
456456
? 'end of string'
457-
: sprintf("'%s' at position %s", $token['value'], $token['position']);
457+
: sprintf("'%s' at position %s", $token->value, $token->position);
458458

459459
if (strlen($this->context)) {
460460
$message .= ' in ' . $this->context;
@@ -684,16 +684,16 @@ private function Annotations(): array
684684
$annotations = [];
685685

686686
while ($this->lexer->lookahead !== null) {
687-
if ($this->lexer->lookahead['type'] !== DocLexer::T_AT) {
687+
if ($this->lexer->lookahead->type !== DocLexer::T_AT) {
688688
$this->lexer->moveNext();
689689
continue;
690690
}
691691

692692
// make sure the @ is preceded by non-catchable pattern
693693
if (
694694
$this->lexer->token !== null &&
695-
$this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen(
696-
$this->lexer->token['value']
695+
$this->lexer->lookahead->position === $this->lexer->token->position + strlen(
696+
$this->lexer->token->value
697697
)
698698
) {
699699
$this->lexer->moveNext();
@@ -705,12 +705,12 @@ private function Annotations(): array
705705
$peek = $this->lexer->glimpse();
706706
if (
707707
($peek === null)
708-
|| ($peek['type'] !== DocLexer::T_NAMESPACE_SEPARATOR && ! in_array(
709-
$peek['type'],
708+
|| ($peek->type !== DocLexer::T_NAMESPACE_SEPARATOR && ! in_array(
709+
$peek->type,
710710
self::$classIdentifiers,
711711
true
712712
))
713-
|| $peek['position'] !== $this->lexer->lookahead['position'] + 1
713+
|| $peek->position !== $this->lexer->lookahead->position + 1
714714
) {
715715
$this->lexer->moveNext();
716716
continue;
@@ -1186,18 +1186,18 @@ private function Identifier(): string
11861186

11871187
$this->lexer->moveNext();
11881188

1189-
$className = $this->lexer->token['value'];
1189+
$className = $this->lexer->token->value;
11901190

11911191
while (
11921192
$this->lexer->lookahead !== null &&
1193-
$this->lexer->lookahead['position'] === ($this->lexer->token['position'] +
1194-
strlen($this->lexer->token['value'])) &&
1193+
$this->lexer->lookahead->position === ($this->lexer->token->position +
1194+
strlen($this->lexer->token->value)) &&
11951195
$this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)
11961196
) {
11971197
$this->match(DocLexer::T_NAMESPACE_SEPARATOR);
11981198
$this->matchAny(self::$classIdentifiers);
11991199

1200-
$className .= '\\' . $this->lexer->token['value'];
1200+
$className .= '\\' . $this->lexer->token->value;
12011201
}
12021202

12031203
return $className;
@@ -1215,7 +1215,7 @@ private function Value()
12151215
{
12161216
$peek = $this->lexer->glimpse();
12171217

1218-
if ($peek['type'] === DocLexer::T_EQUALS) {
1218+
if ($peek->type === DocLexer::T_EQUALS) {
12191219
return $this->FieldAssignment();
12201220
}
12211221

@@ -1244,21 +1244,21 @@ private function PlainValue()
12441244
return $this->Constant();
12451245
}
12461246

1247-
switch ($this->lexer->lookahead['type']) {
1247+
switch ($this->lexer->lookahead->type) {
12481248
case DocLexer::T_STRING:
12491249
$this->match(DocLexer::T_STRING);
12501250

1251-
return $this->lexer->token['value'];
1251+
return $this->lexer->token->value;
12521252

12531253
case DocLexer::T_INTEGER:
12541254
$this->match(DocLexer::T_INTEGER);
12551255

1256-
return (int) $this->lexer->token['value'];
1256+
return (int) $this->lexer->token->value;
12571257

12581258
case DocLexer::T_FLOAT:
12591259
$this->match(DocLexer::T_FLOAT);
12601260

1261-
return (float) $this->lexer->token['value'];
1261+
return (float) $this->lexer->token->value;
12621262

12631263
case DocLexer::T_TRUE:
12641264
$this->match(DocLexer::T_TRUE);
@@ -1290,7 +1290,7 @@ private function PlainValue()
12901290
private function FieldAssignment(): stdClass
12911291
{
12921292
$this->match(DocLexer::T_IDENTIFIER);
1293-
$fieldName = $this->lexer->token['value'];
1293+
$fieldName = $this->lexer->token->value;
12941294

12951295
$this->match(DocLexer::T_EQUALS);
12961296

@@ -1365,14 +1365,14 @@ private function ArrayEntry(): array
13651365
$peek = $this->lexer->glimpse();
13661366

13671367
if (
1368-
$peek['type'] === DocLexer::T_EQUALS
1369-
|| $peek['type'] === DocLexer::T_COLON
1368+
$peek->type === DocLexer::T_EQUALS
1369+
|| $peek->type === DocLexer::T_COLON
13701370
) {
13711371
if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) {
13721372
$key = $this->Constant();
13731373
} else {
13741374
$this->matchAny([DocLexer::T_INTEGER, DocLexer::T_STRING]);
1375-
$key = $this->lexer->token['value'];
1375+
$key = $this->lexer->token->value;
13761376
}
13771377

13781378
$this->matchAny([DocLexer::T_EQUALS, DocLexer::T_COLON]);

tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php

+17-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Tests\Common\Annotations;
44

55
use Doctrine\Common\Annotations\DocLexer;
6+
use Doctrine\Common\Lexer\Token;
67
use PHPUnit\Framework\TestCase;
78

89
use function str_repeat;
@@ -20,12 +21,12 @@ public function testMarkerAnnotation(): void
2021
self::assertTrue($lexer->moveNext());
2122
self::assertNull($lexer->token);
2223
self::assertNotNull($lexer->lookahead);
23-
self::assertEquals('@', $lexer->lookahead['value']);
24+
self::assertEquals('@', $lexer->lookahead->value);
2425

2526
self::assertTrue($lexer->moveNext());
2627
self::assertNotNull($lexer->token);
27-
self::assertEquals('@', $lexer->token['value']);
28-
self::assertEquals('Name', $lexer->lookahead['value']);
28+
self::assertEquals('@', $lexer->token->value);
29+
self::assertEquals('Name', $lexer->lookahead->value);
2930

3031
self::assertFalse($lexer->moveNext());
3132
}
@@ -114,9 +115,9 @@ public function testScannerTokenizesDocBlockWhitConstants(): void
114115
foreach ($tokens as $expected) {
115116
$lexer->moveNext();
116117
$lookahead = $lexer->lookahead;
117-
self::assertEquals($expected['value'], $lookahead['value']);
118-
self::assertEquals($expected['type'], $lookahead['type']);
119-
self::assertEquals($expected['position'], $lookahead['position']);
118+
self::assertEquals($expected['value'], $lookahead->value);
119+
self::assertEquals($expected['type'], $lookahead->type);
120+
self::assertEquals($expected['position'], $lookahead->position);
120121
}
121122

122123
self::assertFalse($lexer->moveNext());
@@ -155,9 +156,9 @@ public function testScannerTokenizesDocBlockWhitInvalidIdentifier(): void
155156
foreach ($tokens as $expected) {
156157
$lexer->moveNext();
157158
$lookahead = $lexer->lookahead;
158-
self::assertEquals($expected['value'], $lookahead['value']);
159-
self::assertEquals($expected['type'], $lookahead['type']);
160-
self::assertEquals($expected['position'], $lookahead['position']);
159+
self::assertEquals($expected['value'], $lookahead->value);
160+
self::assertEquals($expected['type'], $lookahead->type);
161+
self::assertEquals($expected['position'], $lookahead->position);
161162
}
162163

163164
self::assertFalse($lexer->moveNext());
@@ -172,7 +173,7 @@ public function testWithinDoubleQuotesVeryVeryLongStringWillNotOverflowPregSplit
172173

173174
$lexer->setInput('"' . str_repeat('.', 20240) . '"');
174175

175-
self::assertIsArray($lexer->glimpse());
176+
self::assertInstanceOf(Token::class, $lexer->glimpse());
176177
}
177178

178179
/**
@@ -216,9 +217,9 @@ public function testRecognizesDoubleQuotesEscapeSequence(): void
216217
foreach ($tokens as $expected) {
217218
$lexer->moveNext();
218219
$lookahead = $lexer->lookahead;
219-
self::assertEquals($expected['value'], $lookahead['value']);
220-
self::assertEquals($expected['type'], $lookahead['type']);
221-
self::assertEquals($expected['position'], $lookahead['position']);
220+
self::assertEquals($expected['value'], $lookahead->value);
221+
self::assertEquals($expected['type'], $lookahead->type);
222+
self::assertEquals($expected['position'], $lookahead->position);
222223
}
223224

224225
self::assertFalse($lexer->moveNext());
@@ -296,9 +297,9 @@ private function expectDocblockTokens(string $docBlock, array $expectedTokens):
296297
$lookahead = $lexer->lookahead;
297298

298299
$actualTokens[] = [
299-
'value' => $lookahead['value'],
300-
'type' => $lookahead['type'],
301-
'position' => $lookahead['position'],
300+
'value' => $lookahead->value,
301+
'type' => $lookahead->type,
302+
'position' => $lookahead->position,
302303
];
303304
}
304305

0 commit comments

Comments
 (0)