Skip to content

Commit aad0e28

Browse files
committed
Remove token registration from TokenEmulator interface
1 parent 624f71f commit aad0e28

File tree

4 files changed

+9
-37
lines changed

4 files changed

+9
-37
lines changed

lib/PhpParser/Lexer/Emulative.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
99
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
1010
use PhpParser\Lexer\TokenEmulator\TokenEmulatorInterface;
11+
use PhpParser\Parser\Tokens;
1112

1213
class Emulative extends Lexer
1314
{
1415
const PHP_7_3 = '7.3.0dev';
1516
const PHP_7_4 = '7.4.0dev';
1617

18+
const T_COALESCE_EQUAL = 1007;
19+
const T_FN = 1008;
20+
1721
const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
1822
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
1923
(?:.*\r?\n)*?
@@ -33,14 +37,11 @@ public function __construct(array $options = [])
3337
{
3438
parent::__construct($options);
3539

36-
// prepare token emulators
3740
$this->tokenEmulators[] = new FnTokenEmulator();
3841
$this->tokenEmulators[] = new CoaleseEqualTokenEmulator();
3942

40-
// add emulated tokens here
41-
foreach ($this->tokenEmulators as $emulativeToken) {
42-
$this->tokenMap[$emulativeToken->getTokenId()] = $emulativeToken->getParserTokenId();
43-
}
43+
$this->tokenMap[self::T_COALESCE_EQUAL] = Tokens::T_COALESCE_EQUAL;
44+
$this->tokenMap[self::T_FN] = Tokens::T_FN;
4445
}
4546

4647
public function startLexing(string $code, ErrorHandler $errorHandler = null) {

lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,9 @@
33
namespace PhpParser\Lexer\TokenEmulator;
44

55
use PhpParser\Lexer\Emulative;
6-
use PhpParser\Parser\Tokens;
76

87
final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface
98
{
10-
const T_COALESCE_EQUAL = 1007;
11-
12-
public function getTokenId(): int
13-
{
14-
return self::T_COALESCE_EQUAL;
15-
}
16-
17-
public function getParserTokenId(): int
18-
{
19-
return Tokens::T_COALESCE_EQUAL;
20-
}
21-
229
public function isEmulationNeeded(string $code) : bool
2310
{
2411
// skip version where this is supported
@@ -38,7 +25,7 @@ public function emulate(string $code, array $tokens): array
3825
if (isset($tokens[$i + 1])) {
3926
if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') {
4027
array_splice($tokens, $i, 2, [
41-
[self::T_COALESCE_EQUAL, '??=', $line]
28+
[Emulative::T_COALESCE_EQUAL, '??=', $line]
4229
]);
4330
$c--;
4431
continue;

lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,9 @@
33
namespace PhpParser\Lexer\TokenEmulator;
44

55
use PhpParser\Lexer\Emulative;
6-
use PhpParser\Parser\Tokens;
76

87
final class FnTokenEmulator implements TokenEmulatorInterface
98
{
10-
const T_FN = 1008;
11-
12-
public function getTokenId(): int
13-
{
14-
return self::T_FN;
15-
}
16-
17-
public function getParserTokenId(): int
18-
{
19-
return Tokens::T_FN;
20-
}
21-
229
public function isEmulationNeeded(string $code) : bool
2310
{
2411
// skip version where this is supported
@@ -40,7 +27,7 @@ public function emulate(string $code, array $tokens): array
4027
continue;
4128
}
4229

43-
$tokens[$i][0] = self::T_FN;
30+
$tokens[$i][0] = Emulative::T_FN;
4431
}
4532
}
4633

lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
namespace PhpParser\Lexer\TokenEmulator;
44

5+
/** @internal */
56
interface TokenEmulatorInterface
67
{
7-
public function getTokenId(): int;
8-
9-
public function getParserTokenId(): int;
10-
118
public function isEmulationNeeded(string $code): bool;
129

1310
/**

0 commit comments

Comments
 (0)