1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-13 09:57:25 +01:00
PHP-Parser/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php
Nikita Popov 6f74784e16 Switch to a normalized token representation
Each token is now represented by a Token object.
2019-06-30 14:14:24 +02:00

40 lines
1.2 KiB
PHP

<?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;
}
}