comparison vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/Constants/GlobalDefineSniff.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_GlobalDefineSniff
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Checks that global define() constants are not used in modules in Drupal 8.
12 *
13 * @category PHP
14 * @package PHP_CodeSniffer
15 * @link http://pear.php.net/package/PHP_CodeSniffer
16 */
17 class DrupalPractice_Sniffs_Constants_GlobalDefineSniff extends Drupal_Sniffs_Semantics_FunctionCall
18 {
19
20
21 /**
22 * Returns an array of function names this test wants to listen for.
23 *
24 * @return array
25 */
26 public function registerFunctionNames()
27 {
28 return array('define');
29
30 }//end registerFunctionNames()
31
32
33 /**
34 * Processes this function call.
35 *
36 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
37 * @param int $stackPtr The position of the function call in
38 * the stack.
39 * @param int $openBracket The position of the opening
40 * parenthesis in the stack.
41 * @param int $closeBracket The position of the closing
42 * parenthesis in the stack.
43 *
44 * @return void
45 */
46 public function processFunctionCall(
47 PHP_CodeSniffer_File $phpcsFile,
48 $stackPtr,
49 $openBracket,
50 $closeBracket
51 ) {
52 $tokens = $phpcsFile->getTokens();
53
54 // Only check constants in the global scope in module files.
55 if (empty($tokens[$stackPtr]['conditions']) === false || substr($phpcsFile->getFilename(), -7) !== '.module') {
56 return;
57 }
58
59 $coreVersion = DrupalPractice_Project::getCoreVersion($phpcsFile);
60 if ($coreVersion !== '8.x') {
61 return;
62 }
63
64 // Allow constants if they are deprecated.
65 $commentEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
66 if ($commentEnd !== null && $tokens[$commentEnd]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
67 // Go through all comment tags and check if one is @deprecated.
68 $commentTag = $commentEnd;
69 while ($commentTag !== null && $commentTag > $tokens[$commentEnd]['comment_opener']) {
70 if ($tokens[$commentTag]['content'] === '@deprecated') {
71 return;
72 }
73
74 $commentTag = $phpcsFile->findPrevious(T_DOC_COMMENT_TAG, ($commentTag - 1), $tokens[$commentEnd]['comment_opener']);
75 }
76 }
77
78 $warning = 'Global constants should not be used, move it to a class or interface';
79 $phpcsFile->addWarning($warning, $stackPtr, 'GlobalConstant');
80
81 }//end processFunctionCall()
82
83
84 }//end class