1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Lexer\TokenEmulator;
4:
5: use PhpParser\Lexer\Emulative;
6:
7: final class CoaleseEqualTokenEmulator extends TokenEmulator
8: {
9: public function getPhpVersion(): string
10: {
11: return Emulative::PHP_7_4;
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][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:
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: