You are here

public function CoreVersionRequirementSniff::process in Coder 8.3.x

Same name and namespace in other branches
  1. 8.3 coder_sniffer/DrupalPractice/Sniffs/InfoFiles/CoreVersionRequirementSniff.php \DrupalPractice\Sniffs\InfoFiles\CoreVersionRequirementSniff::process()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

int

File

coder_sniffer/DrupalPractice/Sniffs/InfoFiles/CoreVersionRequirementSniff.php, line 50

Class

CoreVersionRequirementSniff
Checks if the *.info.yml file contains core_version_requirement.

Namespace

DrupalPractice\Sniffs\InfoFiles

Code

public function process(File $phpcsFile, $stackPtr) {
  $filename = $phpcsFile
    ->getFilename();
  $fileExtension = strtolower(substr($filename, -9));
  if ($fileExtension !== '.info.yml') {
    return $phpcsFile->numTokens + 1;
  }

  // Exclude config files which might contain the info.yml extension.
  $filenameWithoutExtension = substr($filename, 0, -9);
  if (strpos($filenameWithoutExtension, '.') !== false) {
    return $phpcsFile->numTokens + 1;
  }
  $contents = file_get_contents($phpcsFile
    ->getFilename());
  try {
    $info = Yaml::parse($contents);
  } catch (ParseException $e) {

    // If the YAML is invalid we ignore this file.
    return $phpcsFile->numTokens + 1;
  }

  // Check if the type key is set, to verify if we're inside a project info.yml file.
  if (isset($info['type']) === false) {
    return $phpcsFile->numTokens + 1;
  }

  // Test modules can omit the core_version_requirement key.
  if (isset($info['package']) === true && $info['package'] === 'Testing') {
    return $phpcsFile->numTokens + 1;
  }
  if (isset($info['core_version_requirement']) === false) {
    $warning = '"core_version_requirement" property is missing in the info.yml file';
    $phpcsFile
      ->addWarning($warning, $stackPtr, 'CoreVersionRequirement');
  }
  return $phpcsFile->numTokens + 1;
}