Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@17
|
3 * \DrupalPractice\Sniffs\Constants\GlobalConstantSniff
|
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 DrupalPractice\Sniffs\Constants;
|
Chris@17
|
11
|
Chris@17
|
12 use DrupalPractice\Project;
|
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 * Checks that globally defined constants are not used in Drupal 8 and higher.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @category PHP
|
Chris@0
|
20 * @package PHP_CodeSniffer
|
Chris@0
|
21 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
22 */
|
Chris@17
|
23 class GlobalConstantSniff implements Sniff
|
Chris@0
|
24 {
|
Chris@0
|
25
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return array
|
Chris@0
|
31 */
|
Chris@0
|
32 public function register()
|
Chris@0
|
33 {
|
Chris@0
|
34 return array(T_CONST);
|
Chris@0
|
35
|
Chris@0
|
36 }//end register()
|
Chris@0
|
37
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
41 *
|
Chris@17
|
42 * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
|
Chris@17
|
43 * @param int $stackPtr The position of the current token
|
Chris@17
|
44 * in the stack passed in $tokens.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return void
|
Chris@0
|
47 */
|
Chris@17
|
48 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
49 {
|
Chris@0
|
50 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
51
|
Chris@0
|
52 // Only check constants in the global scope.
|
Chris@0
|
53 if (empty($tokens[$stackPtr]['conditions']) === false) {
|
Chris@0
|
54 return;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@17
|
57 $coreVersion = Project::getCoreVersion($phpcsFile);
|
Chris@0
|
58 if ($coreVersion !== '8.x') {
|
Chris@0
|
59 return;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 // Allow constants if they are deprecated.
|
Chris@0
|
63 $commentEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
Chris@0
|
64 if ($commentEnd !== null && $tokens[$commentEnd]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
|
Chris@0
|
65 // Go through all comment tags and check if one is @deprecated.
|
Chris@0
|
66 $commentTag = $commentEnd;
|
Chris@0
|
67 while ($commentTag !== null && $commentTag > $tokens[$commentEnd]['comment_opener']) {
|
Chris@0
|
68 if ($tokens[$commentTag]['content'] === '@deprecated') {
|
Chris@0
|
69 return;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 $commentTag = $phpcsFile->findPrevious(T_DOC_COMMENT_TAG, ($commentTag - 1), $tokens[$commentEnd]['comment_opener']);
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 $warning = 'Global constants should not be used, move it to a class or interface';
|
Chris@0
|
77 $phpcsFile->addWarning($warning, $stackPtr, 'GlobalConstant');
|
Chris@0
|
78
|
Chris@0
|
79 }//end process()
|
Chris@0
|
80
|
Chris@0
|
81
|
Chris@0
|
82 }//end class
|