1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-13 18:07:32 +01:00
PHP-Parser/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php

40 lines
1.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace PhpParser\Lexer\TokenEmulator;
use PhpParser\Lexer\Emulative;
use PhpParser\Token;
final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface
{
public function isEmulationNeeded(string $code) : bool
{
// skip version where this is supported
if (version_compare(\PHP_VERSION, Emulative::PHP_7_4, '>=')) {
return false;
}
return strpos($code, '??=') !== false;
}
public function emulate(string $code, array $tokens): array
{
// We need to manually iterate and manage a count because we'll change
// the tokens array on the way
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
if (isset($tokens[$i + 1])) {
$token = $tokens[$i];
if ($token->id === \T_COALESCE && $tokens[$i + 1]->value === '=') {
array_splice($tokens, $i, 2, [
new Token(Emulative::T_COALESCE_EQUAL, '??=', $token->line, $token->filePos),
]);
$c--;
continue;
}
}
}
return $tokens;
}
}