Skip to content

Commit 9575535

Browse files
authored
feat(DependenciesArray): Ensure module dependencies are an array in .info.yml (#3166470)
1 parent 67b5756 commit 9575535

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* \Drupal\Sniffs\InfoFiles\DependenciesArraySniff.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
namespace Drupal\Sniffs\InfoFiles;
10+
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use Symfony\Component\Yaml\Yaml;
14+
use Symfony\Component\Yaml\Exception\ParseException;
15+
16+
/**
17+
* Dependencies should be an array in .info.yml files.
18+
*
19+
* @category PHP
20+
* @package PHP_CodeSniffer
21+
* @link http://pear.php.net/package/PHP_CodeSniffer
22+
*/
23+
class DependenciesArraySniff implements Sniff
24+
{
25+
26+
27+
/**
28+
* Returns an array of tokens this test wants to listen for.
29+
*
30+
* @return array<int|string>
31+
*/
32+
public function register()
33+
{
34+
return [T_INLINE_HTML];
35+
36+
}//end register()
37+
38+
39+
/**
40+
* Processes this test when one of its tokens is encountered.
41+
*
42+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
43+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
44+
*
45+
* @return int
46+
*/
47+
public function process(File $phpcsFile, $stackPtr)
48+
{
49+
// Only run this sniff on .info.yml files.
50+
if (strtolower(substr($phpcsFile->getFilename(), -9)) !== '.info.yml') {
51+
return ($phpcsFile->numTokens + 1);
52+
}
53+
54+
try {
55+
$info = Yaml::parse(file_get_contents($phpcsFile->getFilename()));
56+
} catch (ParseException $e) {
57+
// If the YAML is invalid we ignore this file.
58+
return ($phpcsFile->numTokens + 1);
59+
}
60+
61+
if (isset($info['dependencies']) === true && is_array($info['dependencies']) === false) {
62+
// The $stackPtr will always indicate line 1, but we can get the actual line by
63+
// searching $tokens to find the dependencies item.
64+
$tokens = $phpcsFile->getTokens();
65+
// $tokens cannot be empty at this point, but PHPStan 10.1.4 does not know this and gives the error
66+
// "Variable $key might not be defined". So initialise it here.
67+
$key = $stackPtr;
68+
foreach ($tokens as $key => $token) {
69+
if (preg_match('/dependencies\s*\:/', $token['content']) === 1) {
70+
break;
71+
}
72+
}
73+
74+
$error = '"dependencies" should be an array in the info yaml file';
75+
$phpcsFile->addError($error, $key, 'Dependencies');
76+
}
77+
78+
return ($phpcsFile->numTokens + 1);
79+
80+
}//end process()
81+
82+
83+
}//end class
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Drupal\Test\InfoFiles;
4+
5+
use Drupal\Test\CoderSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the DependenciesArray sniff.
9+
*/
10+
class DependenciesArrayUnitTest extends CoderSniffUnitTest
11+
{
12+
13+
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @param string $testFile The name of the file being tested.
21+
*
22+
* @return array<int, int>
23+
*/
24+
protected function getErrorList(string $testFile): array
25+
{
26+
return [3 => 1];
27+
28+
}//end getErrorList()
29+
30+
31+
/**
32+
* Returns the lines where warnings should occur.
33+
*
34+
* The key of the array should represent the line number and the value
35+
* should represent the number of warnings that should occur on that line.
36+
*
37+
* @param string $testFile The name of the file being tested.
38+
*
39+
* @return array<int, int>
40+
*/
41+
protected function getWarningList(string $testFile): array
42+
{
43+
return [];
44+
45+
}//end getWarningList()
46+
47+
48+
/**
49+
* Returns a list of test files that should be checked.
50+
*
51+
* @param string $testFileBase The base path that the unit tests files will have.
52+
*
53+
* @return array<string>
54+
*/
55+
protected function getTestFiles($testFileBase): array
56+
{
57+
return [
58+
__DIR__.'/drupal8/dependencies_array.info.yml',
59+
];
60+
61+
}//end getTestFiles()
62+
63+
64+
}//end class
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
core: 8.x
2+
name: Dependencies Array Test
3+
dependencies :
4+
drupal:taxonomy

0 commit comments

Comments
 (0)