You are here

protected function GlobalClassSniff::getFullyQualifiedName in Coder 8.3

Same name and namespace in other branches
  1. 8.3.x coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalClassSniff.php \DrupalPractice\Sniffs\Objects\GlobalClassSniff::getFullyQualifiedName()

Retrieve the fully qualified name of the given classname.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

string $className The classname for which to retrieve the FQN.:

Return value

string

1 call to GlobalClassSniff::getFullyQualifiedName()
GlobalClassSniff::process in coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalClassSniff.php
Processes this test, when one of its tokens is encountered.

File

coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalClassSniff.php, line 180

Class

GlobalClassSniff
Checks that Node::load() calls and friends are not used in forms, controllers or services.

Namespace

DrupalPractice\Sniffs\Objects

Code

protected function getFullyQualifiedName(File $phpcsFile, $className) {
  $useStatement = $phpcsFile
    ->findNext(T_USE, 0);
  while ($useStatement !== false) {
    $endPtr = $phpcsFile
      ->findEndOfStatement($useStatement);
    $useEnd = $phpcsFile
      ->findNext([
      T_STRING,
      T_NS_SEPARATOR,
      T_WHITESPACE,
    ], $useStatement + 1, null, true) - 1;
    $useFullName = trim($phpcsFile
      ->getTokensAsString($useStatement + 1, $useEnd - $useStatement), '\\ ');

    // Check if use statement contains an alias.
    $asPtr = $phpcsFile
      ->findNext(T_AS, $useEnd + 1, $endPtr);
    if ($asPtr !== false) {
      $aliasName = trim($phpcsFile
        ->getTokensAsString($asPtr + 1, $endPtr - 1 - $asPtr));
      if ($aliasName === $className) {
        return $useFullName;
      }
    }
    $parts = explode('\\', $useFullName);
    $useClassName = end($parts);

    // Check if the resulting classname is the classname we're looking
    // for.
    if ($useClassName === $className) {
      return $useFullName;
    }

    // Check if we're currently in a multi-use statement.
    $tokens = $phpcsFile
      ->getTokens();
    if ($tokens[$endPtr]['code'] === T_COMMA) {
      $useStatement = $endPtr;
      continue;
    }
    $useStatement = $phpcsFile
      ->findNext(T_USE, $useStatement + 1);
  }

  //end while
  return $className;
}