@@ -12,20 +12,20 @@ |
||
12 | 12 | * PHP 8.1 ReadonlyTokenEmulator. |
13 | 13 | */ |
14 | 14 | class ReadonlyFunctionTokenEmulator extends KeywordEmulator { |
15 | - public function getKeywordString(): string { |
|
16 | - return 'readonly'; |
|
17 | - } |
|
15 | + public function getKeywordString(): string { |
|
16 | + return 'readonly'; |
|
17 | + } |
|
18 | 18 | |
19 | - public function getKeywordToken(): int { |
|
20 | - return \T_READONLY; |
|
21 | - } |
|
19 | + public function getKeywordToken(): int { |
|
20 | + return \T_READONLY; |
|
21 | + } |
|
22 | 22 | |
23 | - public function getPhpVersion(): string { |
|
24 | - return Emulative::PHP_8_2; |
|
25 | - } |
|
23 | + public function getPhpVersion(): string { |
|
24 | + return Emulative::PHP_8_2; |
|
25 | + } |
|
26 | 26 | |
27 | - public function reverseEmulate(string $code, array $tokens): array { |
|
28 | - // Don't bother |
|
29 | - return $tokens; |
|
30 | - } |
|
27 | + public function reverseEmulate(string $code, array $tokens): array { |
|
28 | + // Don't bother |
|
29 | + return $tokens; |
|
30 | + } |
|
31 | 31 | } |
@@ -5,40 +5,40 @@ |
||
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | 7 | class ExplicitOctalEmulator extends TokenEmulator { |
8 | - public function getPhpVersion(): string { |
|
9 | - return Emulative::PHP_8_1; |
|
10 | - } |
|
8 | + public function getPhpVersion(): string { |
|
9 | + return Emulative::PHP_8_1; |
|
10 | + } |
|
11 | 11 | |
12 | - public function isEmulationNeeded(string $code): bool { |
|
13 | - return strpos($code, '0o') !== false || strpos($code, '0O') !== false; |
|
14 | - } |
|
12 | + public function isEmulationNeeded(string $code): bool { |
|
13 | + return strpos($code, '0o') !== false || strpos($code, '0O') !== false; |
|
14 | + } |
|
15 | 15 | |
16 | - public function emulate(string $code, array $tokens): array { |
|
17 | - for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
18 | - if ($tokens[$i][0] == \T_LNUMBER && $tokens[$i][1] === '0' && |
|
19 | - isset($tokens[$i + 1]) && $tokens[$i + 1][0] == \T_STRING && |
|
20 | - preg_match('/[oO][0-7]+(?:_[0-7]+)*/', $tokens[$i + 1][1]) |
|
21 | - ) { |
|
22 | - $tokenKind = $this->resolveIntegerOrFloatToken($tokens[$i + 1][1]); |
|
23 | - array_splice($tokens, $i, 2, [ |
|
24 | - [$tokenKind, '0' . $tokens[$i + 1][1], $tokens[$i][2]], |
|
25 | - ]); |
|
26 | - $c--; |
|
27 | - } |
|
28 | - } |
|
29 | - return $tokens; |
|
30 | - } |
|
16 | + public function emulate(string $code, array $tokens): array { |
|
17 | + for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
18 | + if ($tokens[$i][0] == \T_LNUMBER && $tokens[$i][1] === '0' && |
|
19 | + isset($tokens[$i + 1]) && $tokens[$i + 1][0] == \T_STRING && |
|
20 | + preg_match('/[oO][0-7]+(?:_[0-7]+)*/', $tokens[$i + 1][1]) |
|
21 | + ) { |
|
22 | + $tokenKind = $this->resolveIntegerOrFloatToken($tokens[$i + 1][1]); |
|
23 | + array_splice($tokens, $i, 2, [ |
|
24 | + [$tokenKind, '0' . $tokens[$i + 1][1], $tokens[$i][2]], |
|
25 | + ]); |
|
26 | + $c--; |
|
27 | + } |
|
28 | + } |
|
29 | + return $tokens; |
|
30 | + } |
|
31 | 31 | |
32 | - private function resolveIntegerOrFloatToken(string $str): int |
|
33 | - { |
|
34 | - $str = substr($str, 1); |
|
35 | - $str = str_replace('_', '', $str); |
|
36 | - $num = octdec($str); |
|
37 | - return is_float($num) ? \T_DNUMBER : \T_LNUMBER; |
|
38 | - } |
|
32 | + private function resolveIntegerOrFloatToken(string $str): int |
|
33 | + { |
|
34 | + $str = substr($str, 1); |
|
35 | + $str = str_replace('_', '', $str); |
|
36 | + $num = octdec($str); |
|
37 | + return is_float($num) ? \T_DNUMBER : \T_LNUMBER; |
|
38 | + } |
|
39 | 39 | |
40 | - public function reverseEmulate(string $code, array $tokens): array { |
|
41 | - // Explicit octals were not legal code previously, don't bother. |
|
42 | - return $tokens; |
|
43 | - } |
|
40 | + public function reverseEmulate(string $code, array $tokens): array { |
|
41 | + // Explicit octals were not legal code previously, don't bother. |
|
42 | + return $tokens; |
|
43 | + } |
|
44 | 44 | } |
45 | 45 | \ No newline at end of file |
@@ -21,7 +21,7 @@ |
||
21 | 21 | ) { |
22 | 22 | $tokenKind = $this->resolveIntegerOrFloatToken($tokens[$i + 1][1]); |
23 | 23 | array_splice($tokens, $i, 2, [ |
24 | - [$tokenKind, '0' . $tokens[$i + 1][1], $tokens[$i][2]], |
|
24 | + [$tokenKind, '0'.$tokens[$i + 1][1], $tokens[$i][2]], |
|
25 | 25 | ]); |
26 | 26 | $c--; |
27 | 27 | } |
@@ -7,30 +7,30 @@ |
||
7 | 7 | */ |
8 | 8 | final class ReverseEmulator extends TokenEmulator |
9 | 9 | { |
10 | - /** @var TokenEmulator Inner emulator */ |
|
11 | - private $emulator; |
|
10 | + /** @var TokenEmulator Inner emulator */ |
|
11 | + private $emulator; |
|
12 | 12 | |
13 | - public function __construct(TokenEmulator $emulator) { |
|
14 | - $this->emulator = $emulator; |
|
15 | - } |
|
13 | + public function __construct(TokenEmulator $emulator) { |
|
14 | + $this->emulator = $emulator; |
|
15 | + } |
|
16 | 16 | |
17 | - public function getPhpVersion(): string { |
|
18 | - return $this->emulator->getPhpVersion(); |
|
19 | - } |
|
17 | + public function getPhpVersion(): string { |
|
18 | + return $this->emulator->getPhpVersion(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function isEmulationNeeded(string $code): bool { |
|
22 | - return $this->emulator->isEmulationNeeded($code); |
|
23 | - } |
|
21 | + public function isEmulationNeeded(string $code): bool { |
|
22 | + return $this->emulator->isEmulationNeeded($code); |
|
23 | + } |
|
24 | 24 | |
25 | - public function emulate(string $code, array $tokens): array { |
|
26 | - return $this->emulator->reverseEmulate($code, $tokens); |
|
27 | - } |
|
25 | + public function emulate(string $code, array $tokens): array { |
|
26 | + return $this->emulator->reverseEmulate($code, $tokens); |
|
27 | + } |
|
28 | 28 | |
29 | - public function reverseEmulate(string $code, array $tokens): array { |
|
30 | - return $this->emulator->emulate($code, $tokens); |
|
31 | - } |
|
29 | + public function reverseEmulate(string $code, array $tokens): array { |
|
30 | + return $this->emulator->emulate($code, $tokens); |
|
31 | + } |
|
32 | 32 | |
33 | - public function preprocessCode(string $code, array &$patches): string { |
|
34 | - return $code; |
|
35 | - } |
|
33 | + public function preprocessCode(string $code, array &$patches): string { |
|
34 | + return $code; |
|
35 | + } |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |
@@ -5,8 +5,7 @@ |
||
5 | 5 | /** |
6 | 6 | * Reverses emulation direction of the inner emulator. |
7 | 7 | */ |
8 | -final class ReverseEmulator extends TokenEmulator |
|
9 | -{ |
|
8 | +final class ReverseEmulator extends TokenEmulator { |
|
10 | 9 | /** @var TokenEmulator Inner emulator */ |
11 | 10 | private $emulator; |
12 | 11 |
@@ -6,18 +6,18 @@ |
||
6 | 6 | |
7 | 7 | final class MatchTokenEmulator extends KeywordEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_8_0; |
|
12 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_8_0; |
|
12 | + } |
|
13 | 13 | |
14 | - public function getKeywordString(): string |
|
15 | - { |
|
16 | - return 'match'; |
|
17 | - } |
|
14 | + public function getKeywordString(): string |
|
15 | + { |
|
16 | + return 'match'; |
|
17 | + } |
|
18 | 18 | |
19 | - public function getKeywordToken(): int |
|
20 | - { |
|
21 | - return \T_MATCH; |
|
22 | - } |
|
19 | + public function getKeywordToken(): int |
|
20 | + { |
|
21 | + return \T_MATCH; |
|
22 | + } |
|
23 | 23 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class MatchTokenEmulator extends KeywordEmulator |
|
8 | -{ |
|
7 | +final class MatchTokenEmulator extends KeywordEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_8_0; |
@@ -6,42 +6,42 @@ |
||
6 | 6 | |
7 | 7 | final class CoaleseEqualTokenEmulator extends TokenEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_7_4; |
|
12 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_7_4; |
|
12 | + } |
|
13 | 13 | |
14 | - public function isEmulationNeeded(string $code): bool |
|
15 | - { |
|
16 | - return strpos($code, '??=') !== false; |
|
17 | - } |
|
14 | + public function isEmulationNeeded(string $code): bool |
|
15 | + { |
|
16 | + return strpos($code, '??=') !== false; |
|
17 | + } |
|
18 | 18 | |
19 | - public function emulate(string $code, array $tokens): array |
|
20 | - { |
|
21 | - // We need to manually iterate and manage a count because we'll change |
|
22 | - // the tokens array on the way |
|
23 | - $line = 1; |
|
24 | - for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | - if (isset($tokens[$i + 1])) { |
|
26 | - if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') { |
|
27 | - array_splice($tokens, $i, 2, [ |
|
28 | - [\T_COALESCE_EQUAL, '??=', $line] |
|
29 | - ]); |
|
30 | - $c--; |
|
31 | - continue; |
|
32 | - } |
|
33 | - } |
|
34 | - if (\is_array($tokens[$i])) { |
|
35 | - $line += substr_count($tokens[$i][1], "\n"); |
|
36 | - } |
|
37 | - } |
|
19 | + public function emulate(string $code, array $tokens): array |
|
20 | + { |
|
21 | + // We need to manually iterate and manage a count because we'll change |
|
22 | + // the tokens array on the way |
|
23 | + $line = 1; |
|
24 | + for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | + if (isset($tokens[$i + 1])) { |
|
26 | + if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') { |
|
27 | + array_splice($tokens, $i, 2, [ |
|
28 | + [\T_COALESCE_EQUAL, '??=', $line] |
|
29 | + ]); |
|
30 | + $c--; |
|
31 | + continue; |
|
32 | + } |
|
33 | + } |
|
34 | + if (\is_array($tokens[$i])) { |
|
35 | + $line += substr_count($tokens[$i][1], "\n"); |
|
36 | + } |
|
37 | + } |
|
38 | 38 | |
39 | - return $tokens; |
|
40 | - } |
|
39 | + return $tokens; |
|
40 | + } |
|
41 | 41 | |
42 | - public function reverseEmulate(string $code, array $tokens): array |
|
43 | - { |
|
44 | - // ??= was not valid code previously, don't bother. |
|
45 | - return $tokens; |
|
46 | - } |
|
42 | + public function reverseEmulate(string $code, array $tokens): array |
|
43 | + { |
|
44 | + // ??= was not valid code previously, don't bother. |
|
45 | + return $tokens; |
|
46 | + } |
|
47 | 47 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class CoaleseEqualTokenEmulator extends TokenEmulator |
|
8 | -{ |
|
7 | +final class CoaleseEqualTokenEmulator extends TokenEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_7_4; |
@@ -6,26 +6,26 @@ |
||
6 | 6 | |
7 | 7 | final class EnumTokenEmulator extends KeywordEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_8_1; |
|
12 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_8_1; |
|
12 | + } |
|
13 | 13 | |
14 | - public function getKeywordString(): string |
|
15 | - { |
|
16 | - return 'enum'; |
|
17 | - } |
|
14 | + public function getKeywordString(): string |
|
15 | + { |
|
16 | + return 'enum'; |
|
17 | + } |
|
18 | 18 | |
19 | - public function getKeywordToken(): int |
|
20 | - { |
|
21 | - return \T_ENUM; |
|
22 | - } |
|
19 | + public function getKeywordToken(): int |
|
20 | + { |
|
21 | + return \T_ENUM; |
|
22 | + } |
|
23 | 23 | |
24 | - protected function isKeywordContext(array $tokens, int $pos): bool |
|
25 | - { |
|
26 | - return parent::isKeywordContext($tokens, $pos) |
|
27 | - && isset($tokens[$pos + 2]) |
|
28 | - && $tokens[$pos + 1][0] === \T_WHITESPACE |
|
29 | - && $tokens[$pos + 2][0] === \T_STRING; |
|
30 | - } |
|
24 | + protected function isKeywordContext(array $tokens, int $pos): bool |
|
25 | + { |
|
26 | + return parent::isKeywordContext($tokens, $pos) |
|
27 | + && isset($tokens[$pos + 2]) |
|
28 | + && $tokens[$pos + 1][0] === \T_WHITESPACE |
|
29 | + && $tokens[$pos + 2][0] === \T_STRING; |
|
30 | + } |
|
31 | 31 | } |
32 | 32 | \ No newline at end of file |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class EnumTokenEmulator extends KeywordEmulator |
|
8 | -{ |
|
7 | +final class EnumTokenEmulator extends KeywordEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_8_1; |
@@ -6,51 +6,51 @@ |
||
6 | 6 | |
7 | 7 | final class AttributeEmulator extends TokenEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_8_0; |
|
12 | - } |
|
13 | - |
|
14 | - public function isEmulationNeeded(string $code) : bool |
|
15 | - { |
|
16 | - return strpos($code, '#[') !== false; |
|
17 | - } |
|
18 | - |
|
19 | - public function emulate(string $code, array $tokens): array |
|
20 | - { |
|
21 | - // We need to manually iterate and manage a count because we'll change |
|
22 | - // the tokens array on the way. |
|
23 | - $line = 1; |
|
24 | - for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | - if ($tokens[$i] === '#' && isset($tokens[$i + 1]) && $tokens[$i + 1] === '[') { |
|
26 | - array_splice($tokens, $i, 2, [ |
|
27 | - [\T_ATTRIBUTE, '#[', $line] |
|
28 | - ]); |
|
29 | - $c--; |
|
30 | - continue; |
|
31 | - } |
|
32 | - if (\is_array($tokens[$i])) { |
|
33 | - $line += substr_count($tokens[$i][1], "\n"); |
|
34 | - } |
|
35 | - } |
|
36 | - |
|
37 | - return $tokens; |
|
38 | - } |
|
39 | - |
|
40 | - public function reverseEmulate(string $code, array $tokens): array |
|
41 | - { |
|
42 | - // TODO |
|
43 | - return $tokens; |
|
44 | - } |
|
45 | - |
|
46 | - public function preprocessCode(string $code, array &$patches): string { |
|
47 | - $pos = 0; |
|
48 | - while (false !== $pos = strpos($code, '#[', $pos)) { |
|
49 | - // Replace #[ with %[ |
|
50 | - $code[$pos] = '%'; |
|
51 | - $patches[] = [$pos, 'replace', '#']; |
|
52 | - $pos += 2; |
|
53 | - } |
|
54 | - return $code; |
|
55 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_8_0; |
|
12 | + } |
|
13 | + |
|
14 | + public function isEmulationNeeded(string $code) : bool |
|
15 | + { |
|
16 | + return strpos($code, '#[') !== false; |
|
17 | + } |
|
18 | + |
|
19 | + public function emulate(string $code, array $tokens): array |
|
20 | + { |
|
21 | + // We need to manually iterate and manage a count because we'll change |
|
22 | + // the tokens array on the way. |
|
23 | + $line = 1; |
|
24 | + for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | + if ($tokens[$i] === '#' && isset($tokens[$i + 1]) && $tokens[$i + 1] === '[') { |
|
26 | + array_splice($tokens, $i, 2, [ |
|
27 | + [\T_ATTRIBUTE, '#[', $line] |
|
28 | + ]); |
|
29 | + $c--; |
|
30 | + continue; |
|
31 | + } |
|
32 | + if (\is_array($tokens[$i])) { |
|
33 | + $line += substr_count($tokens[$i][1], "\n"); |
|
34 | + } |
|
35 | + } |
|
36 | + |
|
37 | + return $tokens; |
|
38 | + } |
|
39 | + |
|
40 | + public function reverseEmulate(string $code, array $tokens): array |
|
41 | + { |
|
42 | + // TODO |
|
43 | + return $tokens; |
|
44 | + } |
|
45 | + |
|
46 | + public function preprocessCode(string $code, array &$patches): string { |
|
47 | + $pos = 0; |
|
48 | + while (false !== $pos = strpos($code, '#[', $pos)) { |
|
49 | + // Replace #[ with %[ |
|
50 | + $code[$pos] = '%'; |
|
51 | + $patches[] = [$pos, 'replace', '#']; |
|
52 | + $pos += 2; |
|
53 | + } |
|
54 | + return $code; |
|
55 | + } |
|
56 | 56 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class AttributeEmulator extends TokenEmulator |
|
8 | -{ |
|
7 | +final class AttributeEmulator extends TokenEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_8_0; |
@@ -6,31 +6,31 @@ |
||
6 | 6 | |
7 | 7 | final class ReadonlyTokenEmulator extends KeywordEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_8_1; |
|
12 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_8_1; |
|
12 | + } |
|
13 | 13 | |
14 | - public function getKeywordString(): string |
|
15 | - { |
|
16 | - return 'readonly'; |
|
17 | - } |
|
14 | + public function getKeywordString(): string |
|
15 | + { |
|
16 | + return 'readonly'; |
|
17 | + } |
|
18 | 18 | |
19 | - public function getKeywordToken(): int |
|
20 | - { |
|
21 | - return \T_READONLY; |
|
22 | - } |
|
19 | + public function getKeywordToken(): int |
|
20 | + { |
|
21 | + return \T_READONLY; |
|
22 | + } |
|
23 | 23 | |
24 | - protected function isKeywordContext(array $tokens, int $pos): bool |
|
25 | - { |
|
26 | - if (!parent::isKeywordContext($tokens, $pos)) { |
|
27 | - return false; |
|
28 | - } |
|
29 | - // Support "function readonly(" |
|
30 | - return !(isset($tokens[$pos + 1]) && |
|
31 | - ($tokens[$pos + 1][0] === '(' || |
|
32 | - ($tokens[$pos + 1][0] === \T_WHITESPACE && |
|
33 | - isset($tokens[$pos + 2]) && |
|
34 | - $tokens[$pos + 2][0] === '('))); |
|
35 | - } |
|
24 | + protected function isKeywordContext(array $tokens, int $pos): bool |
|
25 | + { |
|
26 | + if (!parent::isKeywordContext($tokens, $pos)) { |
|
27 | + return false; |
|
28 | + } |
|
29 | + // Support "function readonly(" |
|
30 | + return !(isset($tokens[$pos + 1]) && |
|
31 | + ($tokens[$pos + 1][0] === '(' || |
|
32 | + ($tokens[$pos + 1][0] === \T_WHITESPACE && |
|
33 | + isset($tokens[$pos + 2]) && |
|
34 | + $tokens[$pos + 2][0] === '('))); |
|
35 | + } |
|
36 | 36 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class ReadonlyTokenEmulator extends KeywordEmulator |
|
8 | -{ |
|
7 | +final class ReadonlyTokenEmulator extends KeywordEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_8_1; |
@@ -6,62 +6,62 @@ |
||
6 | 6 | |
7 | 7 | final class NullsafeTokenEmulator extends TokenEmulator |
8 | 8 | { |
9 | - public function getPhpVersion(): string |
|
10 | - { |
|
11 | - return Emulative::PHP_8_0; |
|
12 | - } |
|
9 | + public function getPhpVersion(): string |
|
10 | + { |
|
11 | + return Emulative::PHP_8_0; |
|
12 | + } |
|
13 | 13 | |
14 | - public function isEmulationNeeded(string $code): bool |
|
15 | - { |
|
16 | - return strpos($code, '?->') !== false; |
|
17 | - } |
|
14 | + public function isEmulationNeeded(string $code): bool |
|
15 | + { |
|
16 | + return strpos($code, '?->') !== false; |
|
17 | + } |
|
18 | 18 | |
19 | - public function emulate(string $code, array $tokens): array |
|
20 | - { |
|
21 | - // We need to manually iterate and manage a count because we'll change |
|
22 | - // the tokens array on the way |
|
23 | - $line = 1; |
|
24 | - for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | - if ($tokens[$i] === '?' && isset($tokens[$i + 1]) && $tokens[$i + 1][0] === \T_OBJECT_OPERATOR) { |
|
26 | - array_splice($tokens, $i, 2, [ |
|
27 | - [\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line] |
|
28 | - ]); |
|
29 | - $c--; |
|
30 | - continue; |
|
31 | - } |
|
19 | + public function emulate(string $code, array $tokens): array |
|
20 | + { |
|
21 | + // We need to manually iterate and manage a count because we'll change |
|
22 | + // the tokens array on the way |
|
23 | + $line = 1; |
|
24 | + for ($i = 0, $c = count($tokens); $i < $c; ++$i) { |
|
25 | + if ($tokens[$i] === '?' && isset($tokens[$i + 1]) && $tokens[$i + 1][0] === \T_OBJECT_OPERATOR) { |
|
26 | + array_splice($tokens, $i, 2, [ |
|
27 | + [\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line] |
|
28 | + ]); |
|
29 | + $c--; |
|
30 | + continue; |
|
31 | + } |
|
32 | 32 | |
33 | - // Handle ?-> inside encapsed string. |
|
34 | - if ($tokens[$i][0] === \T_ENCAPSED_AND_WHITESPACE && isset($tokens[$i - 1]) |
|
35 | - && $tokens[$i - 1][0] === \T_VARIABLE |
|
36 | - && preg_match('/^\?->([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)/', $tokens[$i][1], $matches) |
|
37 | - ) { |
|
38 | - $replacement = [ |
|
39 | - [\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line], |
|
40 | - [\T_STRING, $matches[1], $line], |
|
41 | - ]; |
|
42 | - if (\strlen($matches[0]) !== \strlen($tokens[$i][1])) { |
|
43 | - $replacement[] = [ |
|
44 | - \T_ENCAPSED_AND_WHITESPACE, |
|
45 | - \substr($tokens[$i][1], \strlen($matches[0])), |
|
46 | - $line |
|
47 | - ]; |
|
48 | - } |
|
49 | - array_splice($tokens, $i, 1, $replacement); |
|
50 | - $c += \count($replacement) - 1; |
|
51 | - continue; |
|
52 | - } |
|
33 | + // Handle ?-> inside encapsed string. |
|
34 | + if ($tokens[$i][0] === \T_ENCAPSED_AND_WHITESPACE && isset($tokens[$i - 1]) |
|
35 | + && $tokens[$i - 1][0] === \T_VARIABLE |
|
36 | + && preg_match('/^\?->([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)/', $tokens[$i][1], $matches) |
|
37 | + ) { |
|
38 | + $replacement = [ |
|
39 | + [\T_NULLSAFE_OBJECT_OPERATOR, '?->', $line], |
|
40 | + [\T_STRING, $matches[1], $line], |
|
41 | + ]; |
|
42 | + if (\strlen($matches[0]) !== \strlen($tokens[$i][1])) { |
|
43 | + $replacement[] = [ |
|
44 | + \T_ENCAPSED_AND_WHITESPACE, |
|
45 | + \substr($tokens[$i][1], \strlen($matches[0])), |
|
46 | + $line |
|
47 | + ]; |
|
48 | + } |
|
49 | + array_splice($tokens, $i, 1, $replacement); |
|
50 | + $c += \count($replacement) - 1; |
|
51 | + continue; |
|
52 | + } |
|
53 | 53 | |
54 | - if (\is_array($tokens[$i])) { |
|
55 | - $line += substr_count($tokens[$i][1], "\n"); |
|
56 | - } |
|
57 | - } |
|
54 | + if (\is_array($tokens[$i])) { |
|
55 | + $line += substr_count($tokens[$i][1], "\n"); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - return $tokens; |
|
60 | - } |
|
59 | + return $tokens; |
|
60 | + } |
|
61 | 61 | |
62 | - public function reverseEmulate(string $code, array $tokens): array |
|
63 | - { |
|
64 | - // ?-> was not valid code previously, don't bother. |
|
65 | - return $tokens; |
|
66 | - } |
|
62 | + public function reverseEmulate(string $code, array $tokens): array |
|
63 | + { |
|
64 | + // ?-> was not valid code previously, don't bother. |
|
65 | + return $tokens; |
|
66 | + } |
|
67 | 67 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use PhpParser\Lexer\Emulative; |
6 | 6 | |
7 | -final class NullsafeTokenEmulator extends TokenEmulator |
|
8 | -{ |
|
7 | +final class NullsafeTokenEmulator extends TokenEmulator { |
|
9 | 8 | public function getPhpVersion(): string |
10 | 9 | { |
11 | 10 | return Emulative::PHP_8_0; |