1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Lexer\TokenEmulator;
4:
5: use PhpParser\Lexer\TokenEmulator\TokenEmulator;
6: use PhpParser\PhpVersion;
7: use PhpParser\Token;
8:
9: class PipeOperatorEmulator extends TokenEmulator {
10: public function getPhpVersion(): PhpVersion {
11: return PhpVersion::fromComponents(8, 5);
12: }
13:
14: public function isEmulationNeeded(string $code): bool {
15: return \strpos($code, '|>') !== false;
16: }
17:
18: public function emulate(string $code, array $tokens): array {
19: for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
20: $token = $tokens[$i];
21: if ($token->text === '|' && isset($tokens[$i + 1]) && $tokens[$i + 1]->text === '>') {
22: array_splice($tokens, $i, 2, [
23: new Token(\T_PIPE, '|>', $token->line, $token->pos),
24: ]);
25: $c--;
26: }
27: }
28: return $tokens;
29: }
30:
31: public function reverseEmulate(string $code, array $tokens): array {
32: for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
33: $token = $tokens[$i];
34: if ($token->id === \T_PIPE) {
35: array_splice($tokens, $i, 1, [
36: new Token(\ord('|'), '|', $token->line, $token->pos),
37: new Token(\ord('>'), '>', $token->line, $token->pos + 1),
38: ]);
39: $i++;
40: $c++;
41: }
42: }
43: return $tokens;
44: }
45: }
46: