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

Breadcrumb

  1. Drupal Core 11.1.x

KeywordEmulator.php

Namespace

PhpParser\Lexer\TokenEmulator

File

vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php

View source
<?php

declare (strict_types=1);
namespace PhpParser\Lexer\TokenEmulator;

use PhpParser\Token;
abstract class KeywordEmulator extends TokenEmulator {
    public abstract function getKeywordString() : string;
    public abstract function getKeywordToken() : int;
    public function isEmulationNeeded(string $code) : bool {
        return strpos(strtolower($code), $this->getKeywordString()) !== false;
    }
    
    /** @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;
    }
    public function emulate(string $code, array $tokens) : array {
        $keywordString = $this->getKeywordString();
        foreach ($tokens as $i => $token) {
            if ($token->id === T_STRING && strtolower($token->text) === $keywordString && $this->isKeywordContext($tokens, $i)) {
                $token->id = $this->getKeywordToken();
            }
        }
        return $tokens;
    }
    
    /** @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;
    }
    public function reverseEmulate(string $code, array $tokens) : array {
        $keywordToken = $this->getKeywordToken();
        foreach ($tokens as $token) {
            if ($token->id === $keywordToken) {
                $token->id = \T_STRING;
            }
        }
        return $tokens;
    }

}

Classes

Title Deprecated Summary
KeywordEmulator
RSS feed
Powered by Drupal