/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // Only run this sniff once per info file. $end = count($phpcsFile->getTokens()) + 1; $fileExtension = strtolower(substr($phpcsFile->getFilename(), -4)); if ($fileExtension !== 'info') { return $end; } $tokens = $phpcsFile->getTokens(); $contents = file_get_contents($phpcsFile->getFilename()); $info = Drupal_Sniffs_InfoFiles_ClassFilesSniff::drupalParseInfoFormat($contents); if (isset($info['name']) === false) { $error = '"name" property is missing in the info file'; $phpcsFile->addError($error, $stackPtr, 'Name'); } if (isset($info['description']) === false) { $error = '"description" property is missing in the info file'; $phpcsFile->addError($error, $stackPtr, 'Description'); } if (isset($info['core']) === false) { $error = '"core" property is missing in the info file'; $phpcsFile->addError($error, $stackPtr, 'Core'); } else { if ($info['core'] === '7.x' && isset($info['php']) === true && $info['php'] <= '5.2') { $error = 'Drupal 7 core already requires PHP 5.2'; $ptr = Drupal_Sniffs_InfoFiles_ClassFilesSniff::getPtr('php', $info['php'], $phpcsFile); $phpcsFile->addError($error, $ptr, 'D7PHPVersion'); } } return $end; }
/** * Determines the Drupal core version a file might be associated with. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * * @return string|false The core version string or false if it could not * be derived. */ public static function getCoreVersion(PHP_CodeSniffer_File $phpcsFile) { $infoFile = static::getInfoFile($phpcsFile); if ($infoFile === false) { return false; } $pathParts = pathinfo($infoFile); // Drupal 6 and 7 use the .info file extension. if ($pathParts['extension'] === 'info') { $info_settings = Drupal_Sniffs_InfoFiles_ClassFilesSniff::drupalParseInfoFormat(file_get_contents($infoFile)); if (isset($info_settings['core'])) { return $info_settings['core']; } } else { // Drupal 8 uses the .yml file extension. // @todo Revisit for Drupal 9, but I don't want to do YAML parsing // for now. return '8.x'; } }