Skip to content

Commit

Permalink
Merge pull request #108 from ec-europa/feature/DQA-4077
Browse files Browse the repository at this point in the history
DQA-4077: Remove description check & avoid config info files
  • Loading branch information
jonhy81 authored May 31, 2022
2 parents ca9e1e9 + c88ab57 commit c443281
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions phpcs/QualityAssurance/Sniffs/InfoFiles/RequiredSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

/**
* \QualityAssurance\Sniffs\InfoFiles\RequiredSniff.
Expand Down Expand Up @@ -48,27 +50,28 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
// Only run this sniff once per info file.
if (preg_match('/\.info\.yml$/', $phpcsFile->getFilename()) === 1) {
// Drupal 8 style info.yml file.
$contents = file_get_contents($phpcsFile->getFilename());
try {
$info = \Symfony\Component\Yaml\Yaml::parse($contents);
} catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
// If the YAML is invalid we ignore this file.
return ($phpcsFile->numTokens + 1);
}
} else {
$filename = $phpcsFile->getFilename();
$fileExtension = strtolower(substr($filename, -9));
if ($fileExtension !== '.info.yml') {
return ($phpcsFile->numTokens + 1);
}

if (isset($info['name']) === false) {
$error = "The key 'name' is missing in the info file";
$phpcsFile->addError($error, $stackPtr, 'INFO');
// Exclude config files which might contain the info.yml extension.
$filenameWithoutExtension = substr($filename, 0, -9);
if (strpos($filenameWithoutExtension, '.') !== false) {
return ($phpcsFile->numTokens + 1);
}

if (isset($info['description']) === false) {
$error = "The key 'description' is missing in the info file";
$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);
}

if (isset($info['name']) === false) {
$error = "The key 'name' is missing in the info file";
$phpcsFile->addError($error, $stackPtr, 'INFO');
}

Expand Down

0 comments on commit c443281

Please sign in to comment.