0 follower

Final Class Yiisoft\VarDumper\UseStatementParser

InheritanceYiisoft\VarDumper\UseStatementParser

UseStatementParser given a PHP file, returns a set of use statements from the code.

Public Methods

Hide inherited methods

Method Description Defined By
fromFile() Returns a set of use statements from the code of the specified file. Yiisoft\VarDumper\UseStatementParser
isTokenIsPartOfUse() Checks whether the token is part of the use statement data. Yiisoft\VarDumper\UseStatementParser

Method Details

Hide inherited methods

fromFile() public method

Returns a set of use statements from the code of the specified file.

public array<string, string> fromFile ( string $file )
$file string

File to read.

return array<string, string>

Use statements data.

throws RuntimeException

if there is a problem reading file.

                public function fromFile(string $file): array
{
    if (!is_file($file)) {
        throw new RuntimeException("File \"{$file}\" does not exist.");
    }
    if (!is_readable($file)) {
        throw new RuntimeException("File \"{$file}\" is not readable.");
    }
    $fileContent = file_get_contents($file);
    if ($fileContent === false) {
        throw new RuntimeException("Failed to read file \"{$file}\".");
    }
    $tokens = token_get_all($fileContent);
    array_shift($tokens);
    $uses = [];
    foreach ($tokens as $i => $token) {
        if (!is_array($token)) {
            continue;
        }
        if ($token[0] === T_USE && isset($tokens[$i + 2]) && $this->isTokenIsPartOfUse($tokens[$i + 2])) {
            $uses += $this->normalize(array_slice($tokens, $i + 1));
            continue;
        }
    }
    return $uses;
}

            
isTokenIsPartOfUse() public method

Checks whether the token is part of the use statement data.

public boolean isTokenIsPartOfUse ( array|string $token )
$token array|string

PHP token.

return boolean

Whether the token is part of the use statement data.

                public function isTokenIsPartOfUse($token): bool
{
    if (!is_array($token)) {
        return false;
    }
    return $token[0] === T_STRING
        || $token[0] === T_NS_SEPARATOR
        || (defined('T_NAME_QUALIFIED') && $token[0] === T_NAME_QUALIFIED)
        || (defined('T_NAME_FULLY_QUALIFIED') && $token[0] === T_NAME_FULLY_QUALIFIED)
        || (defined('T_NAME_RELATIVE') && $token[0] === T_NAME_RELATIVE);
}