|
| 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 |
0 commit comments