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

Breadcrumb

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

function NameContext::getPossibleNames

Get possible ways of writing a fully qualified name (e.g., by making use of aliases).

Parameters

string $name Fully-qualified name (without leading namespace separator):

Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*:

Return value

Name[] Possible representations of the name

1 call to NameContext::getPossibleNames()
NameContext::getShortName in vendor/nikic/php-parser/lib/PhpParser/NameContext.php
Get shortest representation of this fully-qualified name.

File

vendor/nikic/php-parser/lib/PhpParser/NameContext.php, line 156

Class

NameContext

Namespace

PhpParser

Code

public function getPossibleNames(string $name, int $type) : array {
    $lcName = strtolower($name);
    if ($type === Stmt\Use_::TYPE_NORMAL) {
        // self, parent and static must always be unqualified
        if ($lcName === "self" || $lcName === "parent" || $lcName === "static") {
            return [
                new Name($name),
            ];
        }
    }
    // Collect possible ways to write this name, starting with the fully-qualified name
    $possibleNames = [
        new FullyQualified($name),
    ];
    if (null !== ($nsRelativeName = $this->getNamespaceRelativeName($name, $lcName, $type))) {
        // Make sure there is no alias that makes the normally namespace-relative name
        // into something else
        if (null === $this->resolveAlias($nsRelativeName, $type)) {
            $possibleNames[] = $nsRelativeName;
        }
    }
    // Check for relevant namespace use statements
    foreach ($this->origAliases[Stmt\Use_::TYPE_NORMAL] as $alias => $orig) {
        $lcOrig = $orig->toLowerString();
        if (0 === strpos($lcName, $lcOrig . '\\')) {
            $possibleNames[] = new Name($alias . substr($name, strlen($lcOrig)));
        }
    }
    // Check for relevant type-specific use statements
    foreach ($this->origAliases[$type] as $alias => $orig) {
        if ($type === Stmt\Use_::TYPE_CONSTANT) {
            // Constants are complicated-sensitive
            $normalizedOrig = $this->normalizeConstName($orig->toString());
            if ($normalizedOrig === $this->normalizeConstName($name)) {
                $possibleNames[] = new Name($alias);
            }
        }
        else {
            // Everything else is case-insensitive
            if ($orig->toLowerString() === $lcName) {
                $possibleNames[] = new Name($alias);
            }
        }
    }
    return $possibleNames;
}

API Navigation

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