You are here

public static function Project::getName in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/DrupalPractice/Project.php \DrupalPractice\Project::getName()
  2. 8.3.x coder_sniffer/DrupalPractice/Project.php \DrupalPractice\Project::getName()

Determines the project short name a file might be associated with.

Parameters

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

Return value

string|false Returns the project machine name or false if it could not be derived.

4 calls to Project::getName()
ClassNameSniff::process in coder_sniffer/DrupalPractice/Sniffs/General/ClassNameSniff.php
Processes this test, when one of its tokens is encountered.
FormAlterDocSniff::processFunction in coder_sniffer/DrupalPractice/Sniffs/FunctionDefinitions/FormAlterDocSniff.php
Process this function definition.
ProjectUnitTest::testProjectNameDetection in tests/DrupalPractice/ProjectDetection/ProjectUnitTest.php
Tests the extending classes Sniff class.
VariableNameSniff::processFunctionCall in coder_sniffer/DrupalPractice/Sniffs/General/VariableNameSniff.php
Processes this function call.

File

coder_sniffer/DrupalPractice/Project.php, line 36

Class

Project
Helper class to retrieve project information like module/theme name for a file.

Namespace

DrupalPractice

Code

public static function getName(File $phpcsFile) {

  // Cache the project name per file as this might get called often.
  static $cache;
  if (isset($cache[$phpcsFile
    ->getFilename()]) === true) {
    return $cache[$phpcsFile
      ->getFilename()];
  }
  $pathParts = pathinfo($phpcsFile
    ->getFilename());

  // Module and install files are easy: they contain the project name in the
  // file name.
  if (isset($pathParts['extension']) === true && in_array($pathParts['extension'], [
    'install',
    'module',
    'profile',
    'theme',
  ]) === true) {
    $cache[$phpcsFile
      ->getFilename()] = $pathParts['filename'];
    return $pathParts['filename'];
  }
  $infoFile = static::getInfoFile($phpcsFile);
  if ($infoFile === false) {
    return false;
  }
  $pathParts = pathinfo($infoFile);

  // Info files end in *.info.yml on Drupal 8 and *.info on Drupal 7.
  $filename = $pathParts['filename'];
  $filename = preg_replace('/\\.info$/', '', $filename);
  $cache[$phpcsFile
    ->getFilename()] = $filename;
  return $filename;
}