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