Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_InfoFiles_RequiredSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * "version", "project" and "timestamp" are added automatically by drupal.org
|
Chris@0
|
12 * packaging scripts.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @category PHP
|
Chris@0
|
15 * @package PHP_CodeSniffer
|
Chris@0
|
16 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
17 */
|
Chris@0
|
18 class Drupal_Sniffs_InfoFiles_AutoAddedKeysSniff implements PHP_CodeSniffer_Sniff
|
Chris@0
|
19 {
|
Chris@0
|
20
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @return array
|
Chris@0
|
26 */
|
Chris@0
|
27 public function register()
|
Chris@0
|
28 {
|
Chris@0
|
29 return array(T_INLINE_HTML);
|
Chris@0
|
30
|
Chris@0
|
31 }//end register()
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
38 * @param int $stackPtr The position of the current token in the
|
Chris@0
|
39 * stack passed in $tokens.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return int
|
Chris@0
|
42 */
|
Chris@0
|
43 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
44 {
|
Chris@0
|
45 // Only run this sniff once per info file.
|
Chris@0
|
46 if (preg_match('/\.info$/', $phpcsFile->getFilename()) === 1) {
|
Chris@0
|
47 // Drupal 7 style info file.
|
Chris@0
|
48 $contents = file_get_contents($phpcsFile->getFilename());
|
Chris@0
|
49 $info = Drupal_Sniffs_InfoFiles_ClassFilesSniff::drupalParseInfoFormat($contents);
|
Chris@0
|
50 } else if (preg_match('/\.info\.yml$/', $phpcsFile->getFilename()) === 1) {
|
Chris@0
|
51 // Drupal 8 style info.yml file.
|
Chris@0
|
52 $contents = file_get_contents($phpcsFile->getFilename());
|
Chris@0
|
53 try {
|
Chris@0
|
54 $info = \Symfony\Component\Yaml\Yaml::parse($contents);
|
Chris@0
|
55 } catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
|
Chris@0
|
56 // If the YAML is invalid we ignore this file.
|
Chris@0
|
57 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
58 }
|
Chris@0
|
59 } else {
|
Chris@0
|
60 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 if (isset($info['project']) === true) {
|
Chris@0
|
64 $warning = 'Remove "project" from the info file, it will be added by drupal.org packaging automatically';
|
Chris@0
|
65 $phpcsFile->addWarning($warning, $stackPtr, 'Project');
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 if (isset($info['timestamp']) === true) {
|
Chris@0
|
69 $warning = 'Remove "timestamp" from the info file, it will be added by drupal.org packaging automatically';
|
Chris@0
|
70 $phpcsFile->addWarning($warning, $stackPtr, 'Timestamp');
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 // "version" is special: we want to allow it in core, but not anywhere else.
|
Chris@0
|
74 if (isset($info['version']) === true && strpos($phpcsFile->getFilename(), '/core/') === false) {
|
Chris@0
|
75 $warning = 'Remove "version" from the info file, it will be added by drupal.org packaging automatically';
|
Chris@0
|
76 $phpcsFile->addWarning($warning, $stackPtr, 'Version');
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
80
|
Chris@0
|
81 }//end process()
|
Chris@0
|
82
|
Chris@0
|
83
|
Chris@0
|
84 }//end class
|