mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-07-15 03:16:32 +02:00
.github
bin
doc
grammar
lib
PhpParser
Builder
Comment
ErrorHandler
Internal
Lexer
TokenEmulator
AsymmetricVisibilityTokenEmulator.php
AttributeEmulator.php
EnumTokenEmulator.php
ExplicitOctalEmulator.php
KeywordEmulator.php
MatchTokenEmulator.php
NullsafeTokenEmulator.php
PropertyTokenEmulator.php
ReadonlyFunctionTokenEmulator.php
ReadonlyTokenEmulator.php
ReverseEmulator.php
TokenEmulator.php
Emulative.php
Node
NodeVisitor
Parser
PrettyPrinter
Builder.php
BuilderFactory.php
BuilderHelpers.php
Comment.php
ConstExprEvaluationException.php
ConstExprEvaluator.php
Error.php
ErrorHandler.php
JsonDecoder.php
Lexer.php
Modifiers.php
NameContext.php
Node.php
NodeAbstract.php
NodeDumper.php
NodeFinder.php
NodeTraverser.php
NodeTraverserInterface.php
NodeVisitor.php
NodeVisitorAbstract.php
Parser.php
ParserAbstract.php
ParserFactory.php
PhpVersion.php
PrettyPrinter.php
PrettyPrinterAbstract.php
Token.php
compatibility_tokens.php
test
test_old
tools
.editorconfig
.gitattributes
.gitignore
.php-cs-fixer.dist.php
CHANGELOG.md
CONTRIBUTING.md
LICENSE
Makefile
README.md
UPGRADE-1.0.md
UPGRADE-2.0.md
UPGRADE-3.0.md
UPGRADE-4.0.md
UPGRADE-5.0.md
composer.json
phpstan-baseline.neon
phpstan.neon.dist
phpunit.xml.dist
The formatting in this project has become something of a mess, because it changed over time. Add a CS fixer config and reformat to the desired style, which is PSR-12, but with sane brace placement.
50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace PhpParser\Lexer\TokenEmulator;
|
|
|
|
use PhpParser\PhpVersion;
|
|
use PhpParser\Token;
|
|
|
|
final class AttributeEmulator extends TokenEmulator {
|
|
public function getPhpVersion(): PhpVersion {
|
|
return PhpVersion::fromComponents(8, 0);
|
|
}
|
|
|
|
public function isEmulationNeeded(string $code): bool {
|
|
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) {
|
|
$token = $tokens[$i];
|
|
if ($token->text === '#' && isset($tokens[$i + 1]) && $tokens[$i + 1]->text === '[') {
|
|
array_splice($tokens, $i, 2, [
|
|
new Token(\T_ATTRIBUTE, '#[', $token->line, $token->pos),
|
|
]);
|
|
$c--;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return $tokens;
|
|
}
|
|
|
|
public function reverseEmulate(string $code, array $tokens): array {
|
|
// TODO
|
|
return $tokens;
|
|
}
|
|
|
|
public function preprocessCode(string $code, array &$patches): string {
|
|
$pos = 0;
|
|
while (false !== $pos = strpos($code, '#[', $pos)) {
|
|
// Replace #[ with %[
|
|
$code[$pos] = '%';
|
|
$patches[] = [$pos, 'replace', '#'];
|
|
$pos += 2;
|
|
}
|
|
return $code;
|
|
}
|
|
}
|