comparison vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/Constants/GlobalConstantSniff.php @ 0:4c8ae668cc8c

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