Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AsymmetricVisibilityTokenEmulator.php

class AsymmetricVisibilityTokenEmulator

Hierarchy

  • class \PhpParser\Lexer\TokenEmulator\TokenEmulator
    • class \PhpParser\Lexer\TokenEmulator\AsymmetricVisibilityTokenEmulator extends \PhpParser\Lexer\TokenEmulator\TokenEmulator

Expanded class hierarchy of AsymmetricVisibilityTokenEmulator

1 file declares its use of AsymmetricVisibilityTokenEmulator
Emulative.php in vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php

File

vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/AsymmetricVisibilityTokenEmulator.php, line 8

Namespace

PhpParser\Lexer\TokenEmulator
View source
final class AsymmetricVisibilityTokenEmulator extends TokenEmulator {
    public function getPhpVersion() : PhpVersion {
        return PhpVersion::fromComponents(8, 4);
    }
    public function isEmulationNeeded(string $code) : bool {
        $code = strtolower($code);
        return strpos($code, 'public(set)') !== false || strpos($code, 'protected(set)') !== false || strpos($code, 'private(set)') !== false;
    }
    public function emulate(string $code, array $tokens) : array {
        $map = [
            \T_PUBLIC => \T_PUBLIC_SET,
            \T_PROTECTED => \T_PROTECTED_SET,
            \T_PRIVATE => \T_PRIVATE_SET,
        ];
        for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
            $token = $tokens[$i];
            if (isset($map[$token->id]) && $i + 3 < $c && $tokens[$i + 1]->text === '(' && $tokens[$i + 2]->id === \T_STRING && \strtolower($tokens[$i + 2]->text) === 'set' && $tokens[$i + 3]->text === ')' && $this->isKeywordContext($tokens, $i)) {
                array_splice($tokens, $i, 4, [
                    new Token($map[$token->id], $token->text . '(' . $tokens[$i + 2]->text . ')', $token->line, $token->pos),
                ]);
                $c -= 3;
            }
        }
        return $tokens;
    }
    public function reverseEmulate(string $code, array $tokens) : array {
        $reverseMap = [
            \T_PUBLIC_SET => \T_PUBLIC,
            \T_PROTECTED_SET => \T_PROTECTED,
            \T_PRIVATE_SET => \T_PRIVATE,
        ];
        for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
            $token = $tokens[$i];
            if (isset($reverseMap[$token->id]) && \preg_match('/(public|protected|private)\\((set)\\)/i', $token->text, $matches)) {
                [
                    ,
                    $modifier,
                    $set,
                ] = $matches;
                $modifierLen = \strlen($modifier);
                array_splice($tokens, $i, 1, [
                    new Token($reverseMap[$token->id], $modifier, $token->line, $token->pos),
                    new Token(\ord('('), '(', $token->line, $token->pos + $modifierLen),
                    new Token(\T_STRING, $set, $token->line, $token->pos + $modifierLen + 1),
                    new Token(\ord(')'), ')', $token->line, $token->pos + $modifierLen + 4),
                ]);
                $i += 3;
                $c += 3;
            }
        }
        return $tokens;
    }
    
    /** @param Token[] $tokens */
    protected function isKeywordContext(array $tokens, int $pos) : bool {
        $prevToken = $this->getPreviousNonSpaceToken($tokens, $pos);
        if ($prevToken === null) {
            return false;
        }
        return $prevToken->id !== \T_OBJECT_OPERATOR && $prevToken->id !== \T_NULLSAFE_OBJECT_OPERATOR;
    }
    
    /** @param Token[] $tokens */
    private function getPreviousNonSpaceToken(array $tokens, int $start) : ?Token {
        for ($i = $start - 1; $i >= 0; --$i) {
            if ($tokens[$i]->id === T_WHITESPACE) {
                continue;
            }
            return $tokens[$i];
        }
        return null;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AsymmetricVisibilityTokenEmulator::emulate public function Overrides TokenEmulator::emulate
AsymmetricVisibilityTokenEmulator::getPhpVersion public function Overrides TokenEmulator::getPhpVersion
AsymmetricVisibilityTokenEmulator::getPreviousNonSpaceToken private function
AsymmetricVisibilityTokenEmulator::isEmulationNeeded public function Overrides TokenEmulator::isEmulationNeeded
AsymmetricVisibilityTokenEmulator::isKeywordContext protected function
AsymmetricVisibilityTokenEmulator::reverseEmulate public function Overrides TokenEmulator::reverseEmulate
TokenEmulator::preprocessCode public function 2

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal