1<?php declare(strict_types=1);
2
3namespace PhpParser\Lexer\TokenEmulator;
4
5use PhpParser\Lexer\Emulative;
6
7final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface
8{
9    public function isEmulationNeeded(string $code) : bool
10    {
11        // skip version where this is supported
12        if (version_compare(\PHP_VERSION, Emulative::PHP_7_4, '>=')) {
13            return false;
14        }
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][0] === T_COALESCE && $tokens[$i + 1] === '=') {
27                    array_splice($tokens, $i, 2, [
28                        [Emulative::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
39        return $tokens;
40    }
41}
42