You are here

public static function Project::getServicesYmlFile in Coder 8.2

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

Determines the *.services.yml file in a module.

Parameters

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

Return value

string|false The Services YML file name or false if it could not be derived.

1 call to Project::getServicesYmlFile()
Project::isServiceClass in coder_sniffer/DrupalPractice/Project.php
Return true if the given class is a Drupal service registered in *.services.yml.

File

coder_sniffer/DrupalPractice/Project.php, line 118

Class

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

Namespace

DrupalPractice

Code

public static function getServicesYmlFile(File $phpcsFile) {

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

  // Search for an info file.
  $dir = $pathParts['dirname'];
  do {
    $ymlFiles = glob("{$dir}/*.services.yml");

    // Go one directory up if we do not find an info file here.
    $dir = dirname($dir);
  } while (empty($ymlFiles) === true && $dir !== dirname($dir));

  // No YML file found, so we give up.
  if (empty($ymlFiles) === true) {
    $cache[$phpcsFile
      ->getFilename()] = false;
    return false;
  }

  // Sort the YML file names and take the shortest info file.
  usort($ymlFiles, array(
    __NAMESPACE__ . '\\Project',
    'compareLength',
  ));
  $ymlFile = $ymlFiles[0];
  $cache[$phpcsFile
    ->getFilename()] = $ymlFile;
  return $ymlFile;
}