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