1<?php declare(strict_types=1);
2
3namespace PhpParser\Lexer\TokenEmulator;
4
5use PhpParser\Lexer\Emulative;
6
7final class NullsafeTokenEmulator implements TokenEmulatorInterface
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 (isset($tokens[$i + 1])) {
26                if ($tokens[$i] === '?' && $tokens[$i + 1][0] === \T_OBJECT_OPERATOR) {
27                    array_splice($tokens, $i, 2, [
28                        [\T_NULLSAFE_OBJECT_OPERATOR, '?->', $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
39        return $tokens;
40    }
41
42    public function reverseEmulate(string $code, array $tokens): array
43    {
44        // ?-> was not valid code previously, don't bother.
45        return $tokens;
46    }
47}
48