function StaticReflectionParser::parse

Same name in other branches
  1. 9 core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php \Drupal\Component\Annotation\Doctrine\StaticReflectionParser::parse()
  2. 11.x core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php \Drupal\Component\Annotation\Doctrine\StaticReflectionParser::parse()

Return value

void

4 calls to StaticReflectionParser::parse()
StaticReflectionParser::getDocComment in core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php
Gets the doc comment.
StaticReflectionParser::getStaticReflectionParserForDeclaringClass in core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php
Gets the PSR-0 parser for the declaring class.
StaticReflectionParser::getUseStatements in core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php
Gets the use statements from this file.
StaticReflectionParser::hasClassAttribute in core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php
Determines if the class has the provided class attribute.

File

core/lib/Drupal/Component/Annotation/Doctrine/StaticReflectionParser.php, line 169

Class

StaticReflectionParser
Parses a file for namespaces/use/class declarations.

Namespace

Drupal\Component\Annotation\Doctrine

Code

protected function parse() {
    $fileName = $this->finder
        ->findFile($this->className);
    if ($this->parsed || !$fileName) {
        return;
    }
    $this->parsed = true;
    $contents = file_get_contents($fileName);
    if ($this->classAnnotationOptimize) {
        $regex = sprintf('/\\A.*^\\s*((abstract|final)\\s+)?class\\s+%s\\s+/sm', $this->shortClassName);
        if (preg_match($regex, $contents, $matches)) {
            $contents = $matches[0];
        }
    }
    $tokenParser = new TokenParser($contents);
    $docComment = '';
    $last_token = false;
    $attributeNames = [];
    while ($token = $tokenParser->next(false)) {
        switch ($token[0]) {
            case T_USE:
                $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
                break;
            case T_DOC_COMMENT:
                $docComment = $token[1];
                break;
            case T_ATTRIBUTE:
                while ($token = $tokenParser->next()) {
                    if ($token[0] === T_NAME_FULLY_QUALIFIED || $token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_RELATIVE || $token[0] === T_STRING) {
                        $attributeNames[] = $token[1];
                        break 2;
                    }
                }
                break;
            case T_CLASS:
                // Convert the attributes to fully qualified names.
                $this->classAttributes = array_map(fn($name) => $this->fullySpecifyName($name), $attributeNames);
                if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM && $last_token !== T_NEW) {
                    $this->docComment['class'] = $docComment;
                    $docComment = '';
                }
                break;
            case T_VAR:
            case T_PRIVATE:
            case T_PROTECTED:
            case T_PUBLIC:
                $token = $tokenParser->next();
                if ($token[0] === T_VARIABLE) {
                    $propertyName = substr($token[1], 1);
                    $this->docComment['property'][$propertyName] = $docComment;
                    continue 2;
                }
                if ($token[0] !== T_FUNCTION) {
                    // For example, it can be T_FINAL.
                    continue 2;
                }
            // No break.
            case T_FUNCTION:
                // The next string after function is the name, but
                // there can be & before the function name so find the
                // string.
                while (($token = $tokenParser->next()) && $token[0] !== T_STRING) {
                    continue;
                }
                if ($token === null) {
                    break;
                }
                $methodName = $token[1];
                $this->docComment['method'][$methodName] = $docComment;
                $docComment = '';
                break;
            case T_EXTENDS:
                $this->parentClassName = $this->fullySpecifyName($tokenParser->parseClass());
                break;
        }
        $last_token = is_array($token) ? $token[0] : false;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.