comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9
10 namespace Drupal\Sniffs\Classes;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff as PSR2ClassDeclarationSniff;
14 use PHP_CodeSniffer\Util\Tokens;
9 15
10 /** 16 /**
11 * Class Declaration Test. 17 * Class Declaration Test.
12 * 18 *
13 * Checks the declaration of the class is correct. 19 * Checks the declaration of the class is correct.
14 * 20 *
15 * @category PHP 21 * @category PHP
16 * @package PHP_CodeSniffer 22 * @package PHP_CodeSniffer
17 * @link http://pear.php.net/package/PHP_CodeSniffer 23 * @link http://pear.php.net/package/PHP_CodeSniffer
18 */ 24 */
19 class Drupal_Sniffs_Classes_ClassDeclarationSniff extends PSR2_Sniffs_Classes_ClassDeclarationSniff 25 class ClassDeclarationSniff extends PSR2ClassDeclarationSniff
20 { 26 {
21 27
22 28
23 /** 29 /**
24 * Returns an array of tokens this test wants to listen for. 30 * Returns an array of tokens this test wants to listen for.
37 43
38 44
39 /** 45 /**
40 * Processes this test, when one of its tokens is encountered. 46 * Processes this test, when one of its tokens is encountered.
41 * 47 *
42 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 48 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
43 * @param integer $stackPtr The position of the current token in the 49 * @param integer $stackPtr The position of the current token in the
44 * stack passed in $tokens. 50 * stack passed in $tokens.
45 * 51 *
46 * @return void 52 * @return void
47 */ 53 */
48 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 54 public function process(File $phpcsFile, $stackPtr)
49 { 55 {
50 $tokens = $phpcsFile->getTokens(); 56 $tokens = $phpcsFile->getTokens();
51 $errorData = array(strtolower($tokens[$stackPtr]['content'])); 57 $errorData = array(strtolower($tokens[$stackPtr]['content']));
52 58
53 if (isset($tokens[$stackPtr]['scope_opener']) === false) { 59 if (isset($tokens[$stackPtr]['scope_opener']) === false) {
120 126
121 127
122 /** 128 /**
123 * Processes the closing section of a class declaration. 129 * Processes the closing section of a class declaration.
124 * 130 *
125 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 131 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
126 * @param int $stackPtr The position of the current token 132 * @param int $stackPtr The position of the current token
127 * in the stack passed in $tokens. 133 * in the stack passed in $tokens.
128 * 134 *
129 * @return void 135 * @return void
130 */ 136 */
131 public function processClose(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 137 public function processClose(File $phpcsFile, $stackPtr)
132 { 138 {
133 $tokens = $phpcsFile->getTokens(); 139 $tokens = $phpcsFile->getTokens();
134 140
135 // Just in case. 141 // Just in case.
136 if (isset($tokens[$stackPtr]['scope_closer']) === false) { 142 if (isset($tokens[$stackPtr]['scope_closer']) === false) {
141 $closeBrace = $tokens[$stackPtr]['scope_closer']; 147 $closeBrace = $tokens[$stackPtr]['scope_closer'];
142 $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true); 148 $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
143 if ($prevContent !== $tokens[$stackPtr]['scope_opener'] 149 if ($prevContent !== $tokens[$stackPtr]['scope_opener']
144 && $tokens[$prevContent]['line'] !== ($tokens[$closeBrace]['line'] - 2) 150 && $tokens[$prevContent]['line'] !== ($tokens[$closeBrace]['line'] - 2)
145 // If the class only contains a comment no extra line is needed. 151 // If the class only contains a comment no extra line is needed.
146 && isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$prevContent]['code']]) === false 152 && isset(Tokens::$commentTokens[$tokens[$prevContent]['code']]) === false
147 ) { 153 ) {
148 $error = 'The closing brace for the %s must have an empty line before it'; 154 $error = 'The closing brace for the %s must have an empty line before it';
149 $data = array($tokens[$stackPtr]['content']); 155 $data = array($tokens[$stackPtr]['content']);
150 $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data); 156 $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data);
151 157