comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeIndentSniff.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
1 <?php 1 <?php
2 /** 2 /**
3 * Drupal_Sniffs_Whitespace_ScopeIndentSniff. 3 * \Drupal\Sniffs\WhiteSpace\ScopeIndentSniff.
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 9
10 namespace Drupal\Sniffs\WhiteSpace;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Sniffs\Sniff;
14 use PHP_CodeSniffer\Config;
15 use PHP_CodeSniffer\Util\Tokens;
16
10 /** 17 /**
11 * Largely copied from Generic_Sniffs_Whitespace_ScopeIndentSniff, modified to make 18 * Largely copied from
12 * the exact mode working with comments and multi line statements. 19 * \PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff,
20 * modified to make the exact mode working with comments and multi line
21 * statements.
13 * 22 *
14 * Checks that control structures are structured correctly, and their content 23 * Checks that control structures are structured correctly, and their content
15 * is indented correctly. This sniff will throw errors if tabs are used 24 * is indented correctly. This sniff will throw errors if tabs are used
16 * for indentation rather than spaces. 25 * for indentation rather than spaces.
17 * 26 *
18 * @category PHP 27 * @category PHP
19 * @package PHP_CodeSniffer 28 * @package PHP_CodeSniffer
20 * @link http://pear.php.net/package/PHP_CodeSniffer 29 * @link http://pear.php.net/package/PHP_CodeSniffer
21 */ 30 */
22 class Drupal_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff 31 class ScopeIndentSniff implements Sniff
23 { 32 {
24 33
25 /** 34 /**
26 * A list of tokenizers this sniff supports. 35 * A list of tokenizers this sniff supports.
27 * 36 *
118 127
119 128
120 /** 129 /**
121 * Processes this test, when one of its tokens is encountered. 130 * Processes this test, when one of its tokens is encountered.
122 * 131 *
123 * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. 132 * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
124 * @param int $stackPtr The position of the current token 133 * @param int $stackPtr The position of the current token
125 * in the stack passed in $tokens. 134 * in the stack passed in $tokens.
126 * 135 *
127 * @return void 136 * @return void
128 */ 137 */
129 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 138 public function process(File $phpcsFile, $stackPtr)
130 { 139 {
131 $debug = PHP_CodeSniffer::getConfigData('scope_indent_debug'); 140 $debug = Config::getConfigData('scope_indent_debug');
132 if ($debug !== null) { 141 if ($debug !== null) {
133 $this->_debug = (bool) $debug; 142 $this->_debug = (bool) $debug;
134 } 143 }
135 144
136 if ($this->_tabWidth === null) { 145 if ($this->_tabWidth === null) {
137 $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); 146 $config = $phpcsFile->config;
138 if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { 147 if (isset($config->tabWidth) === false || $config->tabWidth === 0) {
139 // We have no idea how wide tabs are, so assume 4 spaces for fixing. 148 // We have no idea how wide tabs are, so assume 4 spaces for fixing.
140 // It shouldn't really matter because indent checks elsewhere in the 149 // It shouldn't really matter because indent checks elsewhere in the
141 // standard should fix things up. 150 // standard should fix things up.
142 $this->_tabWidth = 4; 151 $this->_tabWidth = 4;
143 } else { 152 } else {
144 $this->_tabWidth = $cliValues['tabWidth']; 153 $this->_tabWidth = $config->tabWidth;
145 } 154 }
146 } 155 }
147 156
148 $currentIndent = 0; 157 $currentIndent = 0;
149 $lastOpenTag = $stackPtr; 158 $lastOpenTag = $stackPtr;
716 echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; 725 echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL;
717 } 726 }
718 }//end if 727 }//end if
719 728
720 if ($checkToken !== null 729 if ($checkToken !== null
721 && isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true 730 && isset(Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true
722 && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false 731 && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false
723 && isset($tokens[$checkToken]['scope_opener']) === true 732 && isset($tokens[$checkToken]['scope_opener']) === true
724 ) { 733 ) {
725 $exact = true; 734 $exact = true;
726 735
766 }//end if 775 }//end if
767 776
768 // Method prefix indentation has to be exact or else if will break 777 // Method prefix indentation has to be exact or else if will break
769 // the rest of the function declaration, and potentially future ones. 778 // the rest of the function declaration, and potentially future ones.
770 if ($checkToken !== null 779 if ($checkToken !== null
771 && isset(PHP_CodeSniffer_Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true 780 && isset(Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true
772 && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON 781 && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON
773 ) { 782 ) {
774 $exact = true; 783 $exact = true;
775 } 784 }
776 785
818 && (($tokenIndent !== $checkIndent && $exact === true) 827 && (($tokenIndent !== $checkIndent && $exact === true)
819 || ($tokenIndent < $checkIndent && $exact === false)) 828 || ($tokenIndent < $checkIndent && $exact === false))
820 ) { 829 ) {
821 if ($tokenIndent > $checkIndent) { 830 if ($tokenIndent > $checkIndent) {
822 // Ignore multi line statements. 831 // Ignore multi line statements.
823 $before = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($checkToken - 1), null, true); 832 $before = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($checkToken - 1), null, true);
824 if ($before !== false && in_array( 833 if ($before !== false && in_array(
825 $tokens[$before]['code'], 834 $tokens[$before]['code'],
826 array( 835 array(
827 T_SEMICOLON, 836 T_SEMICOLON,
828 T_CLOSE_CURLY_BRACKET, 837 T_CLOSE_CURLY_BRACKET,
1063 $i = $closer; 1072 $i = $closer;
1064 continue; 1073 continue;
1065 } 1074 }
1066 1075
1067 $condition = $tokens[$tokens[$i]['scope_condition']]['code']; 1076 $condition = $tokens[$tokens[$i]['scope_condition']]['code'];
1068 if (isset(PHP_CodeSniffer_Tokens::$scopeOpeners[$condition]) === true 1077 if (isset(Tokens::$scopeOpeners[$condition]) === true
1069 && in_array($condition, $this->nonIndentingScopes) === false 1078 && in_array($condition, $this->nonIndentingScopes) === false
1070 ) { 1079 ) {
1071 if ($this->_debug === true) { 1080 if ($this->_debug === true) {
1072 $line = $tokens[$i]['line']; 1081 $line = $tokens[$i]['line'];
1073 $type = $tokens[$tokens[$i]['scope_condition']]['type']; 1082 $type = $tokens[$tokens[$i]['scope_condition']]['type'];
1184 if ($parens > $object && $parens > $condition) { 1193 if ($parens > $object && $parens > $condition) {
1185 if ($this->_debug === true) { 1194 if ($this->_debug === true) {
1186 echo "\t* using parenthesis *".PHP_EOL; 1195 echo "\t* using parenthesis *".PHP_EOL;
1187 } 1196 }
1188 1197
1189 $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($parens - 1), null, true); 1198 $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parens - 1), null, true);
1190 $object = 0; 1199 $object = 0;
1191 $condition = 0; 1200 $condition = 0;
1192 } else if ($object > 0 && $object >= $condition) { 1201 } else if ($object > 0 && $object >= $condition) {
1193 if ($this->_debug === true) { 1202 if ($this->_debug === true) {
1194 echo "\t* using object *".PHP_EOL; 1203 echo "\t* using object *".PHP_EOL;