@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | /** |
| 103 | 103 | * Returns an array of tokens this test wants to listen for. |
| 104 | 104 | * |
| 105 | - * @return array |
|
| 105 | + * @return integer[] |
|
| 106 | 106 | */ |
| 107 | 107 | public function register() |
| 108 | 108 | { |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * @param int $stackPtr The position of the current token |
| 123 | 123 | * in the stack passed in $tokens. |
| 124 | 124 | * |
| 125 | - * @return void |
|
| 125 | + * @return integer |
|
| 126 | 126 | */ |
| 127 | 127 | public function process(File $phpcsFile, $stackPtr) |
| 128 | 128 | { |
@@ -17,904 +17,904 @@ discard block |
||
| 17 | 17 | class ScopeIndentSniff implements Sniff |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * A list of tokenizers this sniff supports. |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $supportedTokenizers = [ |
|
| 26 | - 'PHP', |
|
| 27 | - 'JS', |
|
| 28 | - ]; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The number of spaces code should be indented. |
|
| 32 | - * |
|
| 33 | - * @var integer |
|
| 34 | - */ |
|
| 35 | - public $indent = 4; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Does the indent need to be exactly right? |
|
| 39 | - * |
|
| 40 | - * If TRUE, indent needs to be exactly $indent spaces. If FALSE, |
|
| 41 | - * indent needs to be at least $indent spaces (but can be more). |
|
| 42 | - * |
|
| 43 | - * @var boolean |
|
| 44 | - */ |
|
| 45 | - public $exact = false; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Should tabs be used for indenting? |
|
| 49 | - * |
|
| 50 | - * If TRUE, fixes will be made using tabs instead of spaces. |
|
| 51 | - * The size of each tab is important, so it should be specified |
|
| 52 | - * using the --tab-width CLI argument. |
|
| 53 | - * |
|
| 54 | - * @var boolean |
|
| 55 | - */ |
|
| 56 | - public $tabIndent = false; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The --tab-width CLI value that is being used. |
|
| 60 | - * |
|
| 61 | - * @var integer |
|
| 62 | - */ |
|
| 63 | - private $tabWidth = null; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * List of tokens not needing to be checked for indentation. |
|
| 67 | - * |
|
| 68 | - * Useful to allow Sniffs based on this to easily ignore/skip some |
|
| 69 | - * tokens from verification. For example, inline HTML sections |
|
| 70 | - * or PHP open/close tags can escape from here and have their own |
|
| 71 | - * rules elsewhere. |
|
| 72 | - * |
|
| 73 | - * @var int[] |
|
| 74 | - */ |
|
| 75 | - public $ignoreIndentationTokens = []; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * List of tokens not needing to be checked for indentation. |
|
| 79 | - * |
|
| 80 | - * This is a cached copy of the public version of this var, which |
|
| 81 | - * can be set in a ruleset file, and some core ignored tokens. |
|
| 82 | - * |
|
| 83 | - * @var int[] |
|
| 84 | - */ |
|
| 85 | - private $ignoreIndentation = []; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Any scope openers that should not cause an indent. |
|
| 89 | - * |
|
| 90 | - * @var int[] |
|
| 91 | - */ |
|
| 92 | - protected $nonIndentingScopes = []; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Show debug output for this sniff. |
|
| 96 | - * |
|
| 97 | - * @var boolean |
|
| 98 | - */ |
|
| 99 | - private $debug = false; |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Returns an array of tokens this test wants to listen for. |
|
| 104 | - * |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - public function register() |
|
| 108 | - { |
|
| 109 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 110 | - $this->debug = false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return [T_OPEN_TAG]; |
|
| 114 | - |
|
| 115 | - }//end register() |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Processes this test, when one of its tokens is encountered. |
|
| 120 | - * |
|
| 121 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document. |
|
| 122 | - * @param int $stackPtr The position of the current token |
|
| 123 | - * in the stack passed in $tokens. |
|
| 124 | - * |
|
| 125 | - * @return void |
|
| 126 | - */ |
|
| 127 | - public function process(File $phpcsFile, $stackPtr) |
|
| 128 | - { |
|
| 129 | - $debug = Config::getConfigData('scope_indent_debug'); |
|
| 130 | - if ($debug !== null) { |
|
| 131 | - $this->debug = (bool) $debug; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($this->tabWidth === null) { |
|
| 135 | - if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) { |
|
| 136 | - // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 137 | - // It shouldn't really matter because indent checks elsewhere in the |
|
| 138 | - // standard should fix things up. |
|
| 139 | - $this->tabWidth = 4; |
|
| 140 | - } else { |
|
| 141 | - $this->tabWidth = $phpcsFile->config->tabWidth; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $lastOpenTag = $stackPtr; |
|
| 146 | - $lastCloseTag = null; |
|
| 147 | - $openScopes = []; |
|
| 148 | - $adjustments = []; |
|
| 149 | - $setIndents = []; |
|
| 150 | - $disableExactEnd = 0; |
|
| 151 | - |
|
| 152 | - $tokens = $phpcsFile->getTokens(); |
|
| 153 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 154 | - $trimmed = ltrim($tokens[$first]['content']); |
|
| 155 | - if ($trimmed === '') { |
|
| 156 | - $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 157 | - } else { |
|
| 158 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ($this->debug === true) { |
|
| 162 | - $line = $tokens[$stackPtr]['line']; |
|
| 163 | - echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if (empty($this->ignoreIndentation) === true) { |
|
| 167 | - $this->ignoreIndentation = [T_INLINE_HTML => true]; |
|
| 168 | - foreach ($this->ignoreIndentationTokens as $token) { |
|
| 169 | - if (is_int($token) === false) { |
|
| 170 | - if (defined($token) === false) { |
|
| 171 | - continue; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $token = constant($token); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $this->ignoreIndentation[$token] = true; |
|
| 178 | - } |
|
| 179 | - }//end if |
|
| 180 | - |
|
| 181 | - $this->exact = (bool) $this->exact; |
|
| 182 | - $this->tabIndent = (bool) $this->tabIndent; |
|
| 183 | - |
|
| 184 | - $checkAnnotations = $phpcsFile->config->annotations; |
|
| 185 | - |
|
| 186 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 187 | - if ($i === false) { |
|
| 188 | - // Something has gone very wrong; maybe a parse error. |
|
| 189 | - break; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if ($checkAnnotations === true |
|
| 193 | - && $tokens[$i]['code'] === T_PHPCS_SET |
|
| 194 | - && isset($tokens[$i]['sniffCode']) === true |
|
| 195 | - && $tokens[$i]['sniffCode'] === 'Generic.WhiteSpace.ScopeIndent' |
|
| 196 | - && $tokens[$i]['sniffProperty'] === 'exact' |
|
| 197 | - ) { |
|
| 198 | - $value = $tokens[$i]['sniffPropertyValue']; |
|
| 199 | - if ($value === 'true') { |
|
| 200 | - $value = true; |
|
| 201 | - } else if ($value === 'false') { |
|
| 202 | - $value = false; |
|
| 203 | - } else { |
|
| 204 | - $value = (bool) $value; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $this->exact = $value; |
|
| 208 | - |
|
| 209 | - if ($this->debug === true) { |
|
| 210 | - $line = $tokens[$i]['line']; |
|
| 211 | - if ($this->exact === true) { |
|
| 212 | - $value = 'true'; |
|
| 213 | - } else { |
|
| 214 | - $value = 'false'; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - echo "* token $i on line $line set exact flag to $value *".PHP_EOL; |
|
| 218 | - } |
|
| 219 | - }//end if |
|
| 220 | - |
|
| 221 | - $checkToken = null; |
|
| 222 | - $checkIndent = null; |
|
| 223 | - |
|
| 224 | - /* |
|
| 20 | + /** |
|
| 21 | + * A list of tokenizers this sniff supports. |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $supportedTokenizers = [ |
|
| 26 | + 'PHP', |
|
| 27 | + 'JS', |
|
| 28 | + ]; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The number of spaces code should be indented. |
|
| 32 | + * |
|
| 33 | + * @var integer |
|
| 34 | + */ |
|
| 35 | + public $indent = 4; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Does the indent need to be exactly right? |
|
| 39 | + * |
|
| 40 | + * If TRUE, indent needs to be exactly $indent spaces. If FALSE, |
|
| 41 | + * indent needs to be at least $indent spaces (but can be more). |
|
| 42 | + * |
|
| 43 | + * @var boolean |
|
| 44 | + */ |
|
| 45 | + public $exact = false; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Should tabs be used for indenting? |
|
| 49 | + * |
|
| 50 | + * If TRUE, fixes will be made using tabs instead of spaces. |
|
| 51 | + * The size of each tab is important, so it should be specified |
|
| 52 | + * using the --tab-width CLI argument. |
|
| 53 | + * |
|
| 54 | + * @var boolean |
|
| 55 | + */ |
|
| 56 | + public $tabIndent = false; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The --tab-width CLI value that is being used. |
|
| 60 | + * |
|
| 61 | + * @var integer |
|
| 62 | + */ |
|
| 63 | + private $tabWidth = null; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * List of tokens not needing to be checked for indentation. |
|
| 67 | + * |
|
| 68 | + * Useful to allow Sniffs based on this to easily ignore/skip some |
|
| 69 | + * tokens from verification. For example, inline HTML sections |
|
| 70 | + * or PHP open/close tags can escape from here and have their own |
|
| 71 | + * rules elsewhere. |
|
| 72 | + * |
|
| 73 | + * @var int[] |
|
| 74 | + */ |
|
| 75 | + public $ignoreIndentationTokens = []; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * List of tokens not needing to be checked for indentation. |
|
| 79 | + * |
|
| 80 | + * This is a cached copy of the public version of this var, which |
|
| 81 | + * can be set in a ruleset file, and some core ignored tokens. |
|
| 82 | + * |
|
| 83 | + * @var int[] |
|
| 84 | + */ |
|
| 85 | + private $ignoreIndentation = []; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Any scope openers that should not cause an indent. |
|
| 89 | + * |
|
| 90 | + * @var int[] |
|
| 91 | + */ |
|
| 92 | + protected $nonIndentingScopes = []; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Show debug output for this sniff. |
|
| 96 | + * |
|
| 97 | + * @var boolean |
|
| 98 | + */ |
|
| 99 | + private $debug = false; |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Returns an array of tokens this test wants to listen for. |
|
| 104 | + * |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + public function register() |
|
| 108 | + { |
|
| 109 | + if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 110 | + $this->debug = false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return [T_OPEN_TAG]; |
|
| 114 | + |
|
| 115 | + }//end register() |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Processes this test, when one of its tokens is encountered. |
|
| 120 | + * |
|
| 121 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document. |
|
| 122 | + * @param int $stackPtr The position of the current token |
|
| 123 | + * in the stack passed in $tokens. |
|
| 124 | + * |
|
| 125 | + * @return void |
|
| 126 | + */ |
|
| 127 | + public function process(File $phpcsFile, $stackPtr) |
|
| 128 | + { |
|
| 129 | + $debug = Config::getConfigData('scope_indent_debug'); |
|
| 130 | + if ($debug !== null) { |
|
| 131 | + $this->debug = (bool) $debug; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($this->tabWidth === null) { |
|
| 135 | + if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) { |
|
| 136 | + // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
|
| 137 | + // It shouldn't really matter because indent checks elsewhere in the |
|
| 138 | + // standard should fix things up. |
|
| 139 | + $this->tabWidth = 4; |
|
| 140 | + } else { |
|
| 141 | + $this->tabWidth = $phpcsFile->config->tabWidth; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $lastOpenTag = $stackPtr; |
|
| 146 | + $lastCloseTag = null; |
|
| 147 | + $openScopes = []; |
|
| 148 | + $adjustments = []; |
|
| 149 | + $setIndents = []; |
|
| 150 | + $disableExactEnd = 0; |
|
| 151 | + |
|
| 152 | + $tokens = $phpcsFile->getTokens(); |
|
| 153 | + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 154 | + $trimmed = ltrim($tokens[$first]['content']); |
|
| 155 | + if ($trimmed === '') { |
|
| 156 | + $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 157 | + } else { |
|
| 158 | + $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ($this->debug === true) { |
|
| 162 | + $line = $tokens[$stackPtr]['line']; |
|
| 163 | + echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if (empty($this->ignoreIndentation) === true) { |
|
| 167 | + $this->ignoreIndentation = [T_INLINE_HTML => true]; |
|
| 168 | + foreach ($this->ignoreIndentationTokens as $token) { |
|
| 169 | + if (is_int($token) === false) { |
|
| 170 | + if (defined($token) === false) { |
|
| 171 | + continue; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $token = constant($token); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $this->ignoreIndentation[$token] = true; |
|
| 178 | + } |
|
| 179 | + }//end if |
|
| 180 | + |
|
| 181 | + $this->exact = (bool) $this->exact; |
|
| 182 | + $this->tabIndent = (bool) $this->tabIndent; |
|
| 183 | + |
|
| 184 | + $checkAnnotations = $phpcsFile->config->annotations; |
|
| 185 | + |
|
| 186 | + for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 187 | + if ($i === false) { |
|
| 188 | + // Something has gone very wrong; maybe a parse error. |
|
| 189 | + break; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if ($checkAnnotations === true |
|
| 193 | + && $tokens[$i]['code'] === T_PHPCS_SET |
|
| 194 | + && isset($tokens[$i]['sniffCode']) === true |
|
| 195 | + && $tokens[$i]['sniffCode'] === 'Generic.WhiteSpace.ScopeIndent' |
|
| 196 | + && $tokens[$i]['sniffProperty'] === 'exact' |
|
| 197 | + ) { |
|
| 198 | + $value = $tokens[$i]['sniffPropertyValue']; |
|
| 199 | + if ($value === 'true') { |
|
| 200 | + $value = true; |
|
| 201 | + } else if ($value === 'false') { |
|
| 202 | + $value = false; |
|
| 203 | + } else { |
|
| 204 | + $value = (bool) $value; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $this->exact = $value; |
|
| 208 | + |
|
| 209 | + if ($this->debug === true) { |
|
| 210 | + $line = $tokens[$i]['line']; |
|
| 211 | + if ($this->exact === true) { |
|
| 212 | + $value = 'true'; |
|
| 213 | + } else { |
|
| 214 | + $value = 'false'; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + echo "* token $i on line $line set exact flag to $value *".PHP_EOL; |
|
| 218 | + } |
|
| 219 | + }//end if |
|
| 220 | + |
|
| 221 | + $checkToken = null; |
|
| 222 | + $checkIndent = null; |
|
| 223 | + |
|
| 224 | + /* |
|
| 225 | 225 | Don't check indents exactly between parenthesis or arrays as they |
| 226 | 226 | tend to have custom rules, such as with multi-line function calls |
| 227 | 227 | and control structure conditions. |
| 228 | 228 | */ |
| 229 | 229 | |
| 230 | - $exact = $this->exact; |
|
| 231 | - |
|
| 232 | - if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 233 | - $disableExactEnd = max($disableExactEnd, $tokens[$i]['bracket_closer']); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS |
|
| 237 | - && isset($tokens[$i]['parenthesis_closer']) === true |
|
| 238 | - ) { |
|
| 239 | - $disableExactEnd = max($disableExactEnd, $tokens[$i]['parenthesis_closer']); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if ($exact === true && $i < $disableExactEnd) { |
|
| 243 | - $exact = false; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Detect line changes and figure out where the indent is. |
|
| 247 | - if ($tokens[$i]['column'] === 1) { |
|
| 248 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 249 | - if ($trimmed === '') { |
|
| 250 | - if (isset($tokens[($i + 1)]) === true |
|
| 251 | - && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 252 | - ) { |
|
| 253 | - $checkToken = ($i + 1); |
|
| 254 | - $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 255 | - } |
|
| 256 | - } else { |
|
| 257 | - $checkToken = $i; |
|
| 258 | - $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // Closing parenthesis should just be indented to at least |
|
| 263 | - // the same level as where they were opened (but can be more). |
|
| 264 | - if (($checkToken !== null |
|
| 265 | - && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 266 | - && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 267 | - || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 268 | - && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 269 | - ) { |
|
| 270 | - if ($checkToken !== null) { |
|
| 271 | - $parenCloser = $checkToken; |
|
| 272 | - } else { |
|
| 273 | - $parenCloser = $i; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if ($this->debug === true) { |
|
| 277 | - $line = $tokens[$i]['line']; |
|
| 278 | - echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 282 | - if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 283 | - $parens = 0; |
|
| 284 | - if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 285 | - && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 286 | - ) { |
|
| 287 | - $parens = $tokens[$parenCloser]['nested_parenthesis']; |
|
| 288 | - end($parens); |
|
| 289 | - $parens = key($parens); |
|
| 290 | - if ($this->debug === true) { |
|
| 291 | - $line = $tokens[$parens]['line']; |
|
| 292 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $condition = 0; |
|
| 297 | - if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 298 | - && empty($tokens[$parenCloser]['conditions']) === false |
|
| 299 | - && (isset($tokens[$parenCloser]['parenthesis_owner']) === false |
|
| 300 | - || $parens > 0) |
|
| 301 | - ) { |
|
| 302 | - $condition = $tokens[$parenCloser]['conditions']; |
|
| 303 | - end($condition); |
|
| 304 | - $condition = key($condition); |
|
| 305 | - if ($this->debug === true) { |
|
| 306 | - $line = $tokens[$condition]['line']; |
|
| 307 | - $type = $tokens[$condition]['type']; |
|
| 308 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - if ($parens > $condition) { |
|
| 313 | - if ($this->debug === true) { |
|
| 314 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $parenOpener = $parens; |
|
| 318 | - $condition = 0; |
|
| 319 | - } else if ($condition > 0) { |
|
| 320 | - if ($this->debug === true) { |
|
| 321 | - echo "\t* using condition *".PHP_EOL; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $parenOpener = $condition; |
|
| 325 | - $parens = 0; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - $exact = false; |
|
| 329 | - |
|
| 330 | - $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 331 | - $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 332 | - |
|
| 333 | - if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 334 | - if ($this->debug === true) { |
|
| 335 | - echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 339 | - if (isset($adjustments[$condition]) === true) { |
|
| 340 | - $checkIndent += $adjustments[$condition]; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - $currentIndent = $checkIndent; |
|
| 344 | - |
|
| 345 | - if ($this->debug === true) { |
|
| 346 | - $type = $tokens[$lastOpenTag]['type']; |
|
| 347 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 348 | - } |
|
| 349 | - } else if ($condition > 0 |
|
| 350 | - && isset($tokens[$condition]['scope_opener']) === true |
|
| 351 | - && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 352 | - ) { |
|
| 353 | - $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 354 | - if (isset($adjustments[$condition]) === true) { |
|
| 355 | - $checkIndent += $adjustments[$condition]; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $currentIndent = $checkIndent; |
|
| 359 | - |
|
| 360 | - if ($this->debug === true) { |
|
| 361 | - $type = $tokens[$condition]['type']; |
|
| 362 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 363 | - } |
|
| 364 | - } else { |
|
| 365 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 366 | - |
|
| 367 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 368 | - if (isset($adjustments[$first]) === true) { |
|
| 369 | - $checkIndent += $adjustments[$first]; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - if ($this->debug === true) { |
|
| 373 | - $line = $tokens[$first]['line']; |
|
| 374 | - $type = $tokens[$first]['type']; |
|
| 375 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if ($first === $tokens[$parenCloser]['parenthesis_opener'] |
|
| 379 | - && $tokens[($first - 1)]['line'] === $tokens[$first]['line'] |
|
| 380 | - ) { |
|
| 381 | - // This is unlikely to be the start of the statement, so look |
|
| 382 | - // back further to find it. |
|
| 383 | - $first--; |
|
| 384 | - if ($this->debug === true) { |
|
| 385 | - $line = $tokens[$first]['line']; |
|
| 386 | - $type = $tokens[$first]['type']; |
|
| 387 | - echo "\t* first token is the parenthesis opener *".PHP_EOL; |
|
| 388 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 393 | - if ($prev !== $first) { |
|
| 394 | - // This is not the start of the statement. |
|
| 395 | - if ($this->debug === true) { |
|
| 396 | - $line = $tokens[$prev]['line']; |
|
| 397 | - $type = $tokens[$prev]['type']; |
|
| 398 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 402 | - if ($first !== false) { |
|
| 403 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 404 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 405 | - } else { |
|
| 406 | - $first = $prev; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - if ($this->debug === true) { |
|
| 410 | - $line = $tokens[$first]['line']; |
|
| 411 | - $type = $tokens[$first]['type']; |
|
| 412 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 413 | - } |
|
| 414 | - }//end if |
|
| 415 | - |
|
| 416 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 417 | - && $tokens[$first]['scope_closer'] === $first |
|
| 418 | - ) { |
|
| 419 | - if ($this->debug === true) { |
|
| 420 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 424 | - $scopeCloser = $first; |
|
| 425 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 426 | - |
|
| 427 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 428 | - if (isset($adjustments[$first]) === true) { |
|
| 429 | - $currentIndent += $adjustments[$first]; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // Make sure it is divisible by our expected indent. |
|
| 433 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 434 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $setIndents[$first] = $currentIndent; |
|
| 438 | - |
|
| 439 | - if ($this->debug === true) { |
|
| 440 | - $type = $tokens[$first]['type']; |
|
| 441 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 442 | - } |
|
| 443 | - }//end if |
|
| 444 | - } else { |
|
| 445 | - // Don't force current indent to be divisible because there could be custom |
|
| 446 | - // rules in place between parenthesis, such as with arrays. |
|
| 447 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 448 | - if (isset($adjustments[$first]) === true) { |
|
| 449 | - $currentIndent += $adjustments[$first]; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - $setIndents[$first] = $currentIndent; |
|
| 453 | - |
|
| 454 | - if ($this->debug === true) { |
|
| 455 | - $type = $tokens[$first]['type']; |
|
| 456 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 457 | - } |
|
| 458 | - }//end if |
|
| 459 | - }//end if |
|
| 460 | - } else if ($this->debug === true) { |
|
| 461 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 462 | - }//end if |
|
| 463 | - }//end if |
|
| 464 | - |
|
| 465 | - // Closing short array bracket should just be indented to at least |
|
| 466 | - // the same level as where it was opened (but can be more). |
|
| 467 | - if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 468 | - || ($checkToken !== null |
|
| 469 | - && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 470 | - ) { |
|
| 471 | - if ($checkToken !== null) { |
|
| 472 | - $arrayCloser = $checkToken; |
|
| 473 | - } else { |
|
| 474 | - $arrayCloser = $i; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - if ($this->debug === true) { |
|
| 478 | - $line = $tokens[$arrayCloser]['line']; |
|
| 479 | - echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 483 | - if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 484 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 485 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 486 | - if (isset($adjustments[$first]) === true) { |
|
| 487 | - $checkIndent += $adjustments[$first]; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - $exact = false; |
|
| 491 | - |
|
| 492 | - if ($this->debug === true) { |
|
| 493 | - $line = $tokens[$first]['line']; |
|
| 494 | - $type = $tokens[$first]['type']; |
|
| 495 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 499 | - // This is unlikely to be the start of the statement, so look |
|
| 500 | - // back further to find it. |
|
| 501 | - $first--; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 505 | - if ($prev !== $first) { |
|
| 506 | - // This is not the start of the statement. |
|
| 507 | - if ($this->debug === true) { |
|
| 508 | - $line = $tokens[$prev]['line']; |
|
| 509 | - $type = $tokens[$prev]['type']; |
|
| 510 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 514 | - $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 515 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 516 | - if ($this->debug === true) { |
|
| 517 | - $line = $tokens[$first]['line']; |
|
| 518 | - $type = $tokens[$first]['type']; |
|
| 519 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 520 | - } |
|
| 521 | - } else if ($tokens[$first]['code'] === T_WHITESPACE) { |
|
| 522 | - $first = $phpcsFile->findNext(T_WHITESPACE, ($first + 1), null, true); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 526 | - && $tokens[$first]['scope_closer'] === $first |
|
| 527 | - ) { |
|
| 528 | - // The first token is a scope closer and would have already |
|
| 529 | - // been processed and set the indent level correctly, so |
|
| 530 | - // don't adjust it again. |
|
| 531 | - if ($this->debug === true) { |
|
| 532 | - echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - if (isset($setIndents[$first]) === true) { |
|
| 536 | - $currentIndent = $setIndents[$first]; |
|
| 537 | - if ($this->debug === true) { |
|
| 538 | - echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - } else { |
|
| 542 | - // Don't force current indent to be divisible because there could be custom |
|
| 543 | - // rules in place for arrays. |
|
| 544 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 545 | - if (isset($adjustments[$first]) === true) { |
|
| 546 | - $currentIndent += $adjustments[$first]; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $setIndents[$first] = $currentIndent; |
|
| 550 | - |
|
| 551 | - if ($this->debug === true) { |
|
| 552 | - $type = $tokens[$first]['type']; |
|
| 553 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 554 | - } |
|
| 555 | - }//end if |
|
| 556 | - } else if ($this->debug === true) { |
|
| 557 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 558 | - }//end if |
|
| 559 | - }//end if |
|
| 560 | - |
|
| 561 | - // Adjust lines within scopes while auto-fixing. |
|
| 562 | - if ($checkToken !== null |
|
| 563 | - && $exact === false |
|
| 564 | - && (empty($tokens[$checkToken]['conditions']) === false |
|
| 565 | - || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 566 | - && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 567 | - ) { |
|
| 568 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 569 | - $condition = $tokens[$checkToken]['conditions']; |
|
| 570 | - end($condition); |
|
| 571 | - $condition = key($condition); |
|
| 572 | - } else { |
|
| 573 | - $condition = $tokens[$checkToken]['scope_condition']; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 577 | - |
|
| 578 | - if (isset($adjustments[$first]) === true |
|
| 579 | - && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 580 | - || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 581 | - ) { |
|
| 582 | - $length = ($tokenIndent + $adjustments[$first]); |
|
| 583 | - |
|
| 584 | - // When fixing, we're going to adjust the indent of this line |
|
| 585 | - // here automatically, so use this new padding value when |
|
| 586 | - // comparing the expected padding to the actual padding. |
|
| 587 | - if ($phpcsFile->fixer->enabled === true) { |
|
| 588 | - $tokenIndent = $length; |
|
| 589 | - $this->adjustIndent($phpcsFile, $checkToken, $length, $adjustments[$first]); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - if ($this->debug === true) { |
|
| 593 | - $line = $tokens[$checkToken]['line']; |
|
| 594 | - $type = $tokens[$checkToken]['type']; |
|
| 595 | - echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - $adjustments[$checkToken] = $adjustments[$first]; |
|
| 599 | - |
|
| 600 | - if ($this->debug === true) { |
|
| 601 | - $line = $tokens[$checkToken]['line']; |
|
| 602 | - $type = $tokens[$checkToken]['type']; |
|
| 603 | - echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 604 | - } |
|
| 605 | - }//end if |
|
| 606 | - }//end if |
|
| 607 | - |
|
| 608 | - // Scope closers reset the required indent to the same level as the opening condition. |
|
| 609 | - if (($checkToken !== null |
|
| 610 | - && isset($openScopes[$checkToken]) === true |
|
| 611 | - || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 612 | - && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 613 | - && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 614 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 615 | - || ($checkToken === null |
|
| 616 | - && isset($openScopes[$i]) === true |
|
| 617 | - || (isset($tokens[$i]['scope_condition']) === true |
|
| 618 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 619 | - && $tokens[$i]['scope_closer'] === $i |
|
| 620 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 621 | - ) { |
|
| 622 | - if ($this->debug === true) { |
|
| 623 | - if ($checkToken === null) { |
|
| 624 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 625 | - $line = $tokens[$i]['line']; |
|
| 626 | - } else { |
|
| 627 | - $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 628 | - $line = $tokens[$checkToken]['line']; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - $scopeCloser = $checkToken; |
|
| 635 | - if ($scopeCloser === null) { |
|
| 636 | - $scopeCloser = $i; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - $conditionToken = array_pop($openScopes); |
|
| 640 | - if ($this->debug === true) { |
|
| 641 | - $line = $tokens[$conditionToken]['line']; |
|
| 642 | - $type = $tokens[$conditionToken]['type']; |
|
| 643 | - echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 647 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $tokens[$scopeCloser]['scope_condition'], true); |
|
| 648 | - if ($this->debug === true) { |
|
| 649 | - $line = $tokens[$first]['line']; |
|
| 650 | - $type = $tokens[$first]['type']; |
|
| 651 | - echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 655 | - && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 656 | - ) { |
|
| 657 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 658 | - if ($this->debug === true) { |
|
| 659 | - $line = $tokens[$first]['line']; |
|
| 660 | - $type = $tokens[$first]['type']; |
|
| 661 | - echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 662 | - } |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 666 | - if (isset($adjustments[$first]) === true) { |
|
| 667 | - $currentIndent += $adjustments[$first]; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - $setIndents[$scopeCloser] = $currentIndent; |
|
| 671 | - |
|
| 672 | - if ($this->debug === true) { |
|
| 673 | - $type = $tokens[$scopeCloser]['type']; |
|
| 674 | - echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - // We only check the indent of scope closers if they are |
|
| 678 | - // curly braces because other constructs tend to have different rules. |
|
| 679 | - if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 680 | - $exact = true; |
|
| 681 | - } else { |
|
| 682 | - $checkToken = null; |
|
| 683 | - } |
|
| 684 | - }//end if |
|
| 685 | - }//end if |
|
| 686 | - |
|
| 687 | - // Handle scope for JS object notation. |
|
| 688 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 689 | - && (($checkToken !== null |
|
| 690 | - && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 691 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 692 | - || ($checkToken === null |
|
| 693 | - && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 694 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 695 | - ) { |
|
| 696 | - if ($this->debug === true) { |
|
| 697 | - $line = $tokens[$i]['line']; |
|
| 698 | - echo "Close JS object on line $line".PHP_EOL; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - $scopeCloser = $checkToken; |
|
| 702 | - if ($scopeCloser === null) { |
|
| 703 | - $scopeCloser = $i; |
|
| 704 | - } else { |
|
| 705 | - $conditionToken = array_pop($openScopes); |
|
| 706 | - if ($this->debug === true) { |
|
| 707 | - $line = $tokens[$conditionToken]['line']; |
|
| 708 | - $type = $tokens[$conditionToken]['type']; |
|
| 709 | - echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - $parens = 0; |
|
| 714 | - if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 715 | - && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 716 | - ) { |
|
| 717 | - $parens = $tokens[$scopeCloser]['nested_parenthesis']; |
|
| 718 | - end($parens); |
|
| 719 | - $parens = key($parens); |
|
| 720 | - if ($this->debug === true) { |
|
| 721 | - $line = $tokens[$parens]['line']; |
|
| 722 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - $condition = 0; |
|
| 727 | - if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 728 | - && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 729 | - ) { |
|
| 730 | - $condition = $tokens[$scopeCloser]['conditions']; |
|
| 731 | - end($condition); |
|
| 732 | - $condition = key($condition); |
|
| 733 | - if ($this->debug === true) { |
|
| 734 | - $line = $tokens[$condition]['line']; |
|
| 735 | - $type = $tokens[$condition]['type']; |
|
| 736 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 737 | - } |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - if ($parens > $condition) { |
|
| 741 | - if ($this->debug === true) { |
|
| 742 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 746 | - $condition = 0; |
|
| 747 | - } else if ($condition > 0) { |
|
| 748 | - if ($this->debug === true) { |
|
| 749 | - echo "\t* using condition *".PHP_EOL; |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 753 | - $parens = 0; |
|
| 754 | - } else { |
|
| 755 | - if ($this->debug === true) { |
|
| 756 | - $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 757 | - echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 761 | - }//end if |
|
| 762 | - |
|
| 763 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 764 | - if (isset($adjustments[$first]) === true) { |
|
| 765 | - $currentIndent += $adjustments[$first]; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - if ($parens > 0 || $condition > 0) { |
|
| 769 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 770 | - if (isset($adjustments[$first]) === true) { |
|
| 771 | - $checkIndent += $adjustments[$first]; |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - if ($condition > 0) { |
|
| 775 | - $checkIndent += $this->indent; |
|
| 776 | - $currentIndent += $this->indent; |
|
| 777 | - $exact = true; |
|
| 778 | - } |
|
| 779 | - } else { |
|
| 780 | - $checkIndent = $currentIndent; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - // Make sure it is divisible by our expected indent. |
|
| 784 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 785 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 786 | - $setIndents[$first] = $currentIndent; |
|
| 787 | - |
|
| 788 | - if ($this->debug === true) { |
|
| 789 | - $type = $tokens[$first]['type']; |
|
| 790 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 791 | - } |
|
| 792 | - }//end if |
|
| 793 | - |
|
| 794 | - if ($checkToken !== null |
|
| 795 | - && isset(Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 796 | - && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes, true) === false |
|
| 797 | - && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 798 | - ) { |
|
| 799 | - $exact = true; |
|
| 800 | - |
|
| 801 | - $lastOpener = null; |
|
| 802 | - if (empty($openScopes) === false) { |
|
| 803 | - end($openScopes); |
|
| 804 | - $lastOpener = current($openScopes); |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - // A scope opener that shares a closer with another token (like multiple |
|
| 808 | - // CASEs using the same BREAK) needs to reduce the indent level so its |
|
| 809 | - // indent is checked correctly. It will then increase the indent again |
|
| 810 | - // (as all openers do) after being checked. |
|
| 811 | - if ($lastOpener !== null |
|
| 812 | - && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 813 | - && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 814 | - && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 815 | - ) { |
|
| 816 | - $currentIndent -= $this->indent; |
|
| 817 | - $setIndents[$lastOpener] = $currentIndent; |
|
| 818 | - if ($this->debug === true) { |
|
| 819 | - $line = $tokens[$i]['line']; |
|
| 820 | - $type = $tokens[$lastOpener]['type']; |
|
| 821 | - echo "Shared closer found on line $line".PHP_EOL; |
|
| 822 | - echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 823 | - } |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 827 | - && $tokenIndent > $currentIndent |
|
| 828 | - ) { |
|
| 829 | - // The opener is indented more than needed, which is fine. |
|
| 830 | - // But just check that it is divisible by our expected indent. |
|
| 831 | - $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 832 | - $exact = false; |
|
| 833 | - |
|
| 834 | - if ($this->debug === true) { |
|
| 835 | - $line = $tokens[$i]['line']; |
|
| 836 | - echo "Closure found on line $line".PHP_EOL; |
|
| 837 | - echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 838 | - } |
|
| 839 | - } |
|
| 840 | - }//end if |
|
| 841 | - |
|
| 842 | - // Method prefix indentation has to be exact or else it will break |
|
| 843 | - // the rest of the function declaration, and potentially future ones. |
|
| 844 | - if ($checkToken !== null |
|
| 845 | - && isset(Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 846 | - && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 847 | - ) { |
|
| 848 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($checkToken + 1), null, true); |
|
| 849 | - if ($next === false || $tokens[$next]['code'] !== T_CLOSURE) { |
|
| 850 | - if ($this->debug === true) { |
|
| 851 | - $line = $tokens[$checkToken]['line']; |
|
| 852 | - $type = $tokens[$checkToken]['type']; |
|
| 853 | - echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - $exact = true; |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - // JS property indentation has to be exact or else if will break |
|
| 861 | - // things like function and object indentation. |
|
| 862 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 863 | - $exact = true; |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - // Open PHP tags needs to be indented to exact column positions |
|
| 867 | - // so they don't cause problems with indent checks for the code |
|
| 868 | - // within them, but they don't need to line up with the current indent |
|
| 869 | - // in most cases. |
|
| 870 | - if ($checkToken !== null |
|
| 871 | - && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 872 | - || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO) |
|
| 873 | - ) { |
|
| 874 | - $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 875 | - |
|
| 876 | - // If we are re-opening a block that was closed in the same |
|
| 877 | - // scope as us, then reset the indent back to what the scope opener |
|
| 878 | - // set instead of using whatever indent this open tag has set. |
|
| 879 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 880 | - $close = $phpcsFile->findPrevious(T_CLOSE_TAG, ($checkToken - 1)); |
|
| 881 | - if ($close !== false |
|
| 882 | - && $tokens[$checkToken]['conditions'] === $tokens[$close]['conditions'] |
|
| 883 | - ) { |
|
| 884 | - $conditions = array_keys($tokens[$checkToken]['conditions']); |
|
| 885 | - $lastCondition = array_pop($conditions); |
|
| 886 | - $lastOpener = $tokens[$lastCondition]['scope_opener']; |
|
| 887 | - $lastCloser = $tokens[$lastCondition]['scope_closer']; |
|
| 888 | - if ($tokens[$lastCloser]['line'] !== $tokens[$checkToken]['line'] |
|
| 889 | - && isset($setIndents[$lastOpener]) === true |
|
| 890 | - ) { |
|
| 891 | - $checkIndent = $setIndents[$lastOpener]; |
|
| 892 | - } |
|
| 893 | - } |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 897 | - }//end if |
|
| 898 | - |
|
| 899 | - // Close tags needs to be indented to exact column positions. |
|
| 900 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_CLOSE_TAG) { |
|
| 901 | - $exact = true; |
|
| 902 | - $checkIndent = $currentIndent; |
|
| 903 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - // Special case for ELSE statements that are not on the same |
|
| 907 | - // line as the previous IF statements closing brace. They still need |
|
| 908 | - // to have the same indent or it will break code after the block. |
|
| 909 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_ELSE) { |
|
| 910 | - $exact = true; |
|
| 911 | - } |
|
| 912 | - |
|
| 913 | - if ($checkIndent === null) { |
|
| 914 | - $checkIndent = $currentIndent; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - /* |
|
| 230 | + $exact = $this->exact; |
|
| 231 | + |
|
| 232 | + if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 233 | + $disableExactEnd = max($disableExactEnd, $tokens[$i]['bracket_closer']); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS |
|
| 237 | + && isset($tokens[$i]['parenthesis_closer']) === true |
|
| 238 | + ) { |
|
| 239 | + $disableExactEnd = max($disableExactEnd, $tokens[$i]['parenthesis_closer']); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if ($exact === true && $i < $disableExactEnd) { |
|
| 243 | + $exact = false; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Detect line changes and figure out where the indent is. |
|
| 247 | + if ($tokens[$i]['column'] === 1) { |
|
| 248 | + $trimmed = ltrim($tokens[$i]['content']); |
|
| 249 | + if ($trimmed === '') { |
|
| 250 | + if (isset($tokens[($i + 1)]) === true |
|
| 251 | + && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 252 | + ) { |
|
| 253 | + $checkToken = ($i + 1); |
|
| 254 | + $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 255 | + } |
|
| 256 | + } else { |
|
| 257 | + $checkToken = $i; |
|
| 258 | + $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // Closing parenthesis should just be indented to at least |
|
| 263 | + // the same level as where they were opened (but can be more). |
|
| 264 | + if (($checkToken !== null |
|
| 265 | + && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 266 | + && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 267 | + || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 268 | + && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 269 | + ) { |
|
| 270 | + if ($checkToken !== null) { |
|
| 271 | + $parenCloser = $checkToken; |
|
| 272 | + } else { |
|
| 273 | + $parenCloser = $i; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if ($this->debug === true) { |
|
| 277 | + $line = $tokens[$i]['line']; |
|
| 278 | + echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 282 | + if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 283 | + $parens = 0; |
|
| 284 | + if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 285 | + && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 286 | + ) { |
|
| 287 | + $parens = $tokens[$parenCloser]['nested_parenthesis']; |
|
| 288 | + end($parens); |
|
| 289 | + $parens = key($parens); |
|
| 290 | + if ($this->debug === true) { |
|
| 291 | + $line = $tokens[$parens]['line']; |
|
| 292 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $condition = 0; |
|
| 297 | + if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 298 | + && empty($tokens[$parenCloser]['conditions']) === false |
|
| 299 | + && (isset($tokens[$parenCloser]['parenthesis_owner']) === false |
|
| 300 | + || $parens > 0) |
|
| 301 | + ) { |
|
| 302 | + $condition = $tokens[$parenCloser]['conditions']; |
|
| 303 | + end($condition); |
|
| 304 | + $condition = key($condition); |
|
| 305 | + if ($this->debug === true) { |
|
| 306 | + $line = $tokens[$condition]['line']; |
|
| 307 | + $type = $tokens[$condition]['type']; |
|
| 308 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + if ($parens > $condition) { |
|
| 313 | + if ($this->debug === true) { |
|
| 314 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $parenOpener = $parens; |
|
| 318 | + $condition = 0; |
|
| 319 | + } else if ($condition > 0) { |
|
| 320 | + if ($this->debug === true) { |
|
| 321 | + echo "\t* using condition *".PHP_EOL; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $parenOpener = $condition; |
|
| 325 | + $parens = 0; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + $exact = false; |
|
| 329 | + |
|
| 330 | + $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 331 | + $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 332 | + |
|
| 333 | + if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 334 | + if ($this->debug === true) { |
|
| 335 | + echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 339 | + if (isset($adjustments[$condition]) === true) { |
|
| 340 | + $checkIndent += $adjustments[$condition]; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + $currentIndent = $checkIndent; |
|
| 344 | + |
|
| 345 | + if ($this->debug === true) { |
|
| 346 | + $type = $tokens[$lastOpenTag]['type']; |
|
| 347 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 348 | + } |
|
| 349 | + } else if ($condition > 0 |
|
| 350 | + && isset($tokens[$condition]['scope_opener']) === true |
|
| 351 | + && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 352 | + ) { |
|
| 353 | + $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 354 | + if (isset($adjustments[$condition]) === true) { |
|
| 355 | + $checkIndent += $adjustments[$condition]; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $currentIndent = $checkIndent; |
|
| 359 | + |
|
| 360 | + if ($this->debug === true) { |
|
| 361 | + $type = $tokens[$condition]['type']; |
|
| 362 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 363 | + } |
|
| 364 | + } else { |
|
| 365 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 366 | + |
|
| 367 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 368 | + if (isset($adjustments[$first]) === true) { |
|
| 369 | + $checkIndent += $adjustments[$first]; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + if ($this->debug === true) { |
|
| 373 | + $line = $tokens[$first]['line']; |
|
| 374 | + $type = $tokens[$first]['type']; |
|
| 375 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if ($first === $tokens[$parenCloser]['parenthesis_opener'] |
|
| 379 | + && $tokens[($first - 1)]['line'] === $tokens[$first]['line'] |
|
| 380 | + ) { |
|
| 381 | + // This is unlikely to be the start of the statement, so look |
|
| 382 | + // back further to find it. |
|
| 383 | + $first--; |
|
| 384 | + if ($this->debug === true) { |
|
| 385 | + $line = $tokens[$first]['line']; |
|
| 386 | + $type = $tokens[$first]['type']; |
|
| 387 | + echo "\t* first token is the parenthesis opener *".PHP_EOL; |
|
| 388 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 393 | + if ($prev !== $first) { |
|
| 394 | + // This is not the start of the statement. |
|
| 395 | + if ($this->debug === true) { |
|
| 396 | + $line = $tokens[$prev]['line']; |
|
| 397 | + $type = $tokens[$prev]['type']; |
|
| 398 | + echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 402 | + if ($first !== false) { |
|
| 403 | + $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 404 | + $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 405 | + } else { |
|
| 406 | + $first = $prev; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + if ($this->debug === true) { |
|
| 410 | + $line = $tokens[$first]['line']; |
|
| 411 | + $type = $tokens[$first]['type']; |
|
| 412 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 413 | + } |
|
| 414 | + }//end if |
|
| 415 | + |
|
| 416 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 417 | + && $tokens[$first]['scope_closer'] === $first |
|
| 418 | + ) { |
|
| 419 | + if ($this->debug === true) { |
|
| 420 | + echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 424 | + $scopeCloser = $first; |
|
| 425 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 426 | + |
|
| 427 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 428 | + if (isset($adjustments[$first]) === true) { |
|
| 429 | + $currentIndent += $adjustments[$first]; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // Make sure it is divisible by our expected indent. |
|
| 433 | + if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 434 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $setIndents[$first] = $currentIndent; |
|
| 438 | + |
|
| 439 | + if ($this->debug === true) { |
|
| 440 | + $type = $tokens[$first]['type']; |
|
| 441 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 442 | + } |
|
| 443 | + }//end if |
|
| 444 | + } else { |
|
| 445 | + // Don't force current indent to be divisible because there could be custom |
|
| 446 | + // rules in place between parenthesis, such as with arrays. |
|
| 447 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 448 | + if (isset($adjustments[$first]) === true) { |
|
| 449 | + $currentIndent += $adjustments[$first]; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + $setIndents[$first] = $currentIndent; |
|
| 453 | + |
|
| 454 | + if ($this->debug === true) { |
|
| 455 | + $type = $tokens[$first]['type']; |
|
| 456 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 457 | + } |
|
| 458 | + }//end if |
|
| 459 | + }//end if |
|
| 460 | + } else if ($this->debug === true) { |
|
| 461 | + echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 462 | + }//end if |
|
| 463 | + }//end if |
|
| 464 | + |
|
| 465 | + // Closing short array bracket should just be indented to at least |
|
| 466 | + // the same level as where it was opened (but can be more). |
|
| 467 | + if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 468 | + || ($checkToken !== null |
|
| 469 | + && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 470 | + ) { |
|
| 471 | + if ($checkToken !== null) { |
|
| 472 | + $arrayCloser = $checkToken; |
|
| 473 | + } else { |
|
| 474 | + $arrayCloser = $i; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + if ($this->debug === true) { |
|
| 478 | + $line = $tokens[$arrayCloser]['line']; |
|
| 479 | + echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 483 | + if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 484 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 485 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 486 | + if (isset($adjustments[$first]) === true) { |
|
| 487 | + $checkIndent += $adjustments[$first]; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + $exact = false; |
|
| 491 | + |
|
| 492 | + if ($this->debug === true) { |
|
| 493 | + $line = $tokens[$first]['line']; |
|
| 494 | + $type = $tokens[$first]['type']; |
|
| 495 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 499 | + // This is unlikely to be the start of the statement, so look |
|
| 500 | + // back further to find it. |
|
| 501 | + $first--; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 505 | + if ($prev !== $first) { |
|
| 506 | + // This is not the start of the statement. |
|
| 507 | + if ($this->debug === true) { |
|
| 508 | + $line = $tokens[$prev]['line']; |
|
| 509 | + $type = $tokens[$prev]['type']; |
|
| 510 | + echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 514 | + $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 515 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 516 | + if ($this->debug === true) { |
|
| 517 | + $line = $tokens[$first]['line']; |
|
| 518 | + $type = $tokens[$first]['type']; |
|
| 519 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 520 | + } |
|
| 521 | + } else if ($tokens[$first]['code'] === T_WHITESPACE) { |
|
| 522 | + $first = $phpcsFile->findNext(T_WHITESPACE, ($first + 1), null, true); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 526 | + && $tokens[$first]['scope_closer'] === $first |
|
| 527 | + ) { |
|
| 528 | + // The first token is a scope closer and would have already |
|
| 529 | + // been processed and set the indent level correctly, so |
|
| 530 | + // don't adjust it again. |
|
| 531 | + if ($this->debug === true) { |
|
| 532 | + echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + if (isset($setIndents[$first]) === true) { |
|
| 536 | + $currentIndent = $setIndents[$first]; |
|
| 537 | + if ($this->debug === true) { |
|
| 538 | + echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + } else { |
|
| 542 | + // Don't force current indent to be divisible because there could be custom |
|
| 543 | + // rules in place for arrays. |
|
| 544 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 545 | + if (isset($adjustments[$first]) === true) { |
|
| 546 | + $currentIndent += $adjustments[$first]; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $setIndents[$first] = $currentIndent; |
|
| 550 | + |
|
| 551 | + if ($this->debug === true) { |
|
| 552 | + $type = $tokens[$first]['type']; |
|
| 553 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 554 | + } |
|
| 555 | + }//end if |
|
| 556 | + } else if ($this->debug === true) { |
|
| 557 | + echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 558 | + }//end if |
|
| 559 | + }//end if |
|
| 560 | + |
|
| 561 | + // Adjust lines within scopes while auto-fixing. |
|
| 562 | + if ($checkToken !== null |
|
| 563 | + && $exact === false |
|
| 564 | + && (empty($tokens[$checkToken]['conditions']) === false |
|
| 565 | + || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 566 | + && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 567 | + ) { |
|
| 568 | + if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 569 | + $condition = $tokens[$checkToken]['conditions']; |
|
| 570 | + end($condition); |
|
| 571 | + $condition = key($condition); |
|
| 572 | + } else { |
|
| 573 | + $condition = $tokens[$checkToken]['scope_condition']; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 577 | + |
|
| 578 | + if (isset($adjustments[$first]) === true |
|
| 579 | + && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 580 | + || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 581 | + ) { |
|
| 582 | + $length = ($tokenIndent + $adjustments[$first]); |
|
| 583 | + |
|
| 584 | + // When fixing, we're going to adjust the indent of this line |
|
| 585 | + // here automatically, so use this new padding value when |
|
| 586 | + // comparing the expected padding to the actual padding. |
|
| 587 | + if ($phpcsFile->fixer->enabled === true) { |
|
| 588 | + $tokenIndent = $length; |
|
| 589 | + $this->adjustIndent($phpcsFile, $checkToken, $length, $adjustments[$first]); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + if ($this->debug === true) { |
|
| 593 | + $line = $tokens[$checkToken]['line']; |
|
| 594 | + $type = $tokens[$checkToken]['type']; |
|
| 595 | + echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + $adjustments[$checkToken] = $adjustments[$first]; |
|
| 599 | + |
|
| 600 | + if ($this->debug === true) { |
|
| 601 | + $line = $tokens[$checkToken]['line']; |
|
| 602 | + $type = $tokens[$checkToken]['type']; |
|
| 603 | + echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 604 | + } |
|
| 605 | + }//end if |
|
| 606 | + }//end if |
|
| 607 | + |
|
| 608 | + // Scope closers reset the required indent to the same level as the opening condition. |
|
| 609 | + if (($checkToken !== null |
|
| 610 | + && isset($openScopes[$checkToken]) === true |
|
| 611 | + || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 612 | + && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 613 | + && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 614 | + && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 615 | + || ($checkToken === null |
|
| 616 | + && isset($openScopes[$i]) === true |
|
| 617 | + || (isset($tokens[$i]['scope_condition']) === true |
|
| 618 | + && isset($tokens[$i]['scope_closer']) === true |
|
| 619 | + && $tokens[$i]['scope_closer'] === $i |
|
| 620 | + && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 621 | + ) { |
|
| 622 | + if ($this->debug === true) { |
|
| 623 | + if ($checkToken === null) { |
|
| 624 | + $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 625 | + $line = $tokens[$i]['line']; |
|
| 626 | + } else { |
|
| 627 | + $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 628 | + $line = $tokens[$checkToken]['line']; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + $scopeCloser = $checkToken; |
|
| 635 | + if ($scopeCloser === null) { |
|
| 636 | + $scopeCloser = $i; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + $conditionToken = array_pop($openScopes); |
|
| 640 | + if ($this->debug === true) { |
|
| 641 | + $line = $tokens[$conditionToken]['line']; |
|
| 642 | + $type = $tokens[$conditionToken]['type']; |
|
| 643 | + echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 647 | + $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $tokens[$scopeCloser]['scope_condition'], true); |
|
| 648 | + if ($this->debug === true) { |
|
| 649 | + $line = $tokens[$first]['line']; |
|
| 650 | + $type = $tokens[$first]['type']; |
|
| 651 | + echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 655 | + && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 656 | + ) { |
|
| 657 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 658 | + if ($this->debug === true) { |
|
| 659 | + $line = $tokens[$first]['line']; |
|
| 660 | + $type = $tokens[$first]['type']; |
|
| 661 | + echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 666 | + if (isset($adjustments[$first]) === true) { |
|
| 667 | + $currentIndent += $adjustments[$first]; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + $setIndents[$scopeCloser] = $currentIndent; |
|
| 671 | + |
|
| 672 | + if ($this->debug === true) { |
|
| 673 | + $type = $tokens[$scopeCloser]['type']; |
|
| 674 | + echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + // We only check the indent of scope closers if they are |
|
| 678 | + // curly braces because other constructs tend to have different rules. |
|
| 679 | + if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 680 | + $exact = true; |
|
| 681 | + } else { |
|
| 682 | + $checkToken = null; |
|
| 683 | + } |
|
| 684 | + }//end if |
|
| 685 | + }//end if |
|
| 686 | + |
|
| 687 | + // Handle scope for JS object notation. |
|
| 688 | + if ($phpcsFile->tokenizerType === 'JS' |
|
| 689 | + && (($checkToken !== null |
|
| 690 | + && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 691 | + && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 692 | + || ($checkToken === null |
|
| 693 | + && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 694 | + && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 695 | + ) { |
|
| 696 | + if ($this->debug === true) { |
|
| 697 | + $line = $tokens[$i]['line']; |
|
| 698 | + echo "Close JS object on line $line".PHP_EOL; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + $scopeCloser = $checkToken; |
|
| 702 | + if ($scopeCloser === null) { |
|
| 703 | + $scopeCloser = $i; |
|
| 704 | + } else { |
|
| 705 | + $conditionToken = array_pop($openScopes); |
|
| 706 | + if ($this->debug === true) { |
|
| 707 | + $line = $tokens[$conditionToken]['line']; |
|
| 708 | + $type = $tokens[$conditionToken]['type']; |
|
| 709 | + echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + $parens = 0; |
|
| 714 | + if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 715 | + && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 716 | + ) { |
|
| 717 | + $parens = $tokens[$scopeCloser]['nested_parenthesis']; |
|
| 718 | + end($parens); |
|
| 719 | + $parens = key($parens); |
|
| 720 | + if ($this->debug === true) { |
|
| 721 | + $line = $tokens[$parens]['line']; |
|
| 722 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + $condition = 0; |
|
| 727 | + if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 728 | + && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 729 | + ) { |
|
| 730 | + $condition = $tokens[$scopeCloser]['conditions']; |
|
| 731 | + end($condition); |
|
| 732 | + $condition = key($condition); |
|
| 733 | + if ($this->debug === true) { |
|
| 734 | + $line = $tokens[$condition]['line']; |
|
| 735 | + $type = $tokens[$condition]['type']; |
|
| 736 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 737 | + } |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + if ($parens > $condition) { |
|
| 741 | + if ($this->debug === true) { |
|
| 742 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 746 | + $condition = 0; |
|
| 747 | + } else if ($condition > 0) { |
|
| 748 | + if ($this->debug === true) { |
|
| 749 | + echo "\t* using condition *".PHP_EOL; |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 753 | + $parens = 0; |
|
| 754 | + } else { |
|
| 755 | + if ($this->debug === true) { |
|
| 756 | + $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 757 | + echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 761 | + }//end if |
|
| 762 | + |
|
| 763 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 764 | + if (isset($adjustments[$first]) === true) { |
|
| 765 | + $currentIndent += $adjustments[$first]; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + if ($parens > 0 || $condition > 0) { |
|
| 769 | + $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 770 | + if (isset($adjustments[$first]) === true) { |
|
| 771 | + $checkIndent += $adjustments[$first]; |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + if ($condition > 0) { |
|
| 775 | + $checkIndent += $this->indent; |
|
| 776 | + $currentIndent += $this->indent; |
|
| 777 | + $exact = true; |
|
| 778 | + } |
|
| 779 | + } else { |
|
| 780 | + $checkIndent = $currentIndent; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + // Make sure it is divisible by our expected indent. |
|
| 784 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 785 | + $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 786 | + $setIndents[$first] = $currentIndent; |
|
| 787 | + |
|
| 788 | + if ($this->debug === true) { |
|
| 789 | + $type = $tokens[$first]['type']; |
|
| 790 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 791 | + } |
|
| 792 | + }//end if |
|
| 793 | + |
|
| 794 | + if ($checkToken !== null |
|
| 795 | + && isset(Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 796 | + && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes, true) === false |
|
| 797 | + && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 798 | + ) { |
|
| 799 | + $exact = true; |
|
| 800 | + |
|
| 801 | + $lastOpener = null; |
|
| 802 | + if (empty($openScopes) === false) { |
|
| 803 | + end($openScopes); |
|
| 804 | + $lastOpener = current($openScopes); |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + // A scope opener that shares a closer with another token (like multiple |
|
| 808 | + // CASEs using the same BREAK) needs to reduce the indent level so its |
|
| 809 | + // indent is checked correctly. It will then increase the indent again |
|
| 810 | + // (as all openers do) after being checked. |
|
| 811 | + if ($lastOpener !== null |
|
| 812 | + && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 813 | + && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 814 | + && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 815 | + ) { |
|
| 816 | + $currentIndent -= $this->indent; |
|
| 817 | + $setIndents[$lastOpener] = $currentIndent; |
|
| 818 | + if ($this->debug === true) { |
|
| 819 | + $line = $tokens[$i]['line']; |
|
| 820 | + $type = $tokens[$lastOpener]['type']; |
|
| 821 | + echo "Shared closer found on line $line".PHP_EOL; |
|
| 822 | + echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 823 | + } |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 827 | + && $tokenIndent > $currentIndent |
|
| 828 | + ) { |
|
| 829 | + // The opener is indented more than needed, which is fine. |
|
| 830 | + // But just check that it is divisible by our expected indent. |
|
| 831 | + $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 832 | + $exact = false; |
|
| 833 | + |
|
| 834 | + if ($this->debug === true) { |
|
| 835 | + $line = $tokens[$i]['line']; |
|
| 836 | + echo "Closure found on line $line".PHP_EOL; |
|
| 837 | + echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 838 | + } |
|
| 839 | + } |
|
| 840 | + }//end if |
|
| 841 | + |
|
| 842 | + // Method prefix indentation has to be exact or else it will break |
|
| 843 | + // the rest of the function declaration, and potentially future ones. |
|
| 844 | + if ($checkToken !== null |
|
| 845 | + && isset(Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 846 | + && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 847 | + ) { |
|
| 848 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($checkToken + 1), null, true); |
|
| 849 | + if ($next === false || $tokens[$next]['code'] !== T_CLOSURE) { |
|
| 850 | + if ($this->debug === true) { |
|
| 851 | + $line = $tokens[$checkToken]['line']; |
|
| 852 | + $type = $tokens[$checkToken]['type']; |
|
| 853 | + echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + $exact = true; |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + // JS property indentation has to be exact or else if will break |
|
| 861 | + // things like function and object indentation. |
|
| 862 | + if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 863 | + $exact = true; |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + // Open PHP tags needs to be indented to exact column positions |
|
| 867 | + // so they don't cause problems with indent checks for the code |
|
| 868 | + // within them, but they don't need to line up with the current indent |
|
| 869 | + // in most cases. |
|
| 870 | + if ($checkToken !== null |
|
| 871 | + && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 872 | + || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO) |
|
| 873 | + ) { |
|
| 874 | + $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 875 | + |
|
| 876 | + // If we are re-opening a block that was closed in the same |
|
| 877 | + // scope as us, then reset the indent back to what the scope opener |
|
| 878 | + // set instead of using whatever indent this open tag has set. |
|
| 879 | + if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 880 | + $close = $phpcsFile->findPrevious(T_CLOSE_TAG, ($checkToken - 1)); |
|
| 881 | + if ($close !== false |
|
| 882 | + && $tokens[$checkToken]['conditions'] === $tokens[$close]['conditions'] |
|
| 883 | + ) { |
|
| 884 | + $conditions = array_keys($tokens[$checkToken]['conditions']); |
|
| 885 | + $lastCondition = array_pop($conditions); |
|
| 886 | + $lastOpener = $tokens[$lastCondition]['scope_opener']; |
|
| 887 | + $lastCloser = $tokens[$lastCondition]['scope_closer']; |
|
| 888 | + if ($tokens[$lastCloser]['line'] !== $tokens[$checkToken]['line'] |
|
| 889 | + && isset($setIndents[$lastOpener]) === true |
|
| 890 | + ) { |
|
| 891 | + $checkIndent = $setIndents[$lastOpener]; |
|
| 892 | + } |
|
| 893 | + } |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 897 | + }//end if |
|
| 898 | + |
|
| 899 | + // Close tags needs to be indented to exact column positions. |
|
| 900 | + if ($checkToken !== null && $tokens[$checkToken]['code'] === T_CLOSE_TAG) { |
|
| 901 | + $exact = true; |
|
| 902 | + $checkIndent = $currentIndent; |
|
| 903 | + $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + // Special case for ELSE statements that are not on the same |
|
| 907 | + // line as the previous IF statements closing brace. They still need |
|
| 908 | + // to have the same indent or it will break code after the block. |
|
| 909 | + if ($checkToken !== null && $tokens[$checkToken]['code'] === T_ELSE) { |
|
| 910 | + $exact = true; |
|
| 911 | + } |
|
| 912 | + |
|
| 913 | + if ($checkIndent === null) { |
|
| 914 | + $checkIndent = $currentIndent; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + /* |
|
| 918 | 918 | The indent of the line is checked by the following IF block. |
| 919 | 919 | |
| 920 | 920 | Up until now, we've just been figuring out what the indent |
@@ -924,554 +924,554 @@ discard block |
||
| 924 | 924 | the checking of future lines |
| 925 | 925 | */ |
| 926 | 926 | |
| 927 | - if ($checkToken !== null |
|
| 928 | - && isset($this->ignoreIndentation[$tokens[$checkToken]['code']]) === false |
|
| 929 | - && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 930 | - || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 931 | - ) { |
|
| 932 | - $type = 'IncorrectExact'; |
|
| 933 | - $error = 'Line indented incorrectly; expected '; |
|
| 934 | - if ($exact === false) { |
|
| 935 | - $error .= 'at least '; |
|
| 936 | - $type = 'Incorrect'; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - if ($this->tabIndent === true) { |
|
| 940 | - $error .= '%s tabs, found %s'; |
|
| 941 | - $data = [ |
|
| 942 | - floor($checkIndent / $this->tabWidth), |
|
| 943 | - floor($tokenIndent / $this->tabWidth), |
|
| 944 | - ]; |
|
| 945 | - } else { |
|
| 946 | - $error .= '%s spaces, found %s'; |
|
| 947 | - $data = [ |
|
| 948 | - $checkIndent, |
|
| 949 | - $tokenIndent, |
|
| 950 | - ]; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - if ($this->debug === true) { |
|
| 954 | - $line = $tokens[$checkToken]['line']; |
|
| 955 | - $message = vsprintf($error, $data); |
|
| 956 | - echo "[Line $line] $message".PHP_EOL; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - // Assume the change would be applied and continue |
|
| 960 | - // checking indents under this assumption. This gives more |
|
| 961 | - // technically accurate error messages. |
|
| 962 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 963 | - |
|
| 964 | - $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 965 | - if ($fix === true || $this->debug === true) { |
|
| 966 | - $accepted = $this->adjustIndent($phpcsFile, $checkToken, $checkIndent, ($checkIndent - $tokenIndent)); |
|
| 967 | - |
|
| 968 | - if ($accepted === true && $this->debug === true) { |
|
| 969 | - $line = $tokens[$checkToken]['line']; |
|
| 970 | - $type = $tokens[$checkToken]['type']; |
|
| 971 | - echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 972 | - } |
|
| 973 | - } |
|
| 974 | - }//end if |
|
| 975 | - |
|
| 976 | - if ($checkToken !== null) { |
|
| 977 | - $i = $checkToken; |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - // Completely skip here/now docs as the indent is a part of the |
|
| 981 | - // content itself. |
|
| 982 | - if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 983 | - || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 984 | - ) { |
|
| 985 | - $i = $phpcsFile->findNext([T_END_HEREDOC, T_END_NOWDOC], ($i + 1)); |
|
| 986 | - $i = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 987 | - continue; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - // Completely skip multi-line strings as the indent is a part of the |
|
| 991 | - // content itself. |
|
| 992 | - if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 993 | - || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 994 | - ) { |
|
| 995 | - $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 996 | - $i--; |
|
| 997 | - continue; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - // Completely skip doc comments as they tend to have complex |
|
| 1001 | - // indentation rules. |
|
| 1002 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1003 | - $i = $tokens[$i]['comment_closer']; |
|
| 1004 | - continue; |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - // Open tags reset the indent level. |
|
| 1008 | - if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 1009 | - || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 1010 | - ) { |
|
| 1011 | - if ($this->debug === true) { |
|
| 1012 | - $line = $tokens[$i]['line']; |
|
| 1013 | - echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 1014 | - } |
|
| 1015 | - |
|
| 1016 | - if ($checkToken === null) { |
|
| 1017 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1018 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 1019 | - } else { |
|
| 1020 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - $lastOpenTag = $i; |
|
| 1024 | - |
|
| 1025 | - if (isset($adjustments[$i]) === true) { |
|
| 1026 | - $currentIndent += $adjustments[$i]; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - // Make sure it is divisible by our expected indent. |
|
| 1030 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1031 | - $setIndents[$i] = $currentIndent; |
|
| 1032 | - |
|
| 1033 | - if ($this->debug === true) { |
|
| 1034 | - $type = $tokens[$i]['type']; |
|
| 1035 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1036 | - } |
|
| 1037 | - |
|
| 1038 | - continue; |
|
| 1039 | - }//end if |
|
| 1040 | - |
|
| 1041 | - // Close tags reset the indent level, unless they are closing a tag |
|
| 1042 | - // opened on the same line. |
|
| 1043 | - if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 1044 | - if ($this->debug === true) { |
|
| 1045 | - $line = $tokens[$i]['line']; |
|
| 1046 | - echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 1050 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1051 | - $lastCloseTag = $i; |
|
| 1052 | - } else { |
|
| 1053 | - if ($lastCloseTag === null) { |
|
| 1054 | - $currentIndent = 0; |
|
| 1055 | - } else { |
|
| 1056 | - $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 1057 | - } |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - if (isset($adjustments[$i]) === true) { |
|
| 1061 | - $currentIndent += $adjustments[$i]; |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - // Make sure it is divisible by our expected indent. |
|
| 1065 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1066 | - $setIndents[$i] = $currentIndent; |
|
| 1067 | - |
|
| 1068 | - if ($this->debug === true) { |
|
| 1069 | - $type = $tokens[$i]['type']; |
|
| 1070 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - continue; |
|
| 1074 | - }//end if |
|
| 1075 | - |
|
| 1076 | - // Anon classes and functions set the indent based on their own indent level. |
|
| 1077 | - if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 1078 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1079 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1080 | - if ($this->debug === true) { |
|
| 1081 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1082 | - $line = $tokens[$i]['line']; |
|
| 1083 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - $i = $closer; |
|
| 1087 | - continue; |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - if ($this->debug === true) { |
|
| 1091 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1092 | - $line = $tokens[$i]['line']; |
|
| 1093 | - echo "Open $type on line $line".PHP_EOL; |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1097 | - if ($this->debug === true) { |
|
| 1098 | - $line = $tokens[$first]['line']; |
|
| 1099 | - $type = $tokens[$first]['type']; |
|
| 1100 | - echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1101 | - } |
|
| 1102 | - |
|
| 1103 | - while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1104 | - && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1105 | - ) { |
|
| 1106 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 1107 | - if ($this->debug === true) { |
|
| 1108 | - $line = $tokens[$first]['line']; |
|
| 1109 | - $type = $tokens[$first]['type']; |
|
| 1110 | - echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1111 | - } |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1115 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1116 | - if ($this->debug === true) { |
|
| 1117 | - $closerToken = $tokens[$i]['scope_closer']; |
|
| 1118 | - $closerLine = $tokens[$closerToken]['line']; |
|
| 1119 | - $closerType = $tokens[$closerToken]['type']; |
|
| 1120 | - $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1121 | - $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1122 | - $conditionType = $tokens[$conditionToken]['type']; |
|
| 1123 | - echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - if (isset($adjustments[$first]) === true) { |
|
| 1127 | - $currentIndent += $adjustments[$first]; |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - // Make sure it is divisible by our expected indent. |
|
| 1131 | - $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1132 | - $i = $tokens[$i]['scope_opener']; |
|
| 1133 | - $setIndents[$i] = $currentIndent; |
|
| 1134 | - |
|
| 1135 | - if ($this->debug === true) { |
|
| 1136 | - $type = $tokens[$i]['type']; |
|
| 1137 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1138 | - } |
|
| 1139 | - |
|
| 1140 | - continue; |
|
| 1141 | - }//end if |
|
| 1142 | - |
|
| 1143 | - // Scope openers increase the indent level. |
|
| 1144 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1145 | - && isset($tokens[$i]['scope_opener']) === true |
|
| 1146 | - && $tokens[$i]['scope_opener'] === $i |
|
| 1147 | - ) { |
|
| 1148 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1149 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1150 | - if ($this->debug === true) { |
|
| 1151 | - $line = $tokens[$i]['line']; |
|
| 1152 | - $type = $tokens[$i]['type']; |
|
| 1153 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - $i = $closer; |
|
| 1157 | - continue; |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1161 | - if (isset(Tokens::$scopeOpeners[$condition]) === true |
|
| 1162 | - && in_array($condition, $this->nonIndentingScopes, true) === false |
|
| 1163 | - ) { |
|
| 1164 | - if ($this->debug === true) { |
|
| 1165 | - $line = $tokens[$i]['line']; |
|
| 1166 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1167 | - echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - $currentIndent += $this->indent; |
|
| 1171 | - $setIndents[$i] = $currentIndent; |
|
| 1172 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1173 | - if ($this->debug === true) { |
|
| 1174 | - $closerToken = $tokens[$i]['scope_closer']; |
|
| 1175 | - $closerLine = $tokens[$closerToken]['line']; |
|
| 1176 | - $closerType = $tokens[$closerToken]['type']; |
|
| 1177 | - $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1178 | - $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1179 | - $conditionType = $tokens[$conditionToken]['type']; |
|
| 1180 | - echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1181 | - } |
|
| 1182 | - |
|
| 1183 | - if ($this->debug === true) { |
|
| 1184 | - $type = $tokens[$i]['type']; |
|
| 1185 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - continue; |
|
| 1189 | - }//end if |
|
| 1190 | - }//end if |
|
| 1191 | - |
|
| 1192 | - // JS objects set the indent level. |
|
| 1193 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 1194 | - && $tokens[$i]['code'] === T_OBJECT |
|
| 1195 | - ) { |
|
| 1196 | - $closer = $tokens[$i]['bracket_closer']; |
|
| 1197 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1198 | - if ($this->debug === true) { |
|
| 1199 | - $line = $tokens[$i]['line']; |
|
| 1200 | - echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - $i = $closer; |
|
| 1204 | - continue; |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - if ($this->debug === true) { |
|
| 1208 | - $line = $tokens[$i]['line']; |
|
| 1209 | - echo "Open JS object on line $line".PHP_EOL; |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1213 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1214 | - if (isset($adjustments[$first]) === true) { |
|
| 1215 | - $currentIndent += $adjustments[$first]; |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - // Make sure it is divisible by our expected indent. |
|
| 1219 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1220 | - $setIndents[$first] = $currentIndent; |
|
| 1221 | - |
|
| 1222 | - if ($this->debug === true) { |
|
| 1223 | - $type = $tokens[$first]['type']; |
|
| 1224 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - continue; |
|
| 1228 | - }//end if |
|
| 1229 | - |
|
| 1230 | - // Closing an anon class or function. |
|
| 1231 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1232 | - && $tokens[$i]['scope_closer'] === $i |
|
| 1233 | - && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1234 | - || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1235 | - ) { |
|
| 1236 | - if ($this->debug === true) { |
|
| 1237 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1238 | - $line = $tokens[$i]['line']; |
|
| 1239 | - echo "Close $type on line $line".PHP_EOL; |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - $prev = false; |
|
| 1243 | - |
|
| 1244 | - $object = 0; |
|
| 1245 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1246 | - $conditions = $tokens[$i]['conditions']; |
|
| 1247 | - krsort($conditions, SORT_NUMERIC); |
|
| 1248 | - foreach ($conditions as $token => $condition) { |
|
| 1249 | - if ($condition === T_OBJECT) { |
|
| 1250 | - $object = $token; |
|
| 1251 | - break; |
|
| 1252 | - } |
|
| 1253 | - } |
|
| 1254 | - |
|
| 1255 | - if ($this->debug === true && $object !== 0) { |
|
| 1256 | - $line = $tokens[$object]['line']; |
|
| 1257 | - echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1258 | - } |
|
| 1259 | - } |
|
| 1260 | - |
|
| 1261 | - $parens = 0; |
|
| 1262 | - if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1263 | - && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1264 | - ) { |
|
| 1265 | - $parens = $tokens[$i]['nested_parenthesis']; |
|
| 1266 | - end($parens); |
|
| 1267 | - $parens = key($parens); |
|
| 1268 | - if ($this->debug === true) { |
|
| 1269 | - $line = $tokens[$parens]['line']; |
|
| 1270 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1271 | - } |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - $condition = 0; |
|
| 1275 | - if (isset($tokens[$i]['conditions']) === true |
|
| 1276 | - && empty($tokens[$i]['conditions']) === false |
|
| 1277 | - ) { |
|
| 1278 | - $condition = $tokens[$i]['conditions']; |
|
| 1279 | - end($condition); |
|
| 1280 | - $condition = key($condition); |
|
| 1281 | - if ($this->debug === true) { |
|
| 1282 | - $line = $tokens[$condition]['line']; |
|
| 1283 | - $type = $tokens[$condition]['type']; |
|
| 1284 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1285 | - } |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - if ($parens > $object && $parens > $condition) { |
|
| 1289 | - if ($this->debug === true) { |
|
| 1290 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1294 | - $object = 0; |
|
| 1295 | - $condition = 0; |
|
| 1296 | - } else if ($object > 0 && $object >= $condition) { |
|
| 1297 | - if ($this->debug === true) { |
|
| 1298 | - echo "\t* using object *".PHP_EOL; |
|
| 1299 | - } |
|
| 1300 | - |
|
| 1301 | - $prev = $object; |
|
| 1302 | - $parens = 0; |
|
| 1303 | - $condition = 0; |
|
| 1304 | - } else if ($condition > 0) { |
|
| 1305 | - if ($this->debug === true) { |
|
| 1306 | - echo "\t* using condition *".PHP_EOL; |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - $prev = $condition; |
|
| 1310 | - $object = 0; |
|
| 1311 | - $parens = 0; |
|
| 1312 | - }//end if |
|
| 1313 | - |
|
| 1314 | - if ($prev === false) { |
|
| 1315 | - $prev = $phpcsFile->findPrevious([T_EQUAL, T_RETURN], ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1316 | - if ($prev === false) { |
|
| 1317 | - $prev = $i; |
|
| 1318 | - if ($this->debug === true) { |
|
| 1319 | - echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1320 | - } |
|
| 1321 | - } |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - if ($this->debug === true) { |
|
| 1325 | - $line = $tokens[$prev]['line']; |
|
| 1326 | - $type = $tokens[$prev]['type']; |
|
| 1327 | - echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1331 | - if ($this->debug === true) { |
|
| 1332 | - $line = $tokens[$first]['line']; |
|
| 1333 | - $type = $tokens[$first]['type']; |
|
| 1334 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1335 | - } |
|
| 1336 | - |
|
| 1337 | - $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1338 | - if ($prev !== $first) { |
|
| 1339 | - // This is not the start of the statement. |
|
| 1340 | - if ($this->debug === true) { |
|
| 1341 | - $line = $tokens[$prev]['line']; |
|
| 1342 | - $type = $tokens[$prev]['type']; |
|
| 1343 | - echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1344 | - } |
|
| 1345 | - |
|
| 1346 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1347 | - if ($this->debug === true) { |
|
| 1348 | - $line = $tokens[$first]['line']; |
|
| 1349 | - $type = $tokens[$first]['type']; |
|
| 1350 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1351 | - } |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1355 | - if ($object > 0 || $condition > 0) { |
|
| 1356 | - $currentIndent += $this->indent; |
|
| 1357 | - } |
|
| 1358 | - |
|
| 1359 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 1360 | - && $tokens[$first]['scope_closer'] === $first |
|
| 1361 | - ) { |
|
| 1362 | - if ($this->debug === true) { |
|
| 1363 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1364 | - } |
|
| 1365 | - |
|
| 1366 | - if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1367 | - $currentIndent = $setIndents[$first]; |
|
| 1368 | - } else if ($this->debug === true) { |
|
| 1369 | - echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1370 | - } |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - // Make sure it is divisible by our expected indent. |
|
| 1374 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1375 | - $setIndents[$first] = $currentIndent; |
|
| 1376 | - |
|
| 1377 | - if ($this->debug === true) { |
|
| 1378 | - $type = $tokens[$first]['type']; |
|
| 1379 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1380 | - } |
|
| 1381 | - }//end if |
|
| 1382 | - }//end for |
|
| 1383 | - |
|
| 1384 | - // Don't process the rest of the file. |
|
| 1385 | - return $phpcsFile->numTokens; |
|
| 1386 | - |
|
| 1387 | - }//end process() |
|
| 1388 | - |
|
| 1389 | - |
|
| 1390 | - /** |
|
| 1391 | - * Processes this test, when one of its tokens is encountered. |
|
| 1392 | - * |
|
| 1393 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document. |
|
| 1394 | - * @param int $stackPtr The position of the current token |
|
| 1395 | - * in the stack passed in $tokens. |
|
| 1396 | - * @param int $length The length of the new indent. |
|
| 1397 | - * @param int $change The difference in length between |
|
| 1398 | - * the old and new indent. |
|
| 1399 | - * |
|
| 1400 | - * @return bool |
|
| 1401 | - */ |
|
| 1402 | - protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) |
|
| 1403 | - { |
|
| 1404 | - $tokens = $phpcsFile->getTokens(); |
|
| 1405 | - |
|
| 1406 | - // We don't adjust indents outside of PHP. |
|
| 1407 | - if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) { |
|
| 1408 | - return false; |
|
| 1409 | - } |
|
| 1410 | - |
|
| 1411 | - $padding = ''; |
|
| 1412 | - if ($length > 0) { |
|
| 1413 | - if ($this->tabIndent === true) { |
|
| 1414 | - $numTabs = floor($length / $this->tabWidth); |
|
| 1415 | - if ($numTabs > 0) { |
|
| 1416 | - $numSpaces = ($length - ($numTabs * $this->tabWidth)); |
|
| 1417 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1418 | - } |
|
| 1419 | - } else { |
|
| 1420 | - $padding = str_repeat(' ', $length); |
|
| 1421 | - } |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - if ($tokens[$stackPtr]['column'] === 1) { |
|
| 1425 | - $trimmed = ltrim($tokens[$stackPtr]['content']); |
|
| 1426 | - $accepted = $phpcsFile->fixer->replaceToken($stackPtr, $padding.$trimmed); |
|
| 1427 | - } else { |
|
| 1428 | - // Easier to just replace the entire indent. |
|
| 1429 | - $accepted = $phpcsFile->fixer->replaceToken(($stackPtr - 1), $padding); |
|
| 1430 | - } |
|
| 1431 | - |
|
| 1432 | - if ($accepted === false) { |
|
| 1433 | - return false; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1437 | - // We adjusted the start of a comment, so adjust the rest of it |
|
| 1438 | - // as well so the alignment remains correct. |
|
| 1439 | - for ($x = ($stackPtr + 1); $x < $tokens[$stackPtr]['comment_closer']; $x++) { |
|
| 1440 | - if ($tokens[$x]['column'] !== 1) { |
|
| 1441 | - continue; |
|
| 1442 | - } |
|
| 1443 | - |
|
| 1444 | - $length = 0; |
|
| 1445 | - if ($tokens[$x]['code'] === T_DOC_COMMENT_WHITESPACE) { |
|
| 1446 | - $length = $tokens[$x]['length']; |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - $padding = ($length + $change); |
|
| 1450 | - if ($padding > 0) { |
|
| 1451 | - if ($this->tabIndent === true) { |
|
| 1452 | - $numTabs = floor($padding / $this->tabWidth); |
|
| 1453 | - $numSpaces = ($padding - ($numTabs * $this->tabWidth)); |
|
| 1454 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1455 | - } else { |
|
| 1456 | - $padding = str_repeat(' ', $padding); |
|
| 1457 | - } |
|
| 1458 | - } else { |
|
| 1459 | - $padding = ''; |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - $phpcsFile->fixer->replaceToken($x, $padding); |
|
| 1463 | - if ($this->debug === true) { |
|
| 1464 | - $length = strlen($padding); |
|
| 1465 | - $line = $tokens[$x]['line']; |
|
| 1466 | - $type = $tokens[$x]['type']; |
|
| 1467 | - echo "\t=> Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 1468 | - } |
|
| 1469 | - }//end for |
|
| 1470 | - }//end if |
|
| 1471 | - |
|
| 1472 | - return true; |
|
| 1473 | - |
|
| 1474 | - }//end adjustIndent() |
|
| 927 | + if ($checkToken !== null |
|
| 928 | + && isset($this->ignoreIndentation[$tokens[$checkToken]['code']]) === false |
|
| 929 | + && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 930 | + || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 931 | + ) { |
|
| 932 | + $type = 'IncorrectExact'; |
|
| 933 | + $error = 'Line indented incorrectly; expected '; |
|
| 934 | + if ($exact === false) { |
|
| 935 | + $error .= 'at least '; |
|
| 936 | + $type = 'Incorrect'; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + if ($this->tabIndent === true) { |
|
| 940 | + $error .= '%s tabs, found %s'; |
|
| 941 | + $data = [ |
|
| 942 | + floor($checkIndent / $this->tabWidth), |
|
| 943 | + floor($tokenIndent / $this->tabWidth), |
|
| 944 | + ]; |
|
| 945 | + } else { |
|
| 946 | + $error .= '%s spaces, found %s'; |
|
| 947 | + $data = [ |
|
| 948 | + $checkIndent, |
|
| 949 | + $tokenIndent, |
|
| 950 | + ]; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + if ($this->debug === true) { |
|
| 954 | + $line = $tokens[$checkToken]['line']; |
|
| 955 | + $message = vsprintf($error, $data); |
|
| 956 | + echo "[Line $line] $message".PHP_EOL; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + // Assume the change would be applied and continue |
|
| 960 | + // checking indents under this assumption. This gives more |
|
| 961 | + // technically accurate error messages. |
|
| 962 | + $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 963 | + |
|
| 964 | + $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 965 | + if ($fix === true || $this->debug === true) { |
|
| 966 | + $accepted = $this->adjustIndent($phpcsFile, $checkToken, $checkIndent, ($checkIndent - $tokenIndent)); |
|
| 967 | + |
|
| 968 | + if ($accepted === true && $this->debug === true) { |
|
| 969 | + $line = $tokens[$checkToken]['line']; |
|
| 970 | + $type = $tokens[$checkToken]['type']; |
|
| 971 | + echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 972 | + } |
|
| 973 | + } |
|
| 974 | + }//end if |
|
| 975 | + |
|
| 976 | + if ($checkToken !== null) { |
|
| 977 | + $i = $checkToken; |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + // Completely skip here/now docs as the indent is a part of the |
|
| 981 | + // content itself. |
|
| 982 | + if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 983 | + || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 984 | + ) { |
|
| 985 | + $i = $phpcsFile->findNext([T_END_HEREDOC, T_END_NOWDOC], ($i + 1)); |
|
| 986 | + $i = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 987 | + continue; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + // Completely skip multi-line strings as the indent is a part of the |
|
| 991 | + // content itself. |
|
| 992 | + if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 993 | + || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 994 | + ) { |
|
| 995 | + $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 996 | + $i--; |
|
| 997 | + continue; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + // Completely skip doc comments as they tend to have complex |
|
| 1001 | + // indentation rules. |
|
| 1002 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1003 | + $i = $tokens[$i]['comment_closer']; |
|
| 1004 | + continue; |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + // Open tags reset the indent level. |
|
| 1008 | + if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 1009 | + || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 1010 | + ) { |
|
| 1011 | + if ($this->debug === true) { |
|
| 1012 | + $line = $tokens[$i]['line']; |
|
| 1013 | + echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 1014 | + } |
|
| 1015 | + |
|
| 1016 | + if ($checkToken === null) { |
|
| 1017 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1018 | + $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 1019 | + } else { |
|
| 1020 | + $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + $lastOpenTag = $i; |
|
| 1024 | + |
|
| 1025 | + if (isset($adjustments[$i]) === true) { |
|
| 1026 | + $currentIndent += $adjustments[$i]; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + // Make sure it is divisible by our expected indent. |
|
| 1030 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1031 | + $setIndents[$i] = $currentIndent; |
|
| 1032 | + |
|
| 1033 | + if ($this->debug === true) { |
|
| 1034 | + $type = $tokens[$i]['type']; |
|
| 1035 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1036 | + } |
|
| 1037 | + |
|
| 1038 | + continue; |
|
| 1039 | + }//end if |
|
| 1040 | + |
|
| 1041 | + // Close tags reset the indent level, unless they are closing a tag |
|
| 1042 | + // opened on the same line. |
|
| 1043 | + if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 1044 | + if ($this->debug === true) { |
|
| 1045 | + $line = $tokens[$i]['line']; |
|
| 1046 | + echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 1050 | + $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1051 | + $lastCloseTag = $i; |
|
| 1052 | + } else { |
|
| 1053 | + if ($lastCloseTag === null) { |
|
| 1054 | + $currentIndent = 0; |
|
| 1055 | + } else { |
|
| 1056 | + $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 1057 | + } |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + if (isset($adjustments[$i]) === true) { |
|
| 1061 | + $currentIndent += $adjustments[$i]; |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + // Make sure it is divisible by our expected indent. |
|
| 1065 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1066 | + $setIndents[$i] = $currentIndent; |
|
| 1067 | + |
|
| 1068 | + if ($this->debug === true) { |
|
| 1069 | + $type = $tokens[$i]['type']; |
|
| 1070 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + continue; |
|
| 1074 | + }//end if |
|
| 1075 | + |
|
| 1076 | + // Anon classes and functions set the indent based on their own indent level. |
|
| 1077 | + if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 1078 | + $closer = $tokens[$i]['scope_closer']; |
|
| 1079 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1080 | + if ($this->debug === true) { |
|
| 1081 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1082 | + $line = $tokens[$i]['line']; |
|
| 1083 | + echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + $i = $closer; |
|
| 1087 | + continue; |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + if ($this->debug === true) { |
|
| 1091 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1092 | + $line = $tokens[$i]['line']; |
|
| 1093 | + echo "Open $type on line $line".PHP_EOL; |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1097 | + if ($this->debug === true) { |
|
| 1098 | + $line = $tokens[$first]['line']; |
|
| 1099 | + $type = $tokens[$first]['type']; |
|
| 1100 | + echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1101 | + } |
|
| 1102 | + |
|
| 1103 | + while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1104 | + && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1105 | + ) { |
|
| 1106 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 1107 | + if ($this->debug === true) { |
|
| 1108 | + $line = $tokens[$first]['line']; |
|
| 1109 | + $type = $tokens[$first]['type']; |
|
| 1110 | + echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1111 | + } |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1115 | + $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1116 | + if ($this->debug === true) { |
|
| 1117 | + $closerToken = $tokens[$i]['scope_closer']; |
|
| 1118 | + $closerLine = $tokens[$closerToken]['line']; |
|
| 1119 | + $closerType = $tokens[$closerToken]['type']; |
|
| 1120 | + $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1121 | + $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1122 | + $conditionType = $tokens[$conditionToken]['type']; |
|
| 1123 | + echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + if (isset($adjustments[$first]) === true) { |
|
| 1127 | + $currentIndent += $adjustments[$first]; |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + // Make sure it is divisible by our expected indent. |
|
| 1131 | + $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1132 | + $i = $tokens[$i]['scope_opener']; |
|
| 1133 | + $setIndents[$i] = $currentIndent; |
|
| 1134 | + |
|
| 1135 | + if ($this->debug === true) { |
|
| 1136 | + $type = $tokens[$i]['type']; |
|
| 1137 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1138 | + } |
|
| 1139 | + |
|
| 1140 | + continue; |
|
| 1141 | + }//end if |
|
| 1142 | + |
|
| 1143 | + // Scope openers increase the indent level. |
|
| 1144 | + if (isset($tokens[$i]['scope_condition']) === true |
|
| 1145 | + && isset($tokens[$i]['scope_opener']) === true |
|
| 1146 | + && $tokens[$i]['scope_opener'] === $i |
|
| 1147 | + ) { |
|
| 1148 | + $closer = $tokens[$i]['scope_closer']; |
|
| 1149 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1150 | + if ($this->debug === true) { |
|
| 1151 | + $line = $tokens[$i]['line']; |
|
| 1152 | + $type = $tokens[$i]['type']; |
|
| 1153 | + echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + $i = $closer; |
|
| 1157 | + continue; |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1161 | + if (isset(Tokens::$scopeOpeners[$condition]) === true |
|
| 1162 | + && in_array($condition, $this->nonIndentingScopes, true) === false |
|
| 1163 | + ) { |
|
| 1164 | + if ($this->debug === true) { |
|
| 1165 | + $line = $tokens[$i]['line']; |
|
| 1166 | + $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1167 | + echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + $currentIndent += $this->indent; |
|
| 1171 | + $setIndents[$i] = $currentIndent; |
|
| 1172 | + $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1173 | + if ($this->debug === true) { |
|
| 1174 | + $closerToken = $tokens[$i]['scope_closer']; |
|
| 1175 | + $closerLine = $tokens[$closerToken]['line']; |
|
| 1176 | + $closerType = $tokens[$closerToken]['type']; |
|
| 1177 | + $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1178 | + $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1179 | + $conditionType = $tokens[$conditionToken]['type']; |
|
| 1180 | + echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1181 | + } |
|
| 1182 | + |
|
| 1183 | + if ($this->debug === true) { |
|
| 1184 | + $type = $tokens[$i]['type']; |
|
| 1185 | + echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + continue; |
|
| 1189 | + }//end if |
|
| 1190 | + }//end if |
|
| 1191 | + |
|
| 1192 | + // JS objects set the indent level. |
|
| 1193 | + if ($phpcsFile->tokenizerType === 'JS' |
|
| 1194 | + && $tokens[$i]['code'] === T_OBJECT |
|
| 1195 | + ) { |
|
| 1196 | + $closer = $tokens[$i]['bracket_closer']; |
|
| 1197 | + if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1198 | + if ($this->debug === true) { |
|
| 1199 | + $line = $tokens[$i]['line']; |
|
| 1200 | + echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + $i = $closer; |
|
| 1204 | + continue; |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + if ($this->debug === true) { |
|
| 1208 | + $line = $tokens[$i]['line']; |
|
| 1209 | + echo "Open JS object on line $line".PHP_EOL; |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1213 | + $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1214 | + if (isset($adjustments[$first]) === true) { |
|
| 1215 | + $currentIndent += $adjustments[$first]; |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + // Make sure it is divisible by our expected indent. |
|
| 1219 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1220 | + $setIndents[$first] = $currentIndent; |
|
| 1221 | + |
|
| 1222 | + if ($this->debug === true) { |
|
| 1223 | + $type = $tokens[$first]['type']; |
|
| 1224 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + continue; |
|
| 1228 | + }//end if |
|
| 1229 | + |
|
| 1230 | + // Closing an anon class or function. |
|
| 1231 | + if (isset($tokens[$i]['scope_condition']) === true |
|
| 1232 | + && $tokens[$i]['scope_closer'] === $i |
|
| 1233 | + && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1234 | + || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1235 | + ) { |
|
| 1236 | + if ($this->debug === true) { |
|
| 1237 | + $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1238 | + $line = $tokens[$i]['line']; |
|
| 1239 | + echo "Close $type on line $line".PHP_EOL; |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + $prev = false; |
|
| 1243 | + |
|
| 1244 | + $object = 0; |
|
| 1245 | + if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1246 | + $conditions = $tokens[$i]['conditions']; |
|
| 1247 | + krsort($conditions, SORT_NUMERIC); |
|
| 1248 | + foreach ($conditions as $token => $condition) { |
|
| 1249 | + if ($condition === T_OBJECT) { |
|
| 1250 | + $object = $token; |
|
| 1251 | + break; |
|
| 1252 | + } |
|
| 1253 | + } |
|
| 1254 | + |
|
| 1255 | + if ($this->debug === true && $object !== 0) { |
|
| 1256 | + $line = $tokens[$object]['line']; |
|
| 1257 | + echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1258 | + } |
|
| 1259 | + } |
|
| 1260 | + |
|
| 1261 | + $parens = 0; |
|
| 1262 | + if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1263 | + && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1264 | + ) { |
|
| 1265 | + $parens = $tokens[$i]['nested_parenthesis']; |
|
| 1266 | + end($parens); |
|
| 1267 | + $parens = key($parens); |
|
| 1268 | + if ($this->debug === true) { |
|
| 1269 | + $line = $tokens[$parens]['line']; |
|
| 1270 | + echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1271 | + } |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + $condition = 0; |
|
| 1275 | + if (isset($tokens[$i]['conditions']) === true |
|
| 1276 | + && empty($tokens[$i]['conditions']) === false |
|
| 1277 | + ) { |
|
| 1278 | + $condition = $tokens[$i]['conditions']; |
|
| 1279 | + end($condition); |
|
| 1280 | + $condition = key($condition); |
|
| 1281 | + if ($this->debug === true) { |
|
| 1282 | + $line = $tokens[$condition]['line']; |
|
| 1283 | + $type = $tokens[$condition]['type']; |
|
| 1284 | + echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1285 | + } |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + if ($parens > $object && $parens > $condition) { |
|
| 1289 | + if ($this->debug === true) { |
|
| 1290 | + echo "\t* using parenthesis *".PHP_EOL; |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1294 | + $object = 0; |
|
| 1295 | + $condition = 0; |
|
| 1296 | + } else if ($object > 0 && $object >= $condition) { |
|
| 1297 | + if ($this->debug === true) { |
|
| 1298 | + echo "\t* using object *".PHP_EOL; |
|
| 1299 | + } |
|
| 1300 | + |
|
| 1301 | + $prev = $object; |
|
| 1302 | + $parens = 0; |
|
| 1303 | + $condition = 0; |
|
| 1304 | + } else if ($condition > 0) { |
|
| 1305 | + if ($this->debug === true) { |
|
| 1306 | + echo "\t* using condition *".PHP_EOL; |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + $prev = $condition; |
|
| 1310 | + $object = 0; |
|
| 1311 | + $parens = 0; |
|
| 1312 | + }//end if |
|
| 1313 | + |
|
| 1314 | + if ($prev === false) { |
|
| 1315 | + $prev = $phpcsFile->findPrevious([T_EQUAL, T_RETURN], ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1316 | + if ($prev === false) { |
|
| 1317 | + $prev = $i; |
|
| 1318 | + if ($this->debug === true) { |
|
| 1319 | + echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1320 | + } |
|
| 1321 | + } |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + if ($this->debug === true) { |
|
| 1325 | + $line = $tokens[$prev]['line']; |
|
| 1326 | + $type = $tokens[$prev]['type']; |
|
| 1327 | + echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1331 | + if ($this->debug === true) { |
|
| 1332 | + $line = $tokens[$first]['line']; |
|
| 1333 | + $type = $tokens[$first]['type']; |
|
| 1334 | + echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1335 | + } |
|
| 1336 | + |
|
| 1337 | + $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1338 | + if ($prev !== $first) { |
|
| 1339 | + // This is not the start of the statement. |
|
| 1340 | + if ($this->debug === true) { |
|
| 1341 | + $line = $tokens[$prev]['line']; |
|
| 1342 | + $type = $tokens[$prev]['type']; |
|
| 1343 | + echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1344 | + } |
|
| 1345 | + |
|
| 1346 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1347 | + if ($this->debug === true) { |
|
| 1348 | + $line = $tokens[$first]['line']; |
|
| 1349 | + $type = $tokens[$first]['type']; |
|
| 1350 | + echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1351 | + } |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1355 | + if ($object > 0 || $condition > 0) { |
|
| 1356 | + $currentIndent += $this->indent; |
|
| 1357 | + } |
|
| 1358 | + |
|
| 1359 | + if (isset($tokens[$first]['scope_closer']) === true |
|
| 1360 | + && $tokens[$first]['scope_closer'] === $first |
|
| 1361 | + ) { |
|
| 1362 | + if ($this->debug === true) { |
|
| 1363 | + echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1367 | + $currentIndent = $setIndents[$first]; |
|
| 1368 | + } else if ($this->debug === true) { |
|
| 1369 | + echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1370 | + } |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + // Make sure it is divisible by our expected indent. |
|
| 1374 | + $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1375 | + $setIndents[$first] = $currentIndent; |
|
| 1376 | + |
|
| 1377 | + if ($this->debug === true) { |
|
| 1378 | + $type = $tokens[$first]['type']; |
|
| 1379 | + echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1380 | + } |
|
| 1381 | + }//end if |
|
| 1382 | + }//end for |
|
| 1383 | + |
|
| 1384 | + // Don't process the rest of the file. |
|
| 1385 | + return $phpcsFile->numTokens; |
|
| 1386 | + |
|
| 1387 | + }//end process() |
|
| 1388 | + |
|
| 1389 | + |
|
| 1390 | + /** |
|
| 1391 | + * Processes this test, when one of its tokens is encountered. |
|
| 1392 | + * |
|
| 1393 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document. |
|
| 1394 | + * @param int $stackPtr The position of the current token |
|
| 1395 | + * in the stack passed in $tokens. |
|
| 1396 | + * @param int $length The length of the new indent. |
|
| 1397 | + * @param int $change The difference in length between |
|
| 1398 | + * the old and new indent. |
|
| 1399 | + * |
|
| 1400 | + * @return bool |
|
| 1401 | + */ |
|
| 1402 | + protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) |
|
| 1403 | + { |
|
| 1404 | + $tokens = $phpcsFile->getTokens(); |
|
| 1405 | + |
|
| 1406 | + // We don't adjust indents outside of PHP. |
|
| 1407 | + if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) { |
|
| 1408 | + return false; |
|
| 1409 | + } |
|
| 1410 | + |
|
| 1411 | + $padding = ''; |
|
| 1412 | + if ($length > 0) { |
|
| 1413 | + if ($this->tabIndent === true) { |
|
| 1414 | + $numTabs = floor($length / $this->tabWidth); |
|
| 1415 | + if ($numTabs > 0) { |
|
| 1416 | + $numSpaces = ($length - ($numTabs * $this->tabWidth)); |
|
| 1417 | + $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1418 | + } |
|
| 1419 | + } else { |
|
| 1420 | + $padding = str_repeat(' ', $length); |
|
| 1421 | + } |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + if ($tokens[$stackPtr]['column'] === 1) { |
|
| 1425 | + $trimmed = ltrim($tokens[$stackPtr]['content']); |
|
| 1426 | + $accepted = $phpcsFile->fixer->replaceToken($stackPtr, $padding.$trimmed); |
|
| 1427 | + } else { |
|
| 1428 | + // Easier to just replace the entire indent. |
|
| 1429 | + $accepted = $phpcsFile->fixer->replaceToken(($stackPtr - 1), $padding); |
|
| 1430 | + } |
|
| 1431 | + |
|
| 1432 | + if ($accepted === false) { |
|
| 1433 | + return false; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1437 | + // We adjusted the start of a comment, so adjust the rest of it |
|
| 1438 | + // as well so the alignment remains correct. |
|
| 1439 | + for ($x = ($stackPtr + 1); $x < $tokens[$stackPtr]['comment_closer']; $x++) { |
|
| 1440 | + if ($tokens[$x]['column'] !== 1) { |
|
| 1441 | + continue; |
|
| 1442 | + } |
|
| 1443 | + |
|
| 1444 | + $length = 0; |
|
| 1445 | + if ($tokens[$x]['code'] === T_DOC_COMMENT_WHITESPACE) { |
|
| 1446 | + $length = $tokens[$x]['length']; |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + $padding = ($length + $change); |
|
| 1450 | + if ($padding > 0) { |
|
| 1451 | + if ($this->tabIndent === true) { |
|
| 1452 | + $numTabs = floor($padding / $this->tabWidth); |
|
| 1453 | + $numSpaces = ($padding - ($numTabs * $this->tabWidth)); |
|
| 1454 | + $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1455 | + } else { |
|
| 1456 | + $padding = str_repeat(' ', $padding); |
|
| 1457 | + } |
|
| 1458 | + } else { |
|
| 1459 | + $padding = ''; |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + $phpcsFile->fixer->replaceToken($x, $padding); |
|
| 1463 | + if ($this->debug === true) { |
|
| 1464 | + $length = strlen($padding); |
|
| 1465 | + $line = $tokens[$x]['line']; |
|
| 1466 | + $type = $tokens[$x]['type']; |
|
| 1467 | + echo "\t=> Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 1468 | + } |
|
| 1469 | + }//end for |
|
| 1470 | + }//end if |
|
| 1471 | + |
|
| 1472 | + return true; |
|
| 1473 | + |
|
| 1474 | + }//end adjustIndent() |
|
| 1475 | 1475 | |
| 1476 | 1476 | |
| 1477 | 1477 | }//end class |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @var int[] |
| 74 | 74 | */ |
| 75 | - public $ignoreIndentationTokens = []; |
|
| 75 | + public $ignoreIndentationTokens = [ ]; |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * List of tokens not needing to be checked for indentation. |
@@ -82,14 +82,14 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @var int[] |
| 84 | 84 | */ |
| 85 | - private $ignoreIndentation = []; |
|
| 85 | + private $ignoreIndentation = [ ]; |
|
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * Any scope openers that should not cause an indent. |
| 89 | 89 | * |
| 90 | 90 | * @var int[] |
| 91 | 91 | */ |
| 92 | - protected $nonIndentingScopes = []; |
|
| 92 | + protected $nonIndentingScopes = [ ]; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * Show debug output for this sniff. |
@@ -106,11 +106,11 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function register() |
| 108 | 108 | { |
| 109 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
|
| 109 | + if ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) === true ) { |
|
| 110 | 110 | $this->debug = false; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - return [T_OPEN_TAG]; |
|
| 113 | + return [ T_OPEN_TAG ]; |
|
| 114 | 114 | |
| 115 | 115 | }//end register() |
| 116 | 116 | |
@@ -124,15 +124,15 @@ discard block |
||
| 124 | 124 | * |
| 125 | 125 | * @return void |
| 126 | 126 | */ |
| 127 | - public function process(File $phpcsFile, $stackPtr) |
|
| 127 | + public function process( File $phpcsFile, $stackPtr ) |
|
| 128 | 128 | { |
| 129 | - $debug = Config::getConfigData('scope_indent_debug'); |
|
| 130 | - if ($debug !== null) { |
|
| 131 | - $this->debug = (bool) $debug; |
|
| 129 | + $debug = Config::getConfigData( 'scope_indent_debug' ); |
|
| 130 | + if ( $debug !== null ) { |
|
| 131 | + $this->debug = (bool)$debug; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if ($this->tabWidth === null) { |
|
| 135 | - if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) { |
|
| 134 | + if ( $this->tabWidth === null ) { |
|
| 135 | + if ( isset( $phpcsFile->config->tabWidth ) === false || $phpcsFile->config->tabWidth === 0 ) { |
|
| 136 | 136 | // We have no idea how wide tabs are, so assume 4 spaces for fixing. |
| 137 | 137 | // It shouldn't really matter because indent checks elsewhere in the |
| 138 | 138 | // standard should fix things up. |
@@ -144,77 +144,77 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | $lastOpenTag = $stackPtr; |
| 146 | 146 | $lastCloseTag = null; |
| 147 | - $openScopes = []; |
|
| 148 | - $adjustments = []; |
|
| 149 | - $setIndents = []; |
|
| 147 | + $openScopes = [ ]; |
|
| 148 | + $adjustments = [ ]; |
|
| 149 | + $setIndents = [ ]; |
|
| 150 | 150 | $disableExactEnd = 0; |
| 151 | 151 | |
| 152 | 152 | $tokens = $phpcsFile->getTokens(); |
| 153 | - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); |
|
| 154 | - $trimmed = ltrim($tokens[$first]['content']); |
|
| 155 | - if ($trimmed === '') { |
|
| 156 | - $currentIndent = ($tokens[$stackPtr]['column'] - 1); |
|
| 153 | + $first = $phpcsFile->findFirstOnLine( T_INLINE_HTML, $stackPtr ); |
|
| 154 | + $trimmed = ltrim( $tokens[ $first ][ 'content' ] ); |
|
| 155 | + if ( $trimmed === '' ) { |
|
| 156 | + $currentIndent = ( $tokens[ $stackPtr ][ 'column' ] - 1 ); |
|
| 157 | 157 | } else { |
| 158 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen($trimmed)); |
|
| 158 | + $currentIndent = ( strlen( $tokens[ $first ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ($this->debug === true) { |
|
| 162 | - $line = $tokens[$stackPtr]['line']; |
|
| 163 | - echo "Start with token $stackPtr on line $line with indent $currentIndent".PHP_EOL; |
|
| 161 | + if ( $this->debug === true ) { |
|
| 162 | + $line = $tokens[ $stackPtr ][ 'line' ]; |
|
| 163 | + echo "Start with token $stackPtr on line $line with indent $currentIndent" . PHP_EOL; |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - if (empty($this->ignoreIndentation) === true) { |
|
| 167 | - $this->ignoreIndentation = [T_INLINE_HTML => true]; |
|
| 168 | - foreach ($this->ignoreIndentationTokens as $token) { |
|
| 169 | - if (is_int($token) === false) { |
|
| 170 | - if (defined($token) === false) { |
|
| 166 | + if ( empty( $this->ignoreIndentation ) === true ) { |
|
| 167 | + $this->ignoreIndentation = [ T_INLINE_HTML => true ]; |
|
| 168 | + foreach ( $this->ignoreIndentationTokens as $token ) { |
|
| 169 | + if ( is_int( $token ) === false ) { |
|
| 170 | + if ( defined( $token ) === false ) { |
|
| 171 | 171 | continue; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - $token = constant($token); |
|
| 174 | + $token = constant( $token ); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - $this->ignoreIndentation[$token] = true; |
|
| 177 | + $this->ignoreIndentation[ $token ] = true; |
|
| 178 | 178 | } |
| 179 | 179 | }//end if |
| 180 | 180 | |
| 181 | - $this->exact = (bool) $this->exact; |
|
| 182 | - $this->tabIndent = (bool) $this->tabIndent; |
|
| 181 | + $this->exact = (bool)$this->exact; |
|
| 182 | + $this->tabIndent = (bool)$this->tabIndent; |
|
| 183 | 183 | |
| 184 | 184 | $checkAnnotations = $phpcsFile->config->annotations; |
| 185 | 185 | |
| 186 | - for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
| 187 | - if ($i === false) { |
|
| 186 | + for ( $i = ( $stackPtr + 1 ); $i < $phpcsFile->numTokens; $i++ ) { |
|
| 187 | + if ( $i === false ) { |
|
| 188 | 188 | // Something has gone very wrong; maybe a parse error. |
| 189 | 189 | break; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if ($checkAnnotations === true |
|
| 193 | - && $tokens[$i]['code'] === T_PHPCS_SET |
|
| 194 | - && isset($tokens[$i]['sniffCode']) === true |
|
| 195 | - && $tokens[$i]['sniffCode'] === 'Generic.WhiteSpace.ScopeIndent' |
|
| 196 | - && $tokens[$i]['sniffProperty'] === 'exact' |
|
| 192 | + if ( $checkAnnotations === true |
|
| 193 | + && $tokens[ $i ][ 'code' ] === T_PHPCS_SET |
|
| 194 | + && isset( $tokens[ $i ][ 'sniffCode' ] ) === true |
|
| 195 | + && $tokens[ $i ][ 'sniffCode' ] === 'Generic.WhiteSpace.ScopeIndent' |
|
| 196 | + && $tokens[ $i ][ 'sniffProperty' ] === 'exact' |
|
| 197 | 197 | ) { |
| 198 | - $value = $tokens[$i]['sniffPropertyValue']; |
|
| 199 | - if ($value === 'true') { |
|
| 198 | + $value = $tokens[ $i ][ 'sniffPropertyValue' ]; |
|
| 199 | + if ( $value === 'true' ) { |
|
| 200 | 200 | $value = true; |
| 201 | - } else if ($value === 'false') { |
|
| 201 | + } else if ( $value === 'false' ) { |
|
| 202 | 202 | $value = false; |
| 203 | 203 | } else { |
| 204 | - $value = (bool) $value; |
|
| 204 | + $value = (bool)$value; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | $this->exact = $value; |
| 208 | 208 | |
| 209 | - if ($this->debug === true) { |
|
| 210 | - $line = $tokens[$i]['line']; |
|
| 211 | - if ($this->exact === true) { |
|
| 209 | + if ( $this->debug === true ) { |
|
| 210 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 211 | + if ( $this->exact === true ) { |
|
| 212 | 212 | $value = 'true'; |
| 213 | 213 | } else { |
| 214 | 214 | $value = 'false'; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - echo "* token $i on line $line set exact flag to $value *".PHP_EOL; |
|
| 217 | + echo "* token $i on line $line set exact flag to $value *" . PHP_EOL; |
|
| 218 | 218 | } |
| 219 | 219 | }//end if |
| 220 | 220 | |
@@ -229,96 +229,96 @@ discard block |
||
| 229 | 229 | |
| 230 | 230 | $exact = $this->exact; |
| 231 | 231 | |
| 232 | - if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 233 | - $disableExactEnd = max($disableExactEnd, $tokens[$i]['bracket_closer']); |
|
| 232 | + if ( $tokens[ $i ][ 'code' ] === T_OPEN_SHORT_ARRAY ) { |
|
| 233 | + $disableExactEnd = max( $disableExactEnd, $tokens[ $i ][ 'bracket_closer' ] ); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS |
|
| 237 | - && isset($tokens[$i]['parenthesis_closer']) === true |
|
| 236 | + if ( $tokens[ $i ][ 'code' ] === T_OPEN_PARENTHESIS |
|
| 237 | + && isset( $tokens[ $i ][ 'parenthesis_closer' ] ) === true |
|
| 238 | 238 | ) { |
| 239 | - $disableExactEnd = max($disableExactEnd, $tokens[$i]['parenthesis_closer']); |
|
| 239 | + $disableExactEnd = max( $disableExactEnd, $tokens[ $i ][ 'parenthesis_closer' ] ); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - if ($exact === true && $i < $disableExactEnd) { |
|
| 242 | + if ( $exact === true && $i < $disableExactEnd ) { |
|
| 243 | 243 | $exact = false; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Detect line changes and figure out where the indent is. |
| 247 | - if ($tokens[$i]['column'] === 1) { |
|
| 248 | - $trimmed = ltrim($tokens[$i]['content']); |
|
| 249 | - if ($trimmed === '') { |
|
| 250 | - if (isset($tokens[($i + 1)]) === true |
|
| 251 | - && $tokens[$i]['line'] === $tokens[($i + 1)]['line'] |
|
| 247 | + if ( $tokens[ $i ][ 'column' ] === 1 ) { |
|
| 248 | + $trimmed = ltrim( $tokens[ $i ][ 'content' ] ); |
|
| 249 | + if ( $trimmed === '' ) { |
|
| 250 | + if ( isset( $tokens[ ( $i + 1 ) ] ) === true |
|
| 251 | + && $tokens[ $i ][ 'line' ] === $tokens[ ( $i + 1 ) ][ 'line' ] |
|
| 252 | 252 | ) { |
| 253 | - $checkToken = ($i + 1); |
|
| 254 | - $tokenIndent = ($tokens[($i + 1)]['column'] - 1); |
|
| 253 | + $checkToken = ( $i + 1 ); |
|
| 254 | + $tokenIndent = ( $tokens[ ( $i + 1 ) ][ 'column' ] - 1 ); |
|
| 255 | 255 | } |
| 256 | 256 | } else { |
| 257 | 257 | $checkToken = $i; |
| 258 | - $tokenIndent = (strlen($tokens[$i]['content']) - strlen($trimmed)); |
|
| 258 | + $tokenIndent = ( strlen( $tokens[ $i ][ 'content' ] ) - strlen( $trimmed ) ); |
|
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | // Closing parenthesis should just be indented to at least |
| 263 | 263 | // the same level as where they were opened (but can be more). |
| 264 | - if (($checkToken !== null |
|
| 265 | - && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS |
|
| 266 | - && isset($tokens[$checkToken]['parenthesis_opener']) === true) |
|
| 267 | - || ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS |
|
| 268 | - && isset($tokens[$i]['parenthesis_opener']) === true) |
|
| 264 | + if ( ( $checkToken !== null |
|
| 265 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_PARENTHESIS |
|
| 266 | + && isset( $tokens[ $checkToken ][ 'parenthesis_opener' ] ) === true ) |
|
| 267 | + || ( $tokens[ $i ][ 'code' ] === T_CLOSE_PARENTHESIS |
|
| 268 | + && isset( $tokens[ $i ][ 'parenthesis_opener' ] ) === true ) |
|
| 269 | 269 | ) { |
| 270 | - if ($checkToken !== null) { |
|
| 270 | + if ( $checkToken !== null ) { |
|
| 271 | 271 | $parenCloser = $checkToken; |
| 272 | 272 | } else { |
| 273 | 273 | $parenCloser = $i; |
| 274 | 274 | } |
| 275 | 275 | |
| 276 | - if ($this->debug === true) { |
|
| 277 | - $line = $tokens[$i]['line']; |
|
| 278 | - echo "Closing parenthesis found on line $line".PHP_EOL; |
|
| 276 | + if ( $this->debug === true ) { |
|
| 277 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 278 | + echo "Closing parenthesis found on line $line" . PHP_EOL; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | - $parenOpener = $tokens[$parenCloser]['parenthesis_opener']; |
|
| 282 | - if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) { |
|
| 281 | + $parenOpener = $tokens[ $parenCloser ][ 'parenthesis_opener' ]; |
|
| 282 | + if ( $tokens[ $parenCloser ][ 'line' ] !== $tokens[ $parenOpener ][ 'line' ] ) { |
|
| 283 | 283 | $parens = 0; |
| 284 | - if (isset($tokens[$parenCloser]['nested_parenthesis']) === true |
|
| 285 | - && empty($tokens[$parenCloser]['nested_parenthesis']) === false |
|
| 284 | + if ( isset( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ) === true |
|
| 285 | + && empty( $tokens[ $parenCloser ][ 'nested_parenthesis' ] ) === false |
|
| 286 | 286 | ) { |
| 287 | - $parens = $tokens[$parenCloser]['nested_parenthesis']; |
|
| 288 | - end($parens); |
|
| 289 | - $parens = key($parens); |
|
| 290 | - if ($this->debug === true) { |
|
| 291 | - $line = $tokens[$parens]['line']; |
|
| 292 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 287 | + $parens = $tokens[ $parenCloser ][ 'nested_parenthesis' ]; |
|
| 288 | + end( $parens ); |
|
| 289 | + $parens = key( $parens ); |
|
| 290 | + if ( $this->debug === true ) { |
|
| 291 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 292 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 293 | 293 | } |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | $condition = 0; |
| 297 | - if (isset($tokens[$parenCloser]['conditions']) === true |
|
| 298 | - && empty($tokens[$parenCloser]['conditions']) === false |
|
| 299 | - && (isset($tokens[$parenCloser]['parenthesis_owner']) === false |
|
| 300 | - || $parens > 0) |
|
| 297 | + if ( isset( $tokens[ $parenCloser ][ 'conditions' ] ) === true |
|
| 298 | + && empty( $tokens[ $parenCloser ][ 'conditions' ] ) === false |
|
| 299 | + && ( isset( $tokens[ $parenCloser ][ 'parenthesis_owner' ] ) === false |
|
| 300 | + || $parens > 0 ) |
|
| 301 | 301 | ) { |
| 302 | - $condition = $tokens[$parenCloser]['conditions']; |
|
| 303 | - end($condition); |
|
| 304 | - $condition = key($condition); |
|
| 305 | - if ($this->debug === true) { |
|
| 306 | - $line = $tokens[$condition]['line']; |
|
| 307 | - $type = $tokens[$condition]['type']; |
|
| 308 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 302 | + $condition = $tokens[ $parenCloser ][ 'conditions' ]; |
|
| 303 | + end( $condition ); |
|
| 304 | + $condition = key( $condition ); |
|
| 305 | + if ( $this->debug === true ) { |
|
| 306 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 307 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 308 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - if ($parens > $condition) { |
|
| 313 | - if ($this->debug === true) { |
|
| 314 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 312 | + if ( $parens > $condition ) { |
|
| 313 | + if ( $this->debug === true ) { |
|
| 314 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | $parenOpener = $parens; |
| 318 | 318 | $condition = 0; |
| 319 | - } else if ($condition > 0) { |
|
| 320 | - if ($this->debug === true) { |
|
| 321 | - echo "\t* using condition *".PHP_EOL; |
|
| 319 | + } else if ( $condition > 0 ) { |
|
| 320 | + if ( $this->debug === true ) { |
|
| 321 | + echo "\t* using condition *" . PHP_EOL; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | $parenOpener = $condition; |
@@ -327,356 +327,356 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | $exact = false; |
| 329 | 329 | |
| 330 | - $lastOpenTagConditions = array_keys($tokens[$lastOpenTag]['conditions']); |
|
| 331 | - $lastOpenTagCondition = array_pop($lastOpenTagConditions); |
|
| 330 | + $lastOpenTagConditions = array_keys( $tokens[ $lastOpenTag ][ 'conditions' ] ); |
|
| 331 | + $lastOpenTagCondition = array_pop( $lastOpenTagConditions ); |
|
| 332 | 332 | |
| 333 | - if ($condition > 0 && $lastOpenTagCondition === $condition) { |
|
| 334 | - if ($this->debug === true) { |
|
| 335 | - echo "\t* open tag is inside condition; using open tag *".PHP_EOL; |
|
| 333 | + if ( $condition > 0 && $lastOpenTagCondition === $condition ) { |
|
| 334 | + if ( $this->debug === true ) { |
|
| 335 | + echo "\t* open tag is inside condition; using open tag *" . PHP_EOL; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - $checkIndent = ($tokens[$lastOpenTag]['column'] - 1); |
|
| 339 | - if (isset($adjustments[$condition]) === true) { |
|
| 340 | - $checkIndent += $adjustments[$condition]; |
|
| 338 | + $checkIndent = ( $tokens[ $lastOpenTag ][ 'column' ] - 1 ); |
|
| 339 | + if ( isset( $adjustments[ $condition ] ) === true ) { |
|
| 340 | + $checkIndent += $adjustments[ $condition ]; |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | $currentIndent = $checkIndent; |
| 344 | 344 | |
| 345 | - if ($this->debug === true) { |
|
| 346 | - $type = $tokens[$lastOpenTag]['type']; |
|
| 347 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)".PHP_EOL; |
|
| 345 | + if ( $this->debug === true ) { |
|
| 346 | + $type = $tokens[ $lastOpenTag ][ 'type' ]; |
|
| 347 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $lastOpenTag ($type)" . PHP_EOL; |
|
| 348 | 348 | } |
| 349 | - } else if ($condition > 0 |
|
| 350 | - && isset($tokens[$condition]['scope_opener']) === true |
|
| 351 | - && isset($setIndents[$tokens[$condition]['scope_opener']]) === true |
|
| 349 | + } else if ( $condition > 0 |
|
| 350 | + && isset( $tokens[ $condition ][ 'scope_opener' ] ) === true |
|
| 351 | + && isset( $setIndents[ $tokens[ $condition ][ 'scope_opener' ] ] ) === true |
|
| 352 | 352 | ) { |
| 353 | - $checkIndent = $setIndents[$tokens[$condition]['scope_opener']]; |
|
| 354 | - if (isset($adjustments[$condition]) === true) { |
|
| 355 | - $checkIndent += $adjustments[$condition]; |
|
| 353 | + $checkIndent = $setIndents[ $tokens[ $condition ][ 'scope_opener' ] ]; |
|
| 354 | + if ( isset( $adjustments[ $condition ] ) === true ) { |
|
| 355 | + $checkIndent += $adjustments[ $condition ]; |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | $currentIndent = $checkIndent; |
| 359 | 359 | |
| 360 | - if ($this->debug === true) { |
|
| 361 | - $type = $tokens[$condition]['type']; |
|
| 362 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)".PHP_EOL; |
|
| 360 | + if ( $this->debug === true ) { |
|
| 361 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 362 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $condition ($type)" . PHP_EOL; |
|
| 363 | 363 | } |
| 364 | 364 | } else { |
| 365 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parenOpener, true); |
|
| 365 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $parenOpener, true ); |
|
| 366 | 366 | |
| 367 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 368 | - if (isset($adjustments[$first]) === true) { |
|
| 369 | - $checkIndent += $adjustments[$first]; |
|
| 367 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 368 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 369 | + $checkIndent += $adjustments[ $first ]; |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | - if ($this->debug === true) { |
|
| 373 | - $line = $tokens[$first]['line']; |
|
| 374 | - $type = $tokens[$first]['type']; |
|
| 375 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 372 | + if ( $this->debug === true ) { |
|
| 373 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 374 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 375 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - if ($first === $tokens[$parenCloser]['parenthesis_opener'] |
|
| 379 | - && $tokens[($first - 1)]['line'] === $tokens[$first]['line'] |
|
| 378 | + if ( $first === $tokens[ $parenCloser ][ 'parenthesis_opener' ] |
|
| 379 | + && $tokens[ ( $first - 1 ) ][ 'line' ] === $tokens[ $first ][ 'line' ] |
|
| 380 | 380 | ) { |
| 381 | 381 | // This is unlikely to be the start of the statement, so look |
| 382 | 382 | // back further to find it. |
| 383 | 383 | $first--; |
| 384 | - if ($this->debug === true) { |
|
| 385 | - $line = $tokens[$first]['line']; |
|
| 386 | - $type = $tokens[$first]['type']; |
|
| 387 | - echo "\t* first token is the parenthesis opener *".PHP_EOL; |
|
| 388 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 384 | + if ( $this->debug === true ) { |
|
| 385 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 386 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 387 | + echo "\t* first token is the parenthesis opener *" . PHP_EOL; |
|
| 388 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 389 | 389 | } |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 393 | - if ($prev !== $first) { |
|
| 392 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 393 | + if ( $prev !== $first ) { |
|
| 394 | 394 | // This is not the start of the statement. |
| 395 | - if ($this->debug === true) { |
|
| 396 | - $line = $tokens[$prev]['line']; |
|
| 397 | - $type = $tokens[$prev]['type']; |
|
| 398 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 395 | + if ( $this->debug === true ) { |
|
| 396 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 397 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 398 | + echo "\t* previous is $type on line $line *" . PHP_EOL; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 402 | - if ($first !== false) { |
|
| 403 | - $prev = $phpcsFile->findStartOfStatement($first, T_COMMA); |
|
| 404 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $prev, true); |
|
| 401 | + $first = $phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_INLINE_HTML ], $prev, true ); |
|
| 402 | + if ( $first !== false ) { |
|
| 403 | + $prev = $phpcsFile->findStartOfStatement( $first, T_COMMA ); |
|
| 404 | + $first = $phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_INLINE_HTML ], $prev, true ); |
|
| 405 | 405 | } else { |
| 406 | 406 | $first = $prev; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | - if ($this->debug === true) { |
|
| 410 | - $line = $tokens[$first]['line']; |
|
| 411 | - $type = $tokens[$first]['type']; |
|
| 412 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 409 | + if ( $this->debug === true ) { |
|
| 410 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 411 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 412 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 413 | 413 | } |
| 414 | 414 | }//end if |
| 415 | 415 | |
| 416 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 417 | - && $tokens[$first]['scope_closer'] === $first |
|
| 416 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 417 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 418 | 418 | ) { |
| 419 | - if ($this->debug === true) { |
|
| 420 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 419 | + if ( $this->debug === true ) { |
|
| 420 | + echo "\t* first token is a scope closer *" . PHP_EOL; |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | - if (isset($tokens[$first]['scope_condition']) === true) { |
|
| 423 | + if ( isset( $tokens[ $first ][ 'scope_condition' ] ) === true ) { |
|
| 424 | 424 | $scopeCloser = $first; |
| 425 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['scope_condition'], true); |
|
| 425 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $tokens[ $scopeCloser ][ 'scope_condition' ], true ); |
|
| 426 | 426 | |
| 427 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 428 | - if (isset($adjustments[$first]) === true) { |
|
| 429 | - $currentIndent += $adjustments[$first]; |
|
| 427 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 428 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 429 | + $currentIndent += $adjustments[ $first ]; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | // Make sure it is divisible by our expected indent. |
| 433 | - if ($tokens[$tokens[$scopeCloser]['scope_condition']]['code'] !== T_CLOSURE) { |
|
| 434 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 433 | + if ( $tokens[ $tokens[ $scopeCloser ][ 'scope_condition' ] ][ 'code' ] !== T_CLOSURE ) { |
|
| 434 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - $setIndents[$first] = $currentIndent; |
|
| 437 | + $setIndents[ $first ] = $currentIndent; |
|
| 438 | 438 | |
| 439 | - if ($this->debug === true) { |
|
| 440 | - $type = $tokens[$first]['type']; |
|
| 441 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 439 | + if ( $this->debug === true ) { |
|
| 440 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 441 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 442 | 442 | } |
| 443 | 443 | }//end if |
| 444 | 444 | } else { |
| 445 | 445 | // Don't force current indent to be divisible because there could be custom |
| 446 | 446 | // rules in place between parenthesis, such as with arrays. |
| 447 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 448 | - if (isset($adjustments[$first]) === true) { |
|
| 449 | - $currentIndent += $adjustments[$first]; |
|
| 447 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 448 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 449 | + $currentIndent += $adjustments[ $first ]; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - $setIndents[$first] = $currentIndent; |
|
| 452 | + $setIndents[ $first ] = $currentIndent; |
|
| 453 | 453 | |
| 454 | - if ($this->debug === true) { |
|
| 455 | - $type = $tokens[$first]['type']; |
|
| 456 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 454 | + if ( $this->debug === true ) { |
|
| 455 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 456 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 457 | 457 | } |
| 458 | 458 | }//end if |
| 459 | 459 | }//end if |
| 460 | - } else if ($this->debug === true) { |
|
| 461 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 460 | + } else if ( $this->debug === true ) { |
|
| 461 | + echo "\t * ignoring single-line definition *" . PHP_EOL; |
|
| 462 | 462 | }//end if |
| 463 | 463 | }//end if |
| 464 | 464 | |
| 465 | 465 | // Closing short array bracket should just be indented to at least |
| 466 | 466 | // the same level as where it was opened (but can be more). |
| 467 | - if ($tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY |
|
| 468 | - || ($checkToken !== null |
|
| 469 | - && $tokens[$checkToken]['code'] === T_CLOSE_SHORT_ARRAY) |
|
| 467 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSE_SHORT_ARRAY |
|
| 468 | + || ( $checkToken !== null |
|
| 469 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_SHORT_ARRAY ) |
|
| 470 | 470 | ) { |
| 471 | - if ($checkToken !== null) { |
|
| 471 | + if ( $checkToken !== null ) { |
|
| 472 | 472 | $arrayCloser = $checkToken; |
| 473 | 473 | } else { |
| 474 | 474 | $arrayCloser = $i; |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | - if ($this->debug === true) { |
|
| 478 | - $line = $tokens[$arrayCloser]['line']; |
|
| 479 | - echo "Closing short array bracket found on line $line".PHP_EOL; |
|
| 477 | + if ( $this->debug === true ) { |
|
| 478 | + $line = $tokens[ $arrayCloser ][ 'line' ]; |
|
| 479 | + echo "Closing short array bracket found on line $line" . PHP_EOL; |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | - $arrayOpener = $tokens[$arrayCloser]['bracket_opener']; |
|
| 483 | - if ($tokens[$arrayCloser]['line'] !== $tokens[$arrayOpener]['line']) { |
|
| 484 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayOpener, true); |
|
| 485 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 486 | - if (isset($adjustments[$first]) === true) { |
|
| 487 | - $checkIndent += $adjustments[$first]; |
|
| 482 | + $arrayOpener = $tokens[ $arrayCloser ][ 'bracket_opener' ]; |
|
| 483 | + if ( $tokens[ $arrayCloser ][ 'line' ] !== $tokens[ $arrayOpener ][ 'line' ] ) { |
|
| 484 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $arrayOpener, true ); |
|
| 485 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 486 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 487 | + $checkIndent += $adjustments[ $first ]; |
|
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | $exact = false; |
| 491 | 491 | |
| 492 | - if ($this->debug === true) { |
|
| 493 | - $line = $tokens[$first]['line']; |
|
| 494 | - $type = $tokens[$first]['type']; |
|
| 495 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 492 | + if ( $this->debug === true ) { |
|
| 493 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 494 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 495 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 496 | 496 | } |
| 497 | 497 | |
| 498 | - if ($first === $tokens[$arrayCloser]['bracket_opener']) { |
|
| 498 | + if ( $first === $tokens[ $arrayCloser ][ 'bracket_opener' ] ) { |
|
| 499 | 499 | // This is unlikely to be the start of the statement, so look |
| 500 | 500 | // back further to find it. |
| 501 | 501 | $first--; |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | - $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 505 | - if ($prev !== $first) { |
|
| 504 | + $prev = $phpcsFile->findStartOfStatement( $first, [ T_COMMA, T_DOUBLE_ARROW ] ); |
|
| 505 | + if ( $prev !== $first ) { |
|
| 506 | 506 | // This is not the start of the statement. |
| 507 | - if ($this->debug === true) { |
|
| 508 | - $line = $tokens[$prev]['line']; |
|
| 509 | - $type = $tokens[$prev]['type']; |
|
| 510 | - echo "\t* previous is $type on line $line *".PHP_EOL; |
|
| 507 | + if ( $this->debug === true ) { |
|
| 508 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 509 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 510 | + echo "\t* previous is $type on line $line *" . PHP_EOL; |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 514 | - $prev = $phpcsFile->findStartOfStatement($first, [T_COMMA, T_DOUBLE_ARROW]); |
|
| 515 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 516 | - if ($this->debug === true) { |
|
| 517 | - $line = $tokens[$first]['line']; |
|
| 518 | - $type = $tokens[$first]['type']; |
|
| 519 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 513 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 514 | + $prev = $phpcsFile->findStartOfStatement( $first, [ T_COMMA, T_DOUBLE_ARROW ] ); |
|
| 515 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 516 | + if ( $this->debug === true ) { |
|
| 517 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 518 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 519 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 520 | 520 | } |
| 521 | - } else if ($tokens[$first]['code'] === T_WHITESPACE) { |
|
| 522 | - $first = $phpcsFile->findNext(T_WHITESPACE, ($first + 1), null, true); |
|
| 521 | + } else if ( $tokens[ $first ][ 'code' ] === T_WHITESPACE ) { |
|
| 522 | + $first = $phpcsFile->findNext( T_WHITESPACE, ( $first + 1 ), null, true ); |
|
| 523 | 523 | } |
| 524 | 524 | |
| 525 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 526 | - && $tokens[$first]['scope_closer'] === $first |
|
| 525 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 526 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 527 | 527 | ) { |
| 528 | 528 | // The first token is a scope closer and would have already |
| 529 | 529 | // been processed and set the indent level correctly, so |
| 530 | 530 | // don't adjust it again. |
| 531 | - if ($this->debug === true) { |
|
| 532 | - echo "\t* first token is a scope closer; ignoring closing short array bracket *".PHP_EOL; |
|
| 531 | + if ( $this->debug === true ) { |
|
| 532 | + echo "\t* first token is a scope closer; ignoring closing short array bracket *" . PHP_EOL; |
|
| 533 | 533 | } |
| 534 | 534 | |
| 535 | - if (isset($setIndents[$first]) === true) { |
|
| 536 | - $currentIndent = $setIndents[$first]; |
|
| 537 | - if ($this->debug === true) { |
|
| 538 | - echo "\t=> indent reset to $currentIndent".PHP_EOL; |
|
| 535 | + if ( isset( $setIndents[ $first ] ) === true ) { |
|
| 536 | + $currentIndent = $setIndents[ $first ]; |
|
| 537 | + if ( $this->debug === true ) { |
|
| 538 | + echo "\t=> indent reset to $currentIndent" . PHP_EOL; |
|
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | 541 | } else { |
| 542 | 542 | // Don't force current indent to be divisible because there could be custom |
| 543 | 543 | // rules in place for arrays. |
| 544 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 545 | - if (isset($adjustments[$first]) === true) { |
|
| 546 | - $currentIndent += $adjustments[$first]; |
|
| 544 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 545 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 546 | + $currentIndent += $adjustments[ $first ]; |
|
| 547 | 547 | } |
| 548 | 548 | |
| 549 | - $setIndents[$first] = $currentIndent; |
|
| 549 | + $setIndents[ $first ] = $currentIndent; |
|
| 550 | 550 | |
| 551 | - if ($this->debug === true) { |
|
| 552 | - $type = $tokens[$first]['type']; |
|
| 553 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 551 | + if ( $this->debug === true ) { |
|
| 552 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 553 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 554 | 554 | } |
| 555 | 555 | }//end if |
| 556 | - } else if ($this->debug === true) { |
|
| 557 | - echo "\t * ignoring single-line definition *".PHP_EOL; |
|
| 556 | + } else if ( $this->debug === true ) { |
|
| 557 | + echo "\t * ignoring single-line definition *" . PHP_EOL; |
|
| 558 | 558 | }//end if |
| 559 | 559 | }//end if |
| 560 | 560 | |
| 561 | 561 | // Adjust lines within scopes while auto-fixing. |
| 562 | - if ($checkToken !== null |
|
| 562 | + if ( $checkToken !== null |
|
| 563 | 563 | && $exact === false |
| 564 | - && (empty($tokens[$checkToken]['conditions']) === false |
|
| 565 | - || (isset($tokens[$checkToken]['scope_opener']) === true |
|
| 566 | - && $tokens[$checkToken]['scope_opener'] === $checkToken)) |
|
| 564 | + && ( empty( $tokens[ $checkToken ][ 'conditions' ] ) === false |
|
| 565 | + || ( isset( $tokens[ $checkToken ][ 'scope_opener' ] ) === true |
|
| 566 | + && $tokens[ $checkToken ][ 'scope_opener' ] === $checkToken ) ) |
|
| 567 | 567 | ) { |
| 568 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 569 | - $condition = $tokens[$checkToken]['conditions']; |
|
| 570 | - end($condition); |
|
| 571 | - $condition = key($condition); |
|
| 568 | + if ( empty( $tokens[ $checkToken ][ 'conditions' ] ) === false ) { |
|
| 569 | + $condition = $tokens[ $checkToken ][ 'conditions' ]; |
|
| 570 | + end( $condition ); |
|
| 571 | + $condition = key( $condition ); |
|
| 572 | 572 | } else { |
| 573 | - $condition = $tokens[$checkToken]['scope_condition']; |
|
| 573 | + $condition = $tokens[ $checkToken ][ 'scope_condition' ]; |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 576 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $condition, true ); |
|
| 577 | 577 | |
| 578 | - if (isset($adjustments[$first]) === true |
|
| 579 | - && (($adjustments[$first] < 0 && $tokenIndent > $currentIndent) |
|
| 580 | - || ($adjustments[$first] > 0 && $tokenIndent < $currentIndent)) |
|
| 578 | + if ( isset( $adjustments[ $first ] ) === true |
|
| 579 | + && ( ( $adjustments[ $first ] < 0 && $tokenIndent > $currentIndent ) |
|
| 580 | + || ( $adjustments[ $first ] > 0 && $tokenIndent < $currentIndent ) ) |
|
| 581 | 581 | ) { |
| 582 | - $length = ($tokenIndent + $adjustments[$first]); |
|
| 582 | + $length = ( $tokenIndent + $adjustments[ $first ] ); |
|
| 583 | 583 | |
| 584 | 584 | // When fixing, we're going to adjust the indent of this line |
| 585 | 585 | // here automatically, so use this new padding value when |
| 586 | 586 | // comparing the expected padding to the actual padding. |
| 587 | - if ($phpcsFile->fixer->enabled === true) { |
|
| 587 | + if ( $phpcsFile->fixer->enabled === true ) { |
|
| 588 | 588 | $tokenIndent = $length; |
| 589 | - $this->adjustIndent($phpcsFile, $checkToken, $length, $adjustments[$first]); |
|
| 589 | + $this->adjustIndent( $phpcsFile, $checkToken, $length, $adjustments[ $first ] ); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | - if ($this->debug === true) { |
|
| 593 | - $line = $tokens[$checkToken]['line']; |
|
| 594 | - $type = $tokens[$checkToken]['type']; |
|
| 595 | - echo "Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 592 | + if ( $this->debug === true ) { |
|
| 593 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 594 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 595 | + echo "Indent adjusted to $length for $type on line $line" . PHP_EOL; |
|
| 596 | 596 | } |
| 597 | 597 | |
| 598 | - $adjustments[$checkToken] = $adjustments[$first]; |
|
| 598 | + $adjustments[ $checkToken ] = $adjustments[ $first ]; |
|
| 599 | 599 | |
| 600 | - if ($this->debug === true) { |
|
| 601 | - $line = $tokens[$checkToken]['line']; |
|
| 602 | - $type = $tokens[$checkToken]['type']; |
|
| 603 | - echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 600 | + if ( $this->debug === true ) { |
|
| 601 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 602 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 603 | + echo "\t=> add adjustment of " . $adjustments[ $checkToken ] . " for token $checkToken ($type) on line $line" . PHP_EOL; |
|
| 604 | 604 | } |
| 605 | 605 | }//end if |
| 606 | 606 | }//end if |
| 607 | 607 | |
| 608 | 608 | // Scope closers reset the required indent to the same level as the opening condition. |
| 609 | - if (($checkToken !== null |
|
| 610 | - && isset($openScopes[$checkToken]) === true |
|
| 611 | - || (isset($tokens[$checkToken]['scope_condition']) === true |
|
| 612 | - && isset($tokens[$checkToken]['scope_closer']) === true |
|
| 613 | - && $tokens[$checkToken]['scope_closer'] === $checkToken |
|
| 614 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['scope_opener']]['line'])) |
|
| 615 | - || ($checkToken === null |
|
| 616 | - && isset($openScopes[$i]) === true |
|
| 617 | - || (isset($tokens[$i]['scope_condition']) === true |
|
| 618 | - && isset($tokens[$i]['scope_closer']) === true |
|
| 619 | - && $tokens[$i]['scope_closer'] === $i |
|
| 620 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['scope_opener']]['line'])) |
|
| 609 | + if ( ( $checkToken !== null |
|
| 610 | + && isset( $openScopes[ $checkToken ] ) === true |
|
| 611 | + || ( isset( $tokens[ $checkToken ][ 'scope_condition' ] ) === true |
|
| 612 | + && isset( $tokens[ $checkToken ][ 'scope_closer' ] ) === true |
|
| 613 | + && $tokens[ $checkToken ][ 'scope_closer' ] === $checkToken |
|
| 614 | + && $tokens[ $checkToken ][ 'line' ] !== $tokens[ $tokens[ $checkToken ][ 'scope_opener' ] ][ 'line' ] ) ) |
|
| 615 | + || ( $checkToken === null |
|
| 616 | + && isset( $openScopes[ $i ] ) === true |
|
| 617 | + || ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 618 | + && isset( $tokens[ $i ][ 'scope_closer' ] ) === true |
|
| 619 | + && $tokens[ $i ][ 'scope_closer' ] === $i |
|
| 620 | + && $tokens[ $i ][ 'line' ] !== $tokens[ $tokens[ $i ][ 'scope_opener' ] ][ 'line' ] ) ) |
|
| 621 | 621 | ) { |
| 622 | - if ($this->debug === true) { |
|
| 623 | - if ($checkToken === null) { |
|
| 624 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 625 | - $line = $tokens[$i]['line']; |
|
| 622 | + if ( $this->debug === true ) { |
|
| 623 | + if ( $checkToken === null ) { |
|
| 624 | + $type = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ]; |
|
| 625 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 626 | 626 | } else { |
| 627 | - $type = $tokens[$tokens[$checkToken]['scope_condition']]['type']; |
|
| 628 | - $line = $tokens[$checkToken]['line']; |
|
| 627 | + $type = $tokens[ $tokens[ $checkToken ][ 'scope_condition' ] ][ 'type' ]; |
|
| 628 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 629 | 629 | } |
| 630 | 630 | |
| 631 | - echo "Close scope ($type) on line $line".PHP_EOL; |
|
| 631 | + echo "Close scope ($type) on line $line" . PHP_EOL; |
|
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | $scopeCloser = $checkToken; |
| 635 | - if ($scopeCloser === null) { |
|
| 635 | + if ( $scopeCloser === null ) { |
|
| 636 | 636 | $scopeCloser = $i; |
| 637 | 637 | } |
| 638 | 638 | |
| 639 | - $conditionToken = array_pop($openScopes); |
|
| 640 | - if ($this->debug === true) { |
|
| 641 | - $line = $tokens[$conditionToken]['line']; |
|
| 642 | - $type = $tokens[$conditionToken]['type']; |
|
| 643 | - echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 639 | + $conditionToken = array_pop( $openScopes ); |
|
| 640 | + if ( $this->debug === true ) { |
|
| 641 | + $line = $tokens[ $conditionToken ][ 'line' ]; |
|
| 642 | + $type = $tokens[ $conditionToken ][ 'type' ]; |
|
| 643 | + echo "\t=> removed open scope $conditionToken ($type) on line $line" . PHP_EOL; |
|
| 644 | 644 | } |
| 645 | 645 | |
| 646 | - if (isset($tokens[$scopeCloser]['scope_condition']) === true) { |
|
| 647 | - $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $tokens[$scopeCloser]['scope_condition'], true); |
|
| 648 | - if ($this->debug === true) { |
|
| 649 | - $line = $tokens[$first]['line']; |
|
| 650 | - $type = $tokens[$first]['type']; |
|
| 651 | - echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 646 | + if ( isset( $tokens[ $scopeCloser ][ 'scope_condition' ] ) === true ) { |
|
| 647 | + $first = $phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_INLINE_HTML ], $tokens[ $scopeCloser ][ 'scope_condition' ], true ); |
|
| 648 | + if ( $this->debug === true ) { |
|
| 649 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 650 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 651 | + echo "\t* first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 652 | 652 | } |
| 653 | 653 | |
| 654 | - while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 655 | - && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 654 | + while ( $tokens[ $first ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 655 | + && $tokens[ ( $first - 1 ) ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 656 | 656 | ) { |
| 657 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 658 | - if ($this->debug === true) { |
|
| 659 | - $line = $tokens[$first]['line']; |
|
| 660 | - $type = $tokens[$first]['type']; |
|
| 661 | - echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 657 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, ( $first - 1 ), true ); |
|
| 658 | + if ( $this->debug === true ) { |
|
| 659 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 660 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 661 | + echo "\t* found multi-line string; amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 662 | 662 | } |
| 663 | 663 | } |
| 664 | 664 | |
| 665 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 666 | - if (isset($adjustments[$first]) === true) { |
|
| 667 | - $currentIndent += $adjustments[$first]; |
|
| 665 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 666 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 667 | + $currentIndent += $adjustments[ $first ]; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | - $setIndents[$scopeCloser] = $currentIndent; |
|
| 670 | + $setIndents[ $scopeCloser ] = $currentIndent; |
|
| 671 | 671 | |
| 672 | - if ($this->debug === true) { |
|
| 673 | - $type = $tokens[$scopeCloser]['type']; |
|
| 674 | - echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)".PHP_EOL; |
|
| 672 | + if ( $this->debug === true ) { |
|
| 673 | + $type = $tokens[ $scopeCloser ][ 'type' ]; |
|
| 674 | + echo "\t=> indent set to $currentIndent by token $scopeCloser ($type)" . PHP_EOL; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | // We only check the indent of scope closers if they are |
| 678 | 678 | // curly braces because other constructs tend to have different rules. |
| 679 | - if ($tokens[$scopeCloser]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 679 | + if ( $tokens[ $scopeCloser ][ 'code' ] === T_CLOSE_CURLY_BRACKET ) { |
|
| 680 | 680 | $exact = true; |
| 681 | 681 | } else { |
| 682 | 682 | $checkToken = null; |
@@ -685,93 +685,93 @@ discard block |
||
| 685 | 685 | }//end if |
| 686 | 686 | |
| 687 | 687 | // Handle scope for JS object notation. |
| 688 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 689 | - && (($checkToken !== null |
|
| 690 | - && $tokens[$checkToken]['code'] === T_CLOSE_OBJECT |
|
| 691 | - && $tokens[$checkToken]['line'] !== $tokens[$tokens[$checkToken]['bracket_opener']]['line']) |
|
| 692 | - || ($checkToken === null |
|
| 693 | - && $tokens[$i]['code'] === T_CLOSE_OBJECT |
|
| 694 | - && $tokens[$i]['line'] !== $tokens[$tokens[$i]['bracket_opener']]['line'])) |
|
| 688 | + if ( $phpcsFile->tokenizerType === 'JS' |
|
| 689 | + && ( ( $checkToken !== null |
|
| 690 | + && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_OBJECT |
|
| 691 | + && $tokens[ $checkToken ][ 'line' ] !== $tokens[ $tokens[ $checkToken ][ 'bracket_opener' ] ][ 'line' ] ) |
|
| 692 | + || ( $checkToken === null |
|
| 693 | + && $tokens[ $i ][ 'code' ] === T_CLOSE_OBJECT |
|
| 694 | + && $tokens[ $i ][ 'line' ] !== $tokens[ $tokens[ $i ][ 'bracket_opener' ] ][ 'line' ] ) ) |
|
| 695 | 695 | ) { |
| 696 | - if ($this->debug === true) { |
|
| 697 | - $line = $tokens[$i]['line']; |
|
| 698 | - echo "Close JS object on line $line".PHP_EOL; |
|
| 696 | + if ( $this->debug === true ) { |
|
| 697 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 698 | + echo "Close JS object on line $line" . PHP_EOL; |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | 701 | $scopeCloser = $checkToken; |
| 702 | - if ($scopeCloser === null) { |
|
| 702 | + if ( $scopeCloser === null ) { |
|
| 703 | 703 | $scopeCloser = $i; |
| 704 | 704 | } else { |
| 705 | - $conditionToken = array_pop($openScopes); |
|
| 706 | - if ($this->debug === true) { |
|
| 707 | - $line = $tokens[$conditionToken]['line']; |
|
| 708 | - $type = $tokens[$conditionToken]['type']; |
|
| 709 | - echo "\t=> removed open scope $conditionToken ($type) on line $line".PHP_EOL; |
|
| 705 | + $conditionToken = array_pop( $openScopes ); |
|
| 706 | + if ( $this->debug === true ) { |
|
| 707 | + $line = $tokens[ $conditionToken ][ 'line' ]; |
|
| 708 | + $type = $tokens[ $conditionToken ][ 'type' ]; |
|
| 709 | + echo "\t=> removed open scope $conditionToken ($type) on line $line" . PHP_EOL; |
|
| 710 | 710 | } |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | $parens = 0; |
| 714 | - if (isset($tokens[$scopeCloser]['nested_parenthesis']) === true |
|
| 715 | - && empty($tokens[$scopeCloser]['nested_parenthesis']) === false |
|
| 714 | + if ( isset( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ) === true |
|
| 715 | + && empty( $tokens[ $scopeCloser ][ 'nested_parenthesis' ] ) === false |
|
| 716 | 716 | ) { |
| 717 | - $parens = $tokens[$scopeCloser]['nested_parenthesis']; |
|
| 718 | - end($parens); |
|
| 719 | - $parens = key($parens); |
|
| 720 | - if ($this->debug === true) { |
|
| 721 | - $line = $tokens[$parens]['line']; |
|
| 722 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 717 | + $parens = $tokens[ $scopeCloser ][ 'nested_parenthesis' ]; |
|
| 718 | + end( $parens ); |
|
| 719 | + $parens = key( $parens ); |
|
| 720 | + if ( $this->debug === true ) { |
|
| 721 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 722 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 723 | 723 | } |
| 724 | 724 | } |
| 725 | 725 | |
| 726 | 726 | $condition = 0; |
| 727 | - if (isset($tokens[$scopeCloser]['conditions']) === true |
|
| 728 | - && empty($tokens[$scopeCloser]['conditions']) === false |
|
| 727 | + if ( isset( $tokens[ $scopeCloser ][ 'conditions' ] ) === true |
|
| 728 | + && empty( $tokens[ $scopeCloser ][ 'conditions' ] ) === false |
|
| 729 | 729 | ) { |
| 730 | - $condition = $tokens[$scopeCloser]['conditions']; |
|
| 731 | - end($condition); |
|
| 732 | - $condition = key($condition); |
|
| 733 | - if ($this->debug === true) { |
|
| 734 | - $line = $tokens[$condition]['line']; |
|
| 735 | - $type = $tokens[$condition]['type']; |
|
| 736 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 730 | + $condition = $tokens[ $scopeCloser ][ 'conditions' ]; |
|
| 731 | + end( $condition ); |
|
| 732 | + $condition = key( $condition ); |
|
| 733 | + if ( $this->debug === true ) { |
|
| 734 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 735 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 736 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 737 | 737 | } |
| 738 | 738 | } |
| 739 | 739 | |
| 740 | - if ($parens > $condition) { |
|
| 741 | - if ($this->debug === true) { |
|
| 742 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 740 | + if ( $parens > $condition ) { |
|
| 741 | + if ( $this->debug === true ) { |
|
| 742 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 743 | 743 | } |
| 744 | 744 | |
| 745 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $parens, true); |
|
| 745 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $parens, true ); |
|
| 746 | 746 | $condition = 0; |
| 747 | - } else if ($condition > 0) { |
|
| 748 | - if ($this->debug === true) { |
|
| 749 | - echo "\t* using condition *".PHP_EOL; |
|
| 747 | + } else if ( $condition > 0 ) { |
|
| 748 | + if ( $this->debug === true ) { |
|
| 749 | + echo "\t* using condition *" . PHP_EOL; |
|
| 750 | 750 | } |
| 751 | 751 | |
| 752 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $condition, true); |
|
| 752 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $condition, true ); |
|
| 753 | 753 | $parens = 0; |
| 754 | 754 | } else { |
| 755 | - if ($this->debug === true) { |
|
| 756 | - $line = $tokens[$tokens[$scopeCloser]['bracket_opener']]['line']; |
|
| 757 | - echo "\t* token is not in parenthesis or condition; using opener on line $line *".PHP_EOL; |
|
| 755 | + if ( $this->debug === true ) { |
|
| 756 | + $line = $tokens[ $tokens[ $scopeCloser ][ 'bracket_opener' ] ][ 'line' ]; |
|
| 757 | + echo "\t* token is not in parenthesis or condition; using opener on line $line *" . PHP_EOL; |
|
| 758 | 758 | } |
| 759 | 759 | |
| 760 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$scopeCloser]['bracket_opener'], true); |
|
| 760 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $tokens[ $scopeCloser ][ 'bracket_opener' ], true ); |
|
| 761 | 761 | }//end if |
| 762 | 762 | |
| 763 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 764 | - if (isset($adjustments[$first]) === true) { |
|
| 765 | - $currentIndent += $adjustments[$first]; |
|
| 763 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 764 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 765 | + $currentIndent += $adjustments[ $first ]; |
|
| 766 | 766 | } |
| 767 | 767 | |
| 768 | - if ($parens > 0 || $condition > 0) { |
|
| 769 | - $checkIndent = ($tokens[$first]['column'] - 1); |
|
| 770 | - if (isset($adjustments[$first]) === true) { |
|
| 771 | - $checkIndent += $adjustments[$first]; |
|
| 768 | + if ( $parens > 0 || $condition > 0 ) { |
|
| 769 | + $checkIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 770 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 771 | + $checkIndent += $adjustments[ $first ]; |
|
| 772 | 772 | } |
| 773 | 773 | |
| 774 | - if ($condition > 0) { |
|
| 774 | + if ( $condition > 0 ) { |
|
| 775 | 775 | $checkIndent += $this->indent; |
| 776 | 776 | $currentIndent += $this->indent; |
| 777 | 777 | $exact = true; |
@@ -781,76 +781,76 @@ discard block |
||
| 781 | 781 | } |
| 782 | 782 | |
| 783 | 783 | // Make sure it is divisible by our expected indent. |
| 784 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 785 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 786 | - $setIndents[$first] = $currentIndent; |
|
| 784 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 785 | + $checkIndent = (int)( ceil( $checkIndent / $this->indent ) * $this->indent ); |
|
| 786 | + $setIndents[ $first ] = $currentIndent; |
|
| 787 | 787 | |
| 788 | - if ($this->debug === true) { |
|
| 789 | - $type = $tokens[$first]['type']; |
|
| 790 | - echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 788 | + if ( $this->debug === true ) { |
|
| 789 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 790 | + echo "\t=> checking indent of $checkIndent; main indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 791 | 791 | } |
| 792 | 792 | }//end if |
| 793 | 793 | |
| 794 | - if ($checkToken !== null |
|
| 795 | - && isset(Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true |
|
| 796 | - && in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes, true) === false |
|
| 797 | - && isset($tokens[$checkToken]['scope_opener']) === true |
|
| 794 | + if ( $checkToken !== null |
|
| 795 | + && isset( Tokens::$scopeOpeners[ $tokens[ $checkToken ][ 'code' ] ] ) === true |
|
| 796 | + && in_array( $tokens[ $checkToken ][ 'code' ], $this->nonIndentingScopes, true ) === false |
|
| 797 | + && isset( $tokens[ $checkToken ][ 'scope_opener' ] ) === true |
|
| 798 | 798 | ) { |
| 799 | 799 | $exact = true; |
| 800 | 800 | |
| 801 | 801 | $lastOpener = null; |
| 802 | - if (empty($openScopes) === false) { |
|
| 803 | - end($openScopes); |
|
| 804 | - $lastOpener = current($openScopes); |
|
| 802 | + if ( empty( $openScopes ) === false ) { |
|
| 803 | + end( $openScopes ); |
|
| 804 | + $lastOpener = current( $openScopes ); |
|
| 805 | 805 | } |
| 806 | 806 | |
| 807 | 807 | // A scope opener that shares a closer with another token (like multiple |
| 808 | 808 | // CASEs using the same BREAK) needs to reduce the indent level so its |
| 809 | 809 | // indent is checked correctly. It will then increase the indent again |
| 810 | 810 | // (as all openers do) after being checked. |
| 811 | - if ($lastOpener !== null |
|
| 812 | - && isset($tokens[$lastOpener]['scope_closer']) === true |
|
| 813 | - && $tokens[$lastOpener]['level'] === $tokens[$checkToken]['level'] |
|
| 814 | - && $tokens[$lastOpener]['scope_closer'] === $tokens[$checkToken]['scope_closer'] |
|
| 811 | + if ( $lastOpener !== null |
|
| 812 | + && isset( $tokens[ $lastOpener ][ 'scope_closer' ] ) === true |
|
| 813 | + && $tokens[ $lastOpener ][ 'level' ] === $tokens[ $checkToken ][ 'level' ] |
|
| 814 | + && $tokens[ $lastOpener ][ 'scope_closer' ] === $tokens[ $checkToken ][ 'scope_closer' ] |
|
| 815 | 815 | ) { |
| 816 | 816 | $currentIndent -= $this->indent; |
| 817 | - $setIndents[$lastOpener] = $currentIndent; |
|
| 818 | - if ($this->debug === true) { |
|
| 819 | - $line = $tokens[$i]['line']; |
|
| 820 | - $type = $tokens[$lastOpener]['type']; |
|
| 821 | - echo "Shared closer found on line $line".PHP_EOL; |
|
| 822 | - echo "\t=> indent set to $currentIndent by token $lastOpener ($type)".PHP_EOL; |
|
| 817 | + $setIndents[ $lastOpener ] = $currentIndent; |
|
| 818 | + if ( $this->debug === true ) { |
|
| 819 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 820 | + $type = $tokens[ $lastOpener ][ 'type' ]; |
|
| 821 | + echo "Shared closer found on line $line" . PHP_EOL; |
|
| 822 | + echo "\t=> indent set to $currentIndent by token $lastOpener ($type)" . PHP_EOL; |
|
| 823 | 823 | } |
| 824 | 824 | } |
| 825 | 825 | |
| 826 | - if ($tokens[$checkToken]['code'] === T_CLOSURE |
|
| 826 | + if ( $tokens[ $checkToken ][ 'code' ] === T_CLOSURE |
|
| 827 | 827 | && $tokenIndent > $currentIndent |
| 828 | 828 | ) { |
| 829 | 829 | // The opener is indented more than needed, which is fine. |
| 830 | 830 | // But just check that it is divisible by our expected indent. |
| 831 | - $checkIndent = (int) (ceil($tokenIndent / $this->indent) * $this->indent); |
|
| 831 | + $checkIndent = (int)( ceil( $tokenIndent / $this->indent ) * $this->indent ); |
|
| 832 | 832 | $exact = false; |
| 833 | 833 | |
| 834 | - if ($this->debug === true) { |
|
| 835 | - $line = $tokens[$i]['line']; |
|
| 836 | - echo "Closure found on line $line".PHP_EOL; |
|
| 837 | - echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent".PHP_EOL; |
|
| 834 | + if ( $this->debug === true ) { |
|
| 835 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 836 | + echo "Closure found on line $line" . PHP_EOL; |
|
| 837 | + echo "\t=> checking indent of $checkIndent; main indent remains at $currentIndent" . PHP_EOL; |
|
| 838 | 838 | } |
| 839 | 839 | } |
| 840 | 840 | }//end if |
| 841 | 841 | |
| 842 | 842 | // Method prefix indentation has to be exact or else it will break |
| 843 | 843 | // the rest of the function declaration, and potentially future ones. |
| 844 | - if ($checkToken !== null |
|
| 845 | - && isset(Tokens::$methodPrefixes[$tokens[$checkToken]['code']]) === true |
|
| 846 | - && $tokens[($checkToken + 1)]['code'] !== T_DOUBLE_COLON |
|
| 844 | + if ( $checkToken !== null |
|
| 845 | + && isset( Tokens::$methodPrefixes[ $tokens[ $checkToken ][ 'code' ] ] ) === true |
|
| 846 | + && $tokens[ ( $checkToken + 1 ) ][ 'code' ] !== T_DOUBLE_COLON |
|
| 847 | 847 | ) { |
| 848 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($checkToken + 1), null, true); |
|
| 849 | - if ($next === false || $tokens[$next]['code'] !== T_CLOSURE) { |
|
| 850 | - if ($this->debug === true) { |
|
| 851 | - $line = $tokens[$checkToken]['line']; |
|
| 852 | - $type = $tokens[$checkToken]['type']; |
|
| 853 | - echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL; |
|
| 848 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $checkToken + 1 ), null, true ); |
|
| 849 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== T_CLOSURE ) { |
|
| 850 | + if ( $this->debug === true ) { |
|
| 851 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 852 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 853 | + echo "\t* method prefix ($type) found on line $line; indent set to exact *" . PHP_EOL; |
|
| 854 | 854 | } |
| 855 | 855 | |
| 856 | 856 | $exact = true; |
@@ -859,7 +859,7 @@ discard block |
||
| 859 | 859 | |
| 860 | 860 | // JS property indentation has to be exact or else if will break |
| 861 | 861 | // things like function and object indentation. |
| 862 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_PROPERTY) { |
|
| 862 | + if ( $checkToken !== null && $tokens[ $checkToken ][ 'code' ] === T_PROPERTY ) { |
|
| 863 | 863 | $exact = true; |
| 864 | 864 | } |
| 865 | 865 | |
@@ -867,50 +867,50 @@ discard block |
||
| 867 | 867 | // so they don't cause problems with indent checks for the code |
| 868 | 868 | // within them, but they don't need to line up with the current indent |
| 869 | 869 | // in most cases. |
| 870 | - if ($checkToken !== null |
|
| 871 | - && ($tokens[$checkToken]['code'] === T_OPEN_TAG |
|
| 872 | - || $tokens[$checkToken]['code'] === T_OPEN_TAG_WITH_ECHO) |
|
| 870 | + if ( $checkToken !== null |
|
| 871 | + && ( $tokens[ $checkToken ][ 'code' ] === T_OPEN_TAG |
|
| 872 | + || $tokens[ $checkToken ][ 'code' ] === T_OPEN_TAG_WITH_ECHO ) |
|
| 873 | 873 | ) { |
| 874 | - $checkIndent = ($tokens[$checkToken]['column'] - 1); |
|
| 874 | + $checkIndent = ( $tokens[ $checkToken ][ 'column' ] - 1 ); |
|
| 875 | 875 | |
| 876 | 876 | // If we are re-opening a block that was closed in the same |
| 877 | 877 | // scope as us, then reset the indent back to what the scope opener |
| 878 | 878 | // set instead of using whatever indent this open tag has set. |
| 879 | - if (empty($tokens[$checkToken]['conditions']) === false) { |
|
| 880 | - $close = $phpcsFile->findPrevious(T_CLOSE_TAG, ($checkToken - 1)); |
|
| 881 | - if ($close !== false |
|
| 882 | - && $tokens[$checkToken]['conditions'] === $tokens[$close]['conditions'] |
|
| 879 | + if ( empty( $tokens[ $checkToken ][ 'conditions' ] ) === false ) { |
|
| 880 | + $close = $phpcsFile->findPrevious( T_CLOSE_TAG, ( $checkToken - 1 ) ); |
|
| 881 | + if ( $close !== false |
|
| 882 | + && $tokens[ $checkToken ][ 'conditions' ] === $tokens[ $close ][ 'conditions' ] |
|
| 883 | 883 | ) { |
| 884 | - $conditions = array_keys($tokens[$checkToken]['conditions']); |
|
| 885 | - $lastCondition = array_pop($conditions); |
|
| 886 | - $lastOpener = $tokens[$lastCondition]['scope_opener']; |
|
| 887 | - $lastCloser = $tokens[$lastCondition]['scope_closer']; |
|
| 888 | - if ($tokens[$lastCloser]['line'] !== $tokens[$checkToken]['line'] |
|
| 889 | - && isset($setIndents[$lastOpener]) === true |
|
| 884 | + $conditions = array_keys( $tokens[ $checkToken ][ 'conditions' ] ); |
|
| 885 | + $lastCondition = array_pop( $conditions ); |
|
| 886 | + $lastOpener = $tokens[ $lastCondition ][ 'scope_opener' ]; |
|
| 887 | + $lastCloser = $tokens[ $lastCondition ][ 'scope_closer' ]; |
|
| 888 | + if ( $tokens[ $lastCloser ][ 'line' ] !== $tokens[ $checkToken ][ 'line' ] |
|
| 889 | + && isset( $setIndents[ $lastOpener ] ) === true |
|
| 890 | 890 | ) { |
| 891 | - $checkIndent = $setIndents[$lastOpener]; |
|
| 891 | + $checkIndent = $setIndents[ $lastOpener ]; |
|
| 892 | 892 | } |
| 893 | 893 | } |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 896 | + $checkIndent = (int)( ceil( $checkIndent / $this->indent ) * $this->indent ); |
|
| 897 | 897 | }//end if |
| 898 | 898 | |
| 899 | 899 | // Close tags needs to be indented to exact column positions. |
| 900 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_CLOSE_TAG) { |
|
| 900 | + if ( $checkToken !== null && $tokens[ $checkToken ][ 'code' ] === T_CLOSE_TAG ) { |
|
| 901 | 901 | $exact = true; |
| 902 | 902 | $checkIndent = $currentIndent; |
| 903 | - $checkIndent = (int) (ceil($checkIndent / $this->indent) * $this->indent); |
|
| 903 | + $checkIndent = (int)( ceil( $checkIndent / $this->indent ) * $this->indent ); |
|
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | // Special case for ELSE statements that are not on the same |
| 907 | 907 | // line as the previous IF statements closing brace. They still need |
| 908 | 908 | // to have the same indent or it will break code after the block. |
| 909 | - if ($checkToken !== null && $tokens[$checkToken]['code'] === T_ELSE) { |
|
| 909 | + if ( $checkToken !== null && $tokens[ $checkToken ][ 'code' ] === T_ELSE ) { |
|
| 910 | 910 | $exact = true; |
| 911 | 911 | } |
| 912 | 912 | |
| 913 | - if ($checkIndent === null) { |
|
| 913 | + if ( $checkIndent === null ) { |
|
| 914 | 914 | $checkIndent = $currentIndent; |
| 915 | 915 | } |
| 916 | 916 | |
@@ -924,23 +924,23 @@ discard block |
||
| 924 | 924 | the checking of future lines |
| 925 | 925 | */ |
| 926 | 926 | |
| 927 | - if ($checkToken !== null |
|
| 928 | - && isset($this->ignoreIndentation[$tokens[$checkToken]['code']]) === false |
|
| 929 | - && (($tokenIndent !== $checkIndent && $exact === true) |
|
| 930 | - || ($tokenIndent < $checkIndent && $exact === false)) |
|
| 927 | + if ( $checkToken !== null |
|
| 928 | + && isset( $this->ignoreIndentation[ $tokens[ $checkToken ][ 'code' ] ] ) === false |
|
| 929 | + && ( ( $tokenIndent !== $checkIndent && $exact === true ) |
|
| 930 | + || ( $tokenIndent < $checkIndent && $exact === false ) ) |
|
| 931 | 931 | ) { |
| 932 | 932 | $type = 'IncorrectExact'; |
| 933 | 933 | $error = 'Line indented incorrectly; expected '; |
| 934 | - if ($exact === false) { |
|
| 934 | + if ( $exact === false ) { |
|
| 935 | 935 | $error .= 'at least '; |
| 936 | 936 | $type = 'Incorrect'; |
| 937 | 937 | } |
| 938 | 938 | |
| 939 | - if ($this->tabIndent === true) { |
|
| 939 | + if ( $this->tabIndent === true ) { |
|
| 940 | 940 | $error .= '%s tabs, found %s'; |
| 941 | 941 | $data = [ |
| 942 | - floor($checkIndent / $this->tabWidth), |
|
| 943 | - floor($tokenIndent / $this->tabWidth), |
|
| 942 | + floor( $checkIndent / $this->tabWidth ), |
|
| 943 | + floor( $tokenIndent / $this->tabWidth ), |
|
| 944 | 944 | ]; |
| 945 | 945 | } else { |
| 946 | 946 | $error .= '%s spaces, found %s'; |
@@ -950,89 +950,89 @@ discard block |
||
| 950 | 950 | ]; |
| 951 | 951 | } |
| 952 | 952 | |
| 953 | - if ($this->debug === true) { |
|
| 954 | - $line = $tokens[$checkToken]['line']; |
|
| 955 | - $message = vsprintf($error, $data); |
|
| 956 | - echo "[Line $line] $message".PHP_EOL; |
|
| 953 | + if ( $this->debug === true ) { |
|
| 954 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 955 | + $message = vsprintf( $error, $data ); |
|
| 956 | + echo "[Line $line] $message" . PHP_EOL; |
|
| 957 | 957 | } |
| 958 | 958 | |
| 959 | 959 | // Assume the change would be applied and continue |
| 960 | 960 | // checking indents under this assumption. This gives more |
| 961 | 961 | // technically accurate error messages. |
| 962 | - $adjustments[$checkToken] = ($checkIndent - $tokenIndent); |
|
| 962 | + $adjustments[ $checkToken ] = ( $checkIndent - $tokenIndent ); |
|
| 963 | 963 | |
| 964 | - $fix = $phpcsFile->addFixableError($error, $checkToken, $type, $data); |
|
| 965 | - if ($fix === true || $this->debug === true) { |
|
| 966 | - $accepted = $this->adjustIndent($phpcsFile, $checkToken, $checkIndent, ($checkIndent - $tokenIndent)); |
|
| 964 | + $fix = $phpcsFile->addFixableError( $error, $checkToken, $type, $data ); |
|
| 965 | + if ( $fix === true || $this->debug === true ) { |
|
| 966 | + $accepted = $this->adjustIndent( $phpcsFile, $checkToken, $checkIndent, ( $checkIndent - $tokenIndent ) ); |
|
| 967 | 967 | |
| 968 | - if ($accepted === true && $this->debug === true) { |
|
| 969 | - $line = $tokens[$checkToken]['line']; |
|
| 970 | - $type = $tokens[$checkToken]['type']; |
|
| 971 | - echo "\t=> add adjustment of ".$adjustments[$checkToken]." for token $checkToken ($type) on line $line".PHP_EOL; |
|
| 968 | + if ( $accepted === true && $this->debug === true ) { |
|
| 969 | + $line = $tokens[ $checkToken ][ 'line' ]; |
|
| 970 | + $type = $tokens[ $checkToken ][ 'type' ]; |
|
| 971 | + echo "\t=> add adjustment of " . $adjustments[ $checkToken ] . " for token $checkToken ($type) on line $line" . PHP_EOL; |
|
| 972 | 972 | } |
| 973 | 973 | } |
| 974 | 974 | }//end if |
| 975 | 975 | |
| 976 | - if ($checkToken !== null) { |
|
| 976 | + if ( $checkToken !== null ) { |
|
| 977 | 977 | $i = $checkToken; |
| 978 | 978 | } |
| 979 | 979 | |
| 980 | 980 | // Completely skip here/now docs as the indent is a part of the |
| 981 | 981 | // content itself. |
| 982 | - if ($tokens[$i]['code'] === T_START_HEREDOC |
|
| 983 | - || $tokens[$i]['code'] === T_START_NOWDOC |
|
| 982 | + if ( $tokens[ $i ][ 'code' ] === T_START_HEREDOC |
|
| 983 | + || $tokens[ $i ][ 'code' ] === T_START_NOWDOC |
|
| 984 | 984 | ) { |
| 985 | - $i = $phpcsFile->findNext([T_END_HEREDOC, T_END_NOWDOC], ($i + 1)); |
|
| 986 | - $i = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
| 985 | + $i = $phpcsFile->findNext( [ T_END_HEREDOC, T_END_NOWDOC ], ( $i + 1 ) ); |
|
| 986 | + $i = $phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), null, true ); |
|
| 987 | 987 | continue; |
| 988 | 988 | } |
| 989 | 989 | |
| 990 | 990 | // Completely skip multi-line strings as the indent is a part of the |
| 991 | 991 | // content itself. |
| 992 | - if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 993 | - || $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING |
|
| 992 | + if ( $tokens[ $i ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 993 | + || $tokens[ $i ][ 'code' ] === T_DOUBLE_QUOTED_STRING |
|
| 994 | 994 | ) { |
| 995 | - $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
|
| 995 | + $i = $phpcsFile->findNext( $tokens[ $i ][ 'code' ], ( $i + 1 ), null, true ); |
|
| 996 | 996 | $i--; |
| 997 | 997 | continue; |
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | // Completely skip doc comments as they tend to have complex |
| 1001 | 1001 | // indentation rules. |
| 1002 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1003 | - $i = $tokens[$i]['comment_closer']; |
|
| 1002 | + if ( $tokens[ $i ][ 'code' ] === T_DOC_COMMENT_OPEN_TAG ) { |
|
| 1003 | + $i = $tokens[ $i ][ 'comment_closer' ]; |
|
| 1004 | 1004 | continue; |
| 1005 | 1005 | } |
| 1006 | 1006 | |
| 1007 | 1007 | // Open tags reset the indent level. |
| 1008 | - if ($tokens[$i]['code'] === T_OPEN_TAG |
|
| 1009 | - || $tokens[$i]['code'] === T_OPEN_TAG_WITH_ECHO |
|
| 1008 | + if ( $tokens[ $i ][ 'code' ] === T_OPEN_TAG |
|
| 1009 | + || $tokens[ $i ][ 'code' ] === T_OPEN_TAG_WITH_ECHO |
|
| 1010 | 1010 | ) { |
| 1011 | - if ($this->debug === true) { |
|
| 1012 | - $line = $tokens[$i]['line']; |
|
| 1013 | - echo "Open PHP tag found on line $line".PHP_EOL; |
|
| 1011 | + if ( $this->debug === true ) { |
|
| 1012 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1013 | + echo "Open PHP tag found on line $line" . PHP_EOL; |
|
| 1014 | 1014 | } |
| 1015 | 1015 | |
| 1016 | - if ($checkToken === null) { |
|
| 1017 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1018 | - $currentIndent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); |
|
| 1016 | + if ( $checkToken === null ) { |
|
| 1017 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 1018 | + $currentIndent = ( strlen( $tokens[ $first ][ 'content' ] ) - strlen( ltrim( $tokens[ $first ][ 'content' ] ) ) ); |
|
| 1019 | 1019 | } else { |
| 1020 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1020 | + $currentIndent = ( $tokens[ $i ][ 'column' ] - 1 ); |
|
| 1021 | 1021 | } |
| 1022 | 1022 | |
| 1023 | 1023 | $lastOpenTag = $i; |
| 1024 | 1024 | |
| 1025 | - if (isset($adjustments[$i]) === true) { |
|
| 1026 | - $currentIndent += $adjustments[$i]; |
|
| 1025 | + if ( isset( $adjustments[ $i ] ) === true ) { |
|
| 1026 | + $currentIndent += $adjustments[ $i ]; |
|
| 1027 | 1027 | } |
| 1028 | 1028 | |
| 1029 | 1029 | // Make sure it is divisible by our expected indent. |
| 1030 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1031 | - $setIndents[$i] = $currentIndent; |
|
| 1030 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1031 | + $setIndents[ $i ] = $currentIndent; |
|
| 1032 | 1032 | |
| 1033 | - if ($this->debug === true) { |
|
| 1034 | - $type = $tokens[$i]['type']; |
|
| 1035 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1033 | + if ( $this->debug === true ) { |
|
| 1034 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1035 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1036 | 1036 | } |
| 1037 | 1037 | |
| 1038 | 1038 | continue; |
@@ -1040,149 +1040,149 @@ discard block |
||
| 1040 | 1040 | |
| 1041 | 1041 | // Close tags reset the indent level, unless they are closing a tag |
| 1042 | 1042 | // opened on the same line. |
| 1043 | - if ($tokens[$i]['code'] === T_CLOSE_TAG) { |
|
| 1044 | - if ($this->debug === true) { |
|
| 1045 | - $line = $tokens[$i]['line']; |
|
| 1046 | - echo "Close PHP tag found on line $line".PHP_EOL; |
|
| 1043 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSE_TAG ) { |
|
| 1044 | + if ( $this->debug === true ) { |
|
| 1045 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1046 | + echo "Close PHP tag found on line $line" . PHP_EOL; |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | - if ($tokens[$lastOpenTag]['line'] !== $tokens[$i]['line']) { |
|
| 1050 | - $currentIndent = ($tokens[$i]['column'] - 1); |
|
| 1049 | + if ( $tokens[ $lastOpenTag ][ 'line' ] !== $tokens[ $i ][ 'line' ] ) { |
|
| 1050 | + $currentIndent = ( $tokens[ $i ][ 'column' ] - 1 ); |
|
| 1051 | 1051 | $lastCloseTag = $i; |
| 1052 | 1052 | } else { |
| 1053 | - if ($lastCloseTag === null) { |
|
| 1053 | + if ( $lastCloseTag === null ) { |
|
| 1054 | 1054 | $currentIndent = 0; |
| 1055 | 1055 | } else { |
| 1056 | - $currentIndent = ($tokens[$lastCloseTag]['column'] - 1); |
|
| 1056 | + $currentIndent = ( $tokens[ $lastCloseTag ][ 'column' ] - 1 ); |
|
| 1057 | 1057 | } |
| 1058 | 1058 | } |
| 1059 | 1059 | |
| 1060 | - if (isset($adjustments[$i]) === true) { |
|
| 1061 | - $currentIndent += $adjustments[$i]; |
|
| 1060 | + if ( isset( $adjustments[ $i ] ) === true ) { |
|
| 1061 | + $currentIndent += $adjustments[ $i ]; |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | // Make sure it is divisible by our expected indent. |
| 1065 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1066 | - $setIndents[$i] = $currentIndent; |
|
| 1065 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1066 | + $setIndents[ $i ] = $currentIndent; |
|
| 1067 | 1067 | |
| 1068 | - if ($this->debug === true) { |
|
| 1069 | - $type = $tokens[$i]['type']; |
|
| 1070 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1068 | + if ( $this->debug === true ) { |
|
| 1069 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1070 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | continue; |
| 1074 | 1074 | }//end if |
| 1075 | 1075 | |
| 1076 | 1076 | // Anon classes and functions set the indent based on their own indent level. |
| 1077 | - if ($tokens[$i]['code'] === T_CLOSURE || $tokens[$i]['code'] === T_ANON_CLASS) { |
|
| 1078 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1079 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1080 | - if ($this->debug === true) { |
|
| 1081 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1082 | - $line = $tokens[$i]['line']; |
|
| 1083 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1077 | + if ( $tokens[ $i ][ 'code' ] === T_CLOSURE || $tokens[ $i ][ 'code' ] === T_ANON_CLASS ) { |
|
| 1078 | + $closer = $tokens[ $i ][ 'scope_closer' ]; |
|
| 1079 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 1080 | + if ( $this->debug === true ) { |
|
| 1081 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $i ][ 'type' ], 2 ) ) ); |
|
| 1082 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1083 | + echo "* ignoring single-line $type on line $line" . PHP_EOL; |
|
| 1084 | 1084 | } |
| 1085 | 1085 | |
| 1086 | 1086 | $i = $closer; |
| 1087 | 1087 | continue; |
| 1088 | 1088 | } |
| 1089 | 1089 | |
| 1090 | - if ($this->debug === true) { |
|
| 1091 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
|
| 1092 | - $line = $tokens[$i]['line']; |
|
| 1093 | - echo "Open $type on line $line".PHP_EOL; |
|
| 1090 | + if ( $this->debug === true ) { |
|
| 1091 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $i ][ 'type' ], 2 ) ) ); |
|
| 1092 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1093 | + echo "Open $type on line $line" . PHP_EOL; |
|
| 1094 | 1094 | } |
| 1095 | 1095 | |
| 1096 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1097 | - if ($this->debug === true) { |
|
| 1098 | - $line = $tokens[$first]['line']; |
|
| 1099 | - $type = $tokens[$first]['type']; |
|
| 1100 | - echo "\t* first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1096 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 1097 | + if ( $this->debug === true ) { |
|
| 1098 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1099 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1100 | + echo "\t* first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 1101 | 1101 | } |
| 1102 | 1102 | |
| 1103 | - while ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1104 | - && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING |
|
| 1103 | + while ( $tokens[ $first ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 1104 | + && $tokens[ ( $first - 1 ) ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING |
|
| 1105 | 1105 | ) { |
| 1106 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); |
|
| 1107 | - if ($this->debug === true) { |
|
| 1108 | - $line = $tokens[$first]['line']; |
|
| 1109 | - $type = $tokens[$first]['type']; |
|
| 1110 | - echo "\t* found multi-line string; amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1106 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, ( $first - 1 ), true ); |
|
| 1107 | + if ( $this->debug === true ) { |
|
| 1108 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1109 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1110 | + echo "\t* found multi-line string; amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 1111 | 1111 | } |
| 1112 | 1112 | } |
| 1113 | 1113 | |
| 1114 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1115 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1116 | - if ($this->debug === true) { |
|
| 1117 | - $closerToken = $tokens[$i]['scope_closer']; |
|
| 1118 | - $closerLine = $tokens[$closerToken]['line']; |
|
| 1119 | - $closerType = $tokens[$closerToken]['type']; |
|
| 1120 | - $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1121 | - $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1122 | - $conditionType = $tokens[$conditionToken]['type']; |
|
| 1123 | - echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1114 | + $currentIndent = ( ( $tokens[ $first ][ 'column' ] - 1 ) + $this->indent ); |
|
| 1115 | + $openScopes[ $tokens[ $i ][ 'scope_closer' ] ] = $tokens[ $i ][ 'scope_condition' ]; |
|
| 1116 | + if ( $this->debug === true ) { |
|
| 1117 | + $closerToken = $tokens[ $i ][ 'scope_closer' ]; |
|
| 1118 | + $closerLine = $tokens[ $closerToken ][ 'line' ]; |
|
| 1119 | + $closerType = $tokens[ $closerToken ][ 'type' ]; |
|
| 1120 | + $conditionToken = $tokens[ $i ][ 'scope_condition' ]; |
|
| 1121 | + $conditionLine = $tokens[ $conditionToken ][ 'line' ]; |
|
| 1122 | + $conditionType = $tokens[ $conditionToken ][ 'type' ]; |
|
| 1123 | + echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine" . PHP_EOL; |
|
| 1124 | 1124 | } |
| 1125 | 1125 | |
| 1126 | - if (isset($adjustments[$first]) === true) { |
|
| 1127 | - $currentIndent += $adjustments[$first]; |
|
| 1126 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 1127 | + $currentIndent += $adjustments[ $first ]; |
|
| 1128 | 1128 | } |
| 1129 | 1129 | |
| 1130 | 1130 | // Make sure it is divisible by our expected indent. |
| 1131 | - $currentIndent = (int) (floor($currentIndent / $this->indent) * $this->indent); |
|
| 1132 | - $i = $tokens[$i]['scope_opener']; |
|
| 1133 | - $setIndents[$i] = $currentIndent; |
|
| 1131 | + $currentIndent = (int)( floor( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1132 | + $i = $tokens[ $i ][ 'scope_opener' ]; |
|
| 1133 | + $setIndents[ $i ] = $currentIndent; |
|
| 1134 | 1134 | |
| 1135 | - if ($this->debug === true) { |
|
| 1136 | - $type = $tokens[$i]['type']; |
|
| 1137 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1135 | + if ( $this->debug === true ) { |
|
| 1136 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1137 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1138 | 1138 | } |
| 1139 | 1139 | |
| 1140 | 1140 | continue; |
| 1141 | 1141 | }//end if |
| 1142 | 1142 | |
| 1143 | 1143 | // Scope openers increase the indent level. |
| 1144 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1145 | - && isset($tokens[$i]['scope_opener']) === true |
|
| 1146 | - && $tokens[$i]['scope_opener'] === $i |
|
| 1144 | + if ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 1145 | + && isset( $tokens[ $i ][ 'scope_opener' ] ) === true |
|
| 1146 | + && $tokens[ $i ][ 'scope_opener' ] === $i |
|
| 1147 | 1147 | ) { |
| 1148 | - $closer = $tokens[$i]['scope_closer']; |
|
| 1149 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1150 | - if ($this->debug === true) { |
|
| 1151 | - $line = $tokens[$i]['line']; |
|
| 1152 | - $type = $tokens[$i]['type']; |
|
| 1153 | - echo "* ignoring single-line $type on line $line".PHP_EOL; |
|
| 1148 | + $closer = $tokens[ $i ][ 'scope_closer' ]; |
|
| 1149 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 1150 | + if ( $this->debug === true ) { |
|
| 1151 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1152 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1153 | + echo "* ignoring single-line $type on line $line" . PHP_EOL; |
|
| 1154 | 1154 | } |
| 1155 | 1155 | |
| 1156 | 1156 | $i = $closer; |
| 1157 | 1157 | continue; |
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | - $condition = $tokens[$tokens[$i]['scope_condition']]['code']; |
|
| 1161 | - if (isset(Tokens::$scopeOpeners[$condition]) === true |
|
| 1162 | - && in_array($condition, $this->nonIndentingScopes, true) === false |
|
| 1160 | + $condition = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ]; |
|
| 1161 | + if ( isset( Tokens::$scopeOpeners[ $condition ] ) === true |
|
| 1162 | + && in_array( $condition, $this->nonIndentingScopes, true ) === false |
|
| 1163 | 1163 | ) { |
| 1164 | - if ($this->debug === true) { |
|
| 1165 | - $line = $tokens[$i]['line']; |
|
| 1166 | - $type = $tokens[$tokens[$i]['scope_condition']]['type']; |
|
| 1167 | - echo "Open scope ($type) on line $line".PHP_EOL; |
|
| 1164 | + if ( $this->debug === true ) { |
|
| 1165 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1166 | + $type = $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ]; |
|
| 1167 | + echo "Open scope ($type) on line $line" . PHP_EOL; |
|
| 1168 | 1168 | } |
| 1169 | 1169 | |
| 1170 | 1170 | $currentIndent += $this->indent; |
| 1171 | - $setIndents[$i] = $currentIndent; |
|
| 1172 | - $openScopes[$tokens[$i]['scope_closer']] = $tokens[$i]['scope_condition']; |
|
| 1173 | - if ($this->debug === true) { |
|
| 1174 | - $closerToken = $tokens[$i]['scope_closer']; |
|
| 1175 | - $closerLine = $tokens[$closerToken]['line']; |
|
| 1176 | - $closerType = $tokens[$closerToken]['type']; |
|
| 1177 | - $conditionToken = $tokens[$i]['scope_condition']; |
|
| 1178 | - $conditionLine = $tokens[$conditionToken]['line']; |
|
| 1179 | - $conditionType = $tokens[$conditionToken]['type']; |
|
| 1180 | - echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine".PHP_EOL; |
|
| 1171 | + $setIndents[ $i ] = $currentIndent; |
|
| 1172 | + $openScopes[ $tokens[ $i ][ 'scope_closer' ] ] = $tokens[ $i ][ 'scope_condition' ]; |
|
| 1173 | + if ( $this->debug === true ) { |
|
| 1174 | + $closerToken = $tokens[ $i ][ 'scope_closer' ]; |
|
| 1175 | + $closerLine = $tokens[ $closerToken ][ 'line' ]; |
|
| 1176 | + $closerType = $tokens[ $closerToken ][ 'type' ]; |
|
| 1177 | + $conditionToken = $tokens[ $i ][ 'scope_condition' ]; |
|
| 1178 | + $conditionLine = $tokens[ $conditionToken ][ 'line' ]; |
|
| 1179 | + $conditionType = $tokens[ $conditionToken ][ 'type' ]; |
|
| 1180 | + echo "\t=> added open scope $closerToken ($closerType) on line $closerLine, pointing to condition $conditionToken ($conditionType) on line $conditionLine" . PHP_EOL; |
|
| 1181 | 1181 | } |
| 1182 | 1182 | |
| 1183 | - if ($this->debug === true) { |
|
| 1184 | - $type = $tokens[$i]['type']; |
|
| 1185 | - echo "\t=> indent set to $currentIndent by token $i ($type)".PHP_EOL; |
|
| 1183 | + if ( $this->debug === true ) { |
|
| 1184 | + $type = $tokens[ $i ][ 'type' ]; |
|
| 1185 | + echo "\t=> indent set to $currentIndent by token $i ($type)" . PHP_EOL; |
|
| 1186 | 1186 | } |
| 1187 | 1187 | |
| 1188 | 1188 | continue; |
@@ -1190,120 +1190,120 @@ discard block |
||
| 1190 | 1190 | }//end if |
| 1191 | 1191 | |
| 1192 | 1192 | // JS objects set the indent level. |
| 1193 | - if ($phpcsFile->tokenizerType === 'JS' |
|
| 1194 | - && $tokens[$i]['code'] === T_OBJECT |
|
| 1193 | + if ( $phpcsFile->tokenizerType === 'JS' |
|
| 1194 | + && $tokens[ $i ][ 'code' ] === T_OBJECT |
|
| 1195 | 1195 | ) { |
| 1196 | - $closer = $tokens[$i]['bracket_closer']; |
|
| 1197 | - if ($tokens[$i]['line'] === $tokens[$closer]['line']) { |
|
| 1198 | - if ($this->debug === true) { |
|
| 1199 | - $line = $tokens[$i]['line']; |
|
| 1200 | - echo "* ignoring single-line JS object on line $line".PHP_EOL; |
|
| 1196 | + $closer = $tokens[ $i ][ 'bracket_closer' ]; |
|
| 1197 | + if ( $tokens[ $i ][ 'line' ] === $tokens[ $closer ][ 'line' ] ) { |
|
| 1198 | + if ( $this->debug === true ) { |
|
| 1199 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1200 | + echo "* ignoring single-line JS object on line $line" . PHP_EOL; |
|
| 1201 | 1201 | } |
| 1202 | 1202 | |
| 1203 | 1203 | $i = $closer; |
| 1204 | 1204 | continue; |
| 1205 | 1205 | } |
| 1206 | 1206 | |
| 1207 | - if ($this->debug === true) { |
|
| 1208 | - $line = $tokens[$i]['line']; |
|
| 1209 | - echo "Open JS object on line $line".PHP_EOL; |
|
| 1207 | + if ( $this->debug === true ) { |
|
| 1208 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1209 | + echo "Open JS object on line $line" . PHP_EOL; |
|
| 1210 | 1210 | } |
| 1211 | 1211 | |
| 1212 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
|
| 1213 | - $currentIndent = (($tokens[$first]['column'] - 1) + $this->indent); |
|
| 1214 | - if (isset($adjustments[$first]) === true) { |
|
| 1215 | - $currentIndent += $adjustments[$first]; |
|
| 1212 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $i, true ); |
|
| 1213 | + $currentIndent = ( ( $tokens[ $first ][ 'column' ] - 1 ) + $this->indent ); |
|
| 1214 | + if ( isset( $adjustments[ $first ] ) === true ) { |
|
| 1215 | + $currentIndent += $adjustments[ $first ]; |
|
| 1216 | 1216 | } |
| 1217 | 1217 | |
| 1218 | 1218 | // Make sure it is divisible by our expected indent. |
| 1219 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1220 | - $setIndents[$first] = $currentIndent; |
|
| 1219 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1220 | + $setIndents[ $first ] = $currentIndent; |
|
| 1221 | 1221 | |
| 1222 | - if ($this->debug === true) { |
|
| 1223 | - $type = $tokens[$first]['type']; |
|
| 1224 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1222 | + if ( $this->debug === true ) { |
|
| 1223 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1224 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 1225 | 1225 | } |
| 1226 | 1226 | |
| 1227 | 1227 | continue; |
| 1228 | 1228 | }//end if |
| 1229 | 1229 | |
| 1230 | 1230 | // Closing an anon class or function. |
| 1231 | - if (isset($tokens[$i]['scope_condition']) === true |
|
| 1232 | - && $tokens[$i]['scope_closer'] === $i |
|
| 1233 | - && ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE |
|
| 1234 | - || $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS) |
|
| 1231 | + if ( isset( $tokens[ $i ][ 'scope_condition' ] ) === true |
|
| 1232 | + && $tokens[ $i ][ 'scope_closer' ] === $i |
|
| 1233 | + && ( $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ] === T_CLOSURE |
|
| 1234 | + || $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'code' ] === T_ANON_CLASS ) |
|
| 1235 | 1235 | ) { |
| 1236 | - if ($this->debug === true) { |
|
| 1237 | - $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
|
| 1238 | - $line = $tokens[$i]['line']; |
|
| 1239 | - echo "Close $type on line $line".PHP_EOL; |
|
| 1236 | + if ( $this->debug === true ) { |
|
| 1237 | + $type = str_replace( '_', ' ', strtolower( substr( $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ], 2 ) ) ); |
|
| 1238 | + $line = $tokens[ $i ][ 'line' ]; |
|
| 1239 | + echo "Close $type on line $line" . PHP_EOL; |
|
| 1240 | 1240 | } |
| 1241 | 1241 | |
| 1242 | 1242 | $prev = false; |
| 1243 | 1243 | |
| 1244 | 1244 | $object = 0; |
| 1245 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 1246 | - $conditions = $tokens[$i]['conditions']; |
|
| 1247 | - krsort($conditions, SORT_NUMERIC); |
|
| 1248 | - foreach ($conditions as $token => $condition) { |
|
| 1249 | - if ($condition === T_OBJECT) { |
|
| 1245 | + if ( $phpcsFile->tokenizerType === 'JS' ) { |
|
| 1246 | + $conditions = $tokens[ $i ][ 'conditions' ]; |
|
| 1247 | + krsort( $conditions, SORT_NUMERIC ); |
|
| 1248 | + foreach ( $conditions as $token => $condition ) { |
|
| 1249 | + if ( $condition === T_OBJECT ) { |
|
| 1250 | 1250 | $object = $token; |
| 1251 | 1251 | break; |
| 1252 | 1252 | } |
| 1253 | 1253 | } |
| 1254 | 1254 | |
| 1255 | - if ($this->debug === true && $object !== 0) { |
|
| 1256 | - $line = $tokens[$object]['line']; |
|
| 1257 | - echo "\t* token is inside JS object $object on line $line *".PHP_EOL; |
|
| 1255 | + if ( $this->debug === true && $object !== 0 ) { |
|
| 1256 | + $line = $tokens[ $object ][ 'line' ]; |
|
| 1257 | + echo "\t* token is inside JS object $object on line $line *" . PHP_EOL; |
|
| 1258 | 1258 | } |
| 1259 | 1259 | } |
| 1260 | 1260 | |
| 1261 | 1261 | $parens = 0; |
| 1262 | - if (isset($tokens[$i]['nested_parenthesis']) === true |
|
| 1263 | - && empty($tokens[$i]['nested_parenthesis']) === false |
|
| 1262 | + if ( isset( $tokens[ $i ][ 'nested_parenthesis' ] ) === true |
|
| 1263 | + && empty( $tokens[ $i ][ 'nested_parenthesis' ] ) === false |
|
| 1264 | 1264 | ) { |
| 1265 | - $parens = $tokens[$i]['nested_parenthesis']; |
|
| 1266 | - end($parens); |
|
| 1267 | - $parens = key($parens); |
|
| 1268 | - if ($this->debug === true) { |
|
| 1269 | - $line = $tokens[$parens]['line']; |
|
| 1270 | - echo "\t* token has nested parenthesis $parens on line $line *".PHP_EOL; |
|
| 1265 | + $parens = $tokens[ $i ][ 'nested_parenthesis' ]; |
|
| 1266 | + end( $parens ); |
|
| 1267 | + $parens = key( $parens ); |
|
| 1268 | + if ( $this->debug === true ) { |
|
| 1269 | + $line = $tokens[ $parens ][ 'line' ]; |
|
| 1270 | + echo "\t* token has nested parenthesis $parens on line $line *" . PHP_EOL; |
|
| 1271 | 1271 | } |
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | $condition = 0; |
| 1275 | - if (isset($tokens[$i]['conditions']) === true |
|
| 1276 | - && empty($tokens[$i]['conditions']) === false |
|
| 1275 | + if ( isset( $tokens[ $i ][ 'conditions' ] ) === true |
|
| 1276 | + && empty( $tokens[ $i ][ 'conditions' ] ) === false |
|
| 1277 | 1277 | ) { |
| 1278 | - $condition = $tokens[$i]['conditions']; |
|
| 1279 | - end($condition); |
|
| 1280 | - $condition = key($condition); |
|
| 1281 | - if ($this->debug === true) { |
|
| 1282 | - $line = $tokens[$condition]['line']; |
|
| 1283 | - $type = $tokens[$condition]['type']; |
|
| 1284 | - echo "\t* token is inside condition $condition ($type) on line $line *".PHP_EOL; |
|
| 1278 | + $condition = $tokens[ $i ][ 'conditions' ]; |
|
| 1279 | + end( $condition ); |
|
| 1280 | + $condition = key( $condition ); |
|
| 1281 | + if ( $this->debug === true ) { |
|
| 1282 | + $line = $tokens[ $condition ][ 'line' ]; |
|
| 1283 | + $type = $tokens[ $condition ][ 'type' ]; |
|
| 1284 | + echo "\t* token is inside condition $condition ($type) on line $line *" . PHP_EOL; |
|
| 1285 | 1285 | } |
| 1286 | 1286 | } |
| 1287 | 1287 | |
| 1288 | - if ($parens > $object && $parens > $condition) { |
|
| 1289 | - if ($this->debug === true) { |
|
| 1290 | - echo "\t* using parenthesis *".PHP_EOL; |
|
| 1288 | + if ( $parens > $object && $parens > $condition ) { |
|
| 1289 | + if ( $this->debug === true ) { |
|
| 1290 | + echo "\t* using parenthesis *" . PHP_EOL; |
|
| 1291 | 1291 | } |
| 1292 | 1292 | |
| 1293 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($parens - 1), null, true); |
|
| 1293 | + $prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $parens - 1 ), null, true ); |
|
| 1294 | 1294 | $object = 0; |
| 1295 | 1295 | $condition = 0; |
| 1296 | - } else if ($object > 0 && $object >= $condition) { |
|
| 1297 | - if ($this->debug === true) { |
|
| 1298 | - echo "\t* using object *".PHP_EOL; |
|
| 1296 | + } else if ( $object > 0 && $object >= $condition ) { |
|
| 1297 | + if ( $this->debug === true ) { |
|
| 1298 | + echo "\t* using object *" . PHP_EOL; |
|
| 1299 | 1299 | } |
| 1300 | 1300 | |
| 1301 | 1301 | $prev = $object; |
| 1302 | 1302 | $parens = 0; |
| 1303 | 1303 | $condition = 0; |
| 1304 | - } else if ($condition > 0) { |
|
| 1305 | - if ($this->debug === true) { |
|
| 1306 | - echo "\t* using condition *".PHP_EOL; |
|
| 1304 | + } else if ( $condition > 0 ) { |
|
| 1305 | + if ( $this->debug === true ) { |
|
| 1306 | + echo "\t* using condition *" . PHP_EOL; |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | $prev = $condition; |
@@ -1311,72 +1311,72 @@ discard block |
||
| 1311 | 1311 | $parens = 0; |
| 1312 | 1312 | }//end if |
| 1313 | 1313 | |
| 1314 | - if ($prev === false) { |
|
| 1315 | - $prev = $phpcsFile->findPrevious([T_EQUAL, T_RETURN], ($tokens[$i]['scope_condition'] - 1), null, false, null, true); |
|
| 1316 | - if ($prev === false) { |
|
| 1314 | + if ( $prev === false ) { |
|
| 1315 | + $prev = $phpcsFile->findPrevious( [ T_EQUAL, T_RETURN ], ( $tokens[ $i ][ 'scope_condition' ] - 1 ), null, false, null, true ); |
|
| 1316 | + if ( $prev === false ) { |
|
| 1317 | 1317 | $prev = $i; |
| 1318 | - if ($this->debug === true) { |
|
| 1319 | - echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *".PHP_EOL; |
|
| 1318 | + if ( $this->debug === true ) { |
|
| 1319 | + echo "\t* could not find a previous T_EQUAL or T_RETURN token; will use current token *" . PHP_EOL; |
|
| 1320 | 1320 | } |
| 1321 | 1321 | } |
| 1322 | 1322 | } |
| 1323 | 1323 | |
| 1324 | - if ($this->debug === true) { |
|
| 1325 | - $line = $tokens[$prev]['line']; |
|
| 1326 | - $type = $tokens[$prev]['type']; |
|
| 1327 | - echo "\t* previous token is $type on line $line *".PHP_EOL; |
|
| 1324 | + if ( $this->debug === true ) { |
|
| 1325 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 1326 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 1327 | + echo "\t* previous token is $type on line $line *" . PHP_EOL; |
|
| 1328 | 1328 | } |
| 1329 | 1329 | |
| 1330 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1331 | - if ($this->debug === true) { |
|
| 1332 | - $line = $tokens[$first]['line']; |
|
| 1333 | - $type = $tokens[$first]['type']; |
|
| 1334 | - echo "\t* first token on line $line is $first ($type) *".PHP_EOL; |
|
| 1330 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 1331 | + if ( $this->debug === true ) { |
|
| 1332 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1333 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1334 | + echo "\t* first token on line $line is $first ($type) *" . PHP_EOL; |
|
| 1335 | 1335 | } |
| 1336 | 1336 | |
| 1337 | - $prev = $phpcsFile->findStartOfStatement($first); |
|
| 1338 | - if ($prev !== $first) { |
|
| 1337 | + $prev = $phpcsFile->findStartOfStatement( $first ); |
|
| 1338 | + if ( $prev !== $first ) { |
|
| 1339 | 1339 | // This is not the start of the statement. |
| 1340 | - if ($this->debug === true) { |
|
| 1341 | - $line = $tokens[$prev]['line']; |
|
| 1342 | - $type = $tokens[$prev]['type']; |
|
| 1343 | - echo "\t* amended previous is $type on line $line *".PHP_EOL; |
|
| 1340 | + if ( $this->debug === true ) { |
|
| 1341 | + $line = $tokens[ $prev ][ 'line' ]; |
|
| 1342 | + $type = $tokens[ $prev ][ 'type' ]; |
|
| 1343 | + echo "\t* amended previous is $type on line $line *" . PHP_EOL; |
|
| 1344 | 1344 | } |
| 1345 | 1345 | |
| 1346 | - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true); |
|
| 1347 | - if ($this->debug === true) { |
|
| 1348 | - $line = $tokens[$first]['line']; |
|
| 1349 | - $type = $tokens[$first]['type']; |
|
| 1350 | - echo "\t* amended first token is $first ($type) on line $line *".PHP_EOL; |
|
| 1346 | + $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $prev, true ); |
|
| 1347 | + if ( $this->debug === true ) { |
|
| 1348 | + $line = $tokens[ $first ][ 'line' ]; |
|
| 1349 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1350 | + echo "\t* amended first token is $first ($type) on line $line *" . PHP_EOL; |
|
| 1351 | 1351 | } |
| 1352 | 1352 | } |
| 1353 | 1353 | |
| 1354 | - $currentIndent = ($tokens[$first]['column'] - 1); |
|
| 1355 | - if ($object > 0 || $condition > 0) { |
|
| 1354 | + $currentIndent = ( $tokens[ $first ][ 'column' ] - 1 ); |
|
| 1355 | + if ( $object > 0 || $condition > 0 ) { |
|
| 1356 | 1356 | $currentIndent += $this->indent; |
| 1357 | 1357 | } |
| 1358 | 1358 | |
| 1359 | - if (isset($tokens[$first]['scope_closer']) === true |
|
| 1360 | - && $tokens[$first]['scope_closer'] === $first |
|
| 1359 | + if ( isset( $tokens[ $first ][ 'scope_closer' ] ) === true |
|
| 1360 | + && $tokens[ $first ][ 'scope_closer' ] === $first |
|
| 1361 | 1361 | ) { |
| 1362 | - if ($this->debug === true) { |
|
| 1363 | - echo "\t* first token is a scope closer *".PHP_EOL; |
|
| 1362 | + if ( $this->debug === true ) { |
|
| 1363 | + echo "\t* first token is a scope closer *" . PHP_EOL; |
|
| 1364 | 1364 | } |
| 1365 | 1365 | |
| 1366 | - if ($condition === 0 || $tokens[$condition]['scope_opener'] < $first) { |
|
| 1367 | - $currentIndent = $setIndents[$first]; |
|
| 1368 | - } else if ($this->debug === true) { |
|
| 1369 | - echo "\t* ignoring scope closer *".PHP_EOL; |
|
| 1366 | + if ( $condition === 0 || $tokens[ $condition ][ 'scope_opener' ] < $first ) { |
|
| 1367 | + $currentIndent = $setIndents[ $first ]; |
|
| 1368 | + } else if ( $this->debug === true ) { |
|
| 1369 | + echo "\t* ignoring scope closer *" . PHP_EOL; |
|
| 1370 | 1370 | } |
| 1371 | 1371 | } |
| 1372 | 1372 | |
| 1373 | 1373 | // Make sure it is divisible by our expected indent. |
| 1374 | - $currentIndent = (int) (ceil($currentIndent / $this->indent) * $this->indent); |
|
| 1375 | - $setIndents[$first] = $currentIndent; |
|
| 1374 | + $currentIndent = (int)( ceil( $currentIndent / $this->indent ) * $this->indent ); |
|
| 1375 | + $setIndents[ $first ] = $currentIndent; |
|
| 1376 | 1376 | |
| 1377 | - if ($this->debug === true) { |
|
| 1378 | - $type = $tokens[$first]['type']; |
|
| 1379 | - echo "\t=> indent set to $currentIndent by token $first ($type)".PHP_EOL; |
|
| 1377 | + if ( $this->debug === true ) { |
|
| 1378 | + $type = $tokens[ $first ][ 'type' ]; |
|
| 1379 | + echo "\t=> indent set to $currentIndent by token $first ($type)" . PHP_EOL; |
|
| 1380 | 1380 | } |
| 1381 | 1381 | }//end if |
| 1382 | 1382 | }//end for |
@@ -1399,72 +1399,72 @@ discard block |
||
| 1399 | 1399 | * |
| 1400 | 1400 | * @return bool |
| 1401 | 1401 | */ |
| 1402 | - protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) |
|
| 1402 | + protected function adjustIndent( File $phpcsFile, $stackPtr, $length, $change ) |
|
| 1403 | 1403 | { |
| 1404 | 1404 | $tokens = $phpcsFile->getTokens(); |
| 1405 | 1405 | |
| 1406 | 1406 | // We don't adjust indents outside of PHP. |
| 1407 | - if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) { |
|
| 1407 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_INLINE_HTML ) { |
|
| 1408 | 1408 | return false; |
| 1409 | 1409 | } |
| 1410 | 1410 | |
| 1411 | 1411 | $padding = ''; |
| 1412 | - if ($length > 0) { |
|
| 1413 | - if ($this->tabIndent === true) { |
|
| 1414 | - $numTabs = floor($length / $this->tabWidth); |
|
| 1415 | - if ($numTabs > 0) { |
|
| 1416 | - $numSpaces = ($length - ($numTabs * $this->tabWidth)); |
|
| 1417 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1412 | + if ( $length > 0 ) { |
|
| 1413 | + if ( $this->tabIndent === true ) { |
|
| 1414 | + $numTabs = floor( $length / $this->tabWidth ); |
|
| 1415 | + if ( $numTabs > 0 ) { |
|
| 1416 | + $numSpaces = ( $length - ( $numTabs * $this->tabWidth ) ); |
|
| 1417 | + $padding = str_repeat( "\t", $numTabs ) . str_repeat( ' ', $numSpaces ); |
|
| 1418 | 1418 | } |
| 1419 | 1419 | } else { |
| 1420 | - $padding = str_repeat(' ', $length); |
|
| 1420 | + $padding = str_repeat( ' ', $length ); |
|
| 1421 | 1421 | } |
| 1422 | 1422 | } |
| 1423 | 1423 | |
| 1424 | - if ($tokens[$stackPtr]['column'] === 1) { |
|
| 1425 | - $trimmed = ltrim($tokens[$stackPtr]['content']); |
|
| 1426 | - $accepted = $phpcsFile->fixer->replaceToken($stackPtr, $padding.$trimmed); |
|
| 1424 | + if ( $tokens[ $stackPtr ][ 'column' ] === 1 ) { |
|
| 1425 | + $trimmed = ltrim( $tokens[ $stackPtr ][ 'content' ] ); |
|
| 1426 | + $accepted = $phpcsFile->fixer->replaceToken( $stackPtr, $padding . $trimmed ); |
|
| 1427 | 1427 | } else { |
| 1428 | 1428 | // Easier to just replace the entire indent. |
| 1429 | - $accepted = $phpcsFile->fixer->replaceToken(($stackPtr - 1), $padding); |
|
| 1429 | + $accepted = $phpcsFile->fixer->replaceToken( ( $stackPtr - 1 ), $padding ); |
|
| 1430 | 1430 | } |
| 1431 | 1431 | |
| 1432 | - if ($accepted === false) { |
|
| 1432 | + if ( $accepted === false ) { |
|
| 1433 | 1433 | return false; |
| 1434 | 1434 | } |
| 1435 | 1435 | |
| 1436 | - if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 1436 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_DOC_COMMENT_OPEN_TAG ) { |
|
| 1437 | 1437 | // We adjusted the start of a comment, so adjust the rest of it |
| 1438 | 1438 | // as well so the alignment remains correct. |
| 1439 | - for ($x = ($stackPtr + 1); $x < $tokens[$stackPtr]['comment_closer']; $x++) { |
|
| 1440 | - if ($tokens[$x]['column'] !== 1) { |
|
| 1439 | + for ( $x = ( $stackPtr + 1 ); $x < $tokens[ $stackPtr ][ 'comment_closer' ]; $x++ ) { |
|
| 1440 | + if ( $tokens[ $x ][ 'column' ] !== 1 ) { |
|
| 1441 | 1441 | continue; |
| 1442 | 1442 | } |
| 1443 | 1443 | |
| 1444 | 1444 | $length = 0; |
| 1445 | - if ($tokens[$x]['code'] === T_DOC_COMMENT_WHITESPACE) { |
|
| 1446 | - $length = $tokens[$x]['length']; |
|
| 1445 | + if ( $tokens[ $x ][ 'code' ] === T_DOC_COMMENT_WHITESPACE ) { |
|
| 1446 | + $length = $tokens[ $x ][ 'length' ]; |
|
| 1447 | 1447 | } |
| 1448 | 1448 | |
| 1449 | - $padding = ($length + $change); |
|
| 1450 | - if ($padding > 0) { |
|
| 1451 | - if ($this->tabIndent === true) { |
|
| 1452 | - $numTabs = floor($padding / $this->tabWidth); |
|
| 1453 | - $numSpaces = ($padding - ($numTabs * $this->tabWidth)); |
|
| 1454 | - $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); |
|
| 1449 | + $padding = ( $length + $change ); |
|
| 1450 | + if ( $padding > 0 ) { |
|
| 1451 | + if ( $this->tabIndent === true ) { |
|
| 1452 | + $numTabs = floor( $padding / $this->tabWidth ); |
|
| 1453 | + $numSpaces = ( $padding - ( $numTabs * $this->tabWidth ) ); |
|
| 1454 | + $padding = str_repeat( "\t", $numTabs ) . str_repeat( ' ', $numSpaces ); |
|
| 1455 | 1455 | } else { |
| 1456 | - $padding = str_repeat(' ', $padding); |
|
| 1456 | + $padding = str_repeat( ' ', $padding ); |
|
| 1457 | 1457 | } |
| 1458 | 1458 | } else { |
| 1459 | 1459 | $padding = ''; |
| 1460 | 1460 | } |
| 1461 | 1461 | |
| 1462 | - $phpcsFile->fixer->replaceToken($x, $padding); |
|
| 1463 | - if ($this->debug === true) { |
|
| 1464 | - $length = strlen($padding); |
|
| 1465 | - $line = $tokens[$x]['line']; |
|
| 1466 | - $type = $tokens[$x]['type']; |
|
| 1467 | - echo "\t=> Indent adjusted to $length for $type on line $line".PHP_EOL; |
|
| 1462 | + $phpcsFile->fixer->replaceToken( $x, $padding ); |
|
| 1463 | + if ( $this->debug === true ) { |
|
| 1464 | + $length = strlen( $padding ); |
|
| 1465 | + $line = $tokens[ $x ][ 'line' ]; |
|
| 1466 | + $type = $tokens[ $x ][ 'type' ]; |
|
| 1467 | + echo "\t=> Indent adjusted to $length for $type on line $line" . PHP_EOL; |
|
| 1468 | 1468 | } |
| 1469 | 1469 | }//end for |
| 1470 | 1470 | }//end if |
@@ -14,8 +14,7 @@ discard block |
||
| 14 | 14 | use PHP_CodeSniffer\Util\Tokens; |
| 15 | 15 | use PHP_CodeSniffer\Config; |
| 16 | 16 | |
| 17 | -class ScopeIndentSniff implements Sniff |
|
| 18 | -{ |
|
| 17 | +class ScopeIndentSniff implements Sniff { |
|
| 19 | 18 | |
| 20 | 19 | /** |
| 21 | 20 | * A list of tokenizers this sniff supports. |
@@ -104,8 +103,7 @@ discard block |
||
| 104 | 103 | * |
| 105 | 104 | * @return array |
| 106 | 105 | */ |
| 107 | - public function register() |
|
| 108 | - { |
|
| 106 | + public function register() { |
|
| 109 | 107 | if (defined('PHP_CODESNIFFER_IN_TESTS') === true) { |
| 110 | 108 | $this->debug = false; |
| 111 | 109 | } |
@@ -124,8 +122,7 @@ discard block |
||
| 124 | 122 | * |
| 125 | 123 | * @return void |
| 126 | 124 | */ |
| 127 | - public function process(File $phpcsFile, $stackPtr) |
|
| 128 | - { |
|
| 125 | + public function process(File $phpcsFile, $stackPtr) { |
|
| 129 | 126 | $debug = Config::getConfigData('scope_indent_debug'); |
| 130 | 127 | if ($debug !== null) { |
| 131 | 128 | $this->debug = (bool) $debug; |
@@ -1399,8 +1396,7 @@ discard block |
||
| 1399 | 1396 | * |
| 1400 | 1397 | * @return bool |
| 1401 | 1398 | */ |
| 1402 | - protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) |
|
| 1403 | - { |
|
| 1399 | + protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) { |
|
| 1404 | 1400 | $tokens = $phpcsFile->getTokens(); |
| 1405 | 1401 | |
| 1406 | 1402 | // We don't adjust indents outside of PHP. |
@@ -1090,7 +1090,7 @@ discard block |
||
| 1090 | 1090 | if ($this->debug === true) { |
| 1091 | 1091 | $type = str_replace('_', ' ', strtolower(substr($tokens[$i]['type'], 2))); |
| 1092 | 1092 | $line = $tokens[$i]['line']; |
| 1093 | - echo "Open $type on line $line".PHP_EOL; |
|
| 1093 | + echo "open $type on line $line".PHP_EOL; |
|
| 1094 | 1094 | } |
| 1095 | 1095 | |
| 1096 | 1096 | $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $i, true); |
@@ -1236,7 +1236,7 @@ discard block |
||
| 1236 | 1236 | if ($this->debug === true) { |
| 1237 | 1237 | $type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2))); |
| 1238 | 1238 | $line = $tokens[$i]['line']; |
| 1239 | - echo "Close $type on line $line".PHP_EOL; |
|
| 1239 | + echo "close $type on line $line".PHP_EOL; |
|
| 1240 | 1240 | } |
| 1241 | 1241 | |
| 1242 | 1242 | $prev = false; |
@@ -9,9 +9,3 @@ |
||
| 9 | 9 | $arr = array( |
| 10 | 10 | 'a' => 'a', |
| 11 | 11 | <<<<<<< HEAD |
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
@@ -5,13 +5,7 @@ |
||
| 5 | 5 | // This is not a merge conflict - it is a valid test case to test handling of arrays without associated closer. |
| 6 | 6 | // Please do not remove. |
| 7 | 7 | function test() |
| 8 | - { |
|
| 9 | - $arr = array( |
|
| 10 | - 'a' => 'a', |
|
| 8 | + { |
|
| 9 | + $arr = array( |
|
| 10 | + 'a' => 'a', |
|
| 11 | 11 | <<<<<<< HEAD |
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
@@ -9,9 +9,3 @@ |
||
| 9 | 9 | $arr = array( |
| 10 | 10 | 'a' => 'a', |
| 11 | 11 | <<<<<<< HEAD |
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
@@ -8,10 +8,4 @@ |
||
| 8 | 8 | { |
| 9 | 9 | $arr = array( |
| 10 | 10 | 'a' => 'a', |
| 11 | -<<<<<<< HEAD |
|
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
| 11 | +<< << <<< HEAD |
|
@@ -4,14 +4,7 @@ |
||
| 4 | 4 | // The following function has a simulated git conflict for testing. |
| 5 | 5 | // This is not a merge conflict - it is a valid test case to test handling of arrays without associated closer. |
| 6 | 6 | // Please do not remove. |
| 7 | -function test() |
|
| 8 | - { |
|
| 7 | +function test() { |
|
| 9 | 8 | $arr = array( |
| 10 | 9 | 'a' => 'a', |
| 11 | 10 | <<<<<<< HEAD |
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
@@ -9,9 +9,3 @@ |
||
| 9 | 9 | $arr = array( |
| 10 | 10 | 'a' => 'a', |
| 11 | 11 | <<<<<<< HEAD |
| 12 | - 'b' => 'b' |
|
| 13 | -======= |
|
| 14 | - 'c' => 'c' |
|
| 15 | ->>>>>>> master |
|
| 16 | - ); |
|
| 17 | - } |
|
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * Should this test be skipped for some reason. |
| 21 | 21 | * |
| 22 | - * @return void |
|
| 22 | + * @return boolean |
|
| 23 | 23 | */ |
| 24 | 24 | protected function shouldSkipTest() |
| 25 | 25 | { |
@@ -16,54 +16,54 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Should this test be skipped for some reason. |
|
| 21 | - * |
|
| 22 | - * @return void |
|
| 23 | - */ |
|
| 24 | - protected function shouldSkipTest() |
|
| 25 | - { |
|
| 26 | - $lintPath = Config::getExecutablePath('gjslint'); |
|
| 27 | - if ($lintPath === null) { |
|
| 28 | - return true; |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * Should this test be skipped for some reason. |
|
| 21 | + * |
|
| 22 | + * @return void |
|
| 23 | + */ |
|
| 24 | + protected function shouldSkipTest() |
|
| 25 | + { |
|
| 26 | + $lintPath = Config::getExecutablePath('gjslint'); |
|
| 27 | + if ($lintPath === null) { |
|
| 28 | + return true; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - return false; |
|
| 31 | + return false; |
|
| 32 | 32 | |
| 33 | - }//end shouldSkipTest() |
|
| 33 | + }//end shouldSkipTest() |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns the lines where errors should occur. |
|
| 38 | - * |
|
| 39 | - * The key of the array should represent the line number and the value |
|
| 40 | - * should represent the number of errors that should occur on that line. |
|
| 41 | - * |
|
| 42 | - * @return array<int, int> |
|
| 43 | - */ |
|
| 44 | - public function getErrorList() |
|
| 45 | - { |
|
| 46 | - return []; |
|
| 36 | + /** |
|
| 37 | + * Returns the lines where errors should occur. |
|
| 38 | + * |
|
| 39 | + * The key of the array should represent the line number and the value |
|
| 40 | + * should represent the number of errors that should occur on that line. |
|
| 41 | + * |
|
| 42 | + * @return array<int, int> |
|
| 43 | + */ |
|
| 44 | + public function getErrorList() |
|
| 45 | + { |
|
| 46 | + return []; |
|
| 47 | 47 | |
| 48 | - }//end getErrorList() |
|
| 48 | + }//end getErrorList() |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Returns the lines where warnings should occur. |
|
| 53 | - * |
|
| 54 | - * The key of the array should represent the line number and the value |
|
| 55 | - * should represent the number of warnings that should occur on that line. |
|
| 56 | - * |
|
| 57 | - * @return array<int, int> |
|
| 58 | - */ |
|
| 59 | - public function getWarningList() |
|
| 60 | - { |
|
| 61 | - return [ |
|
| 62 | - 3 => 1, |
|
| 63 | - 5 => 1, |
|
| 64 | - ]; |
|
| 51 | + /** |
|
| 52 | + * Returns the lines where warnings should occur. |
|
| 53 | + * |
|
| 54 | + * The key of the array should represent the line number and the value |
|
| 55 | + * should represent the number of warnings that should occur on that line. |
|
| 56 | + * |
|
| 57 | + * @return array<int, int> |
|
| 58 | + */ |
|
| 59 | + public function getWarningList() |
|
| 60 | + { |
|
| 61 | + return [ |
|
| 62 | + 3 => 1, |
|
| 63 | + 5 => 1, |
|
| 64 | + ]; |
|
| 65 | 65 | |
| 66 | - }//end getWarningList() |
|
| 66 | + }//end getWarningList() |
|
| 67 | 67 | |
| 68 | 68 | |
| 69 | 69 | }//end class |
@@ -23,8 +23,8 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | protected function shouldSkipTest() |
| 25 | 25 | { |
| 26 | - $lintPath = Config::getExecutablePath('gjslint'); |
|
| 27 | - if ($lintPath === null) { |
|
| 26 | + $lintPath = Config::getExecutablePath( 'gjslint' ); |
|
| 27 | + if ( $lintPath === null ) { |
|
| 28 | 28 | return true; |
| 29 | 29 | } |
| 30 | 30 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function getErrorList() |
| 45 | 45 | { |
| 46 | - return []; |
|
| 46 | + return [ ]; |
|
| 47 | 47 | |
| 48 | 48 | }//end getErrorList() |
| 49 | 49 | |
@@ -12,8 +12,7 @@ discard block |
||
| 12 | 12 | use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; |
| 13 | 13 | use PHP_CodeSniffer\Config; |
| 14 | 14 | |
| 15 | -class ClosureLinterUnitTest extends AbstractSniffUnitTest |
|
| 16 | -{ |
|
| 15 | +class ClosureLinterUnitTest extends AbstractSniffUnitTest { |
|
| 17 | 16 | |
| 18 | 17 | |
| 19 | 18 | /** |
@@ -21,8 +20,7 @@ discard block |
||
| 21 | 20 | * |
| 22 | 21 | * @return void |
| 23 | 22 | */ |
| 24 | - protected function shouldSkipTest() |
|
| 25 | - { |
|
| 23 | + protected function shouldSkipTest() { |
|
| 26 | 24 | $lintPath = Config::getExecutablePath('gjslint'); |
| 27 | 25 | if ($lintPath === null) { |
| 28 | 26 | return true; |
@@ -41,8 +39,7 @@ discard block |
||
| 41 | 39 | * |
| 42 | 40 | * @return array<int, int> |
| 43 | 41 | */ |
| 44 | - public function getErrorList() |
|
| 45 | - { |
|
| 42 | + public function getErrorList() { |
|
| 46 | 43 | return []; |
| 47 | 44 | |
| 48 | 45 | }//end getErrorList() |
@@ -56,8 +53,7 @@ discard block |
||
| 56 | 53 | * |
| 57 | 54 | * @return array<int, int> |
| 58 | 55 | */ |
| 59 | - public function getWarningList() |
|
| 60 | - { |
|
| 56 | + public function getWarningList() { |
|
| 61 | 57 | return [ |
| 62 | 58 | 3 => 1, |
| 63 | 59 | 5 => 1, |
@@ -57,5 +57,4 @@ |
||
| 57 | 57 | |
| 58 | 58 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 59 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 60 | +$a |
|
| 62 | 61 | \ No newline at end of file |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | function test() |
| 23 | - { |
|
| 24 | - $arr = array( |
|
| 25 | - 'a' => 'a' |
|
| 23 | + { |
|
| 24 | + $arr = array( |
|
| 25 | + 'a' => 'a' |
|
| 26 | 26 | <<<<<<< HEAD |
| 27 | 27 | 'b' => 'b' |
| 28 | 28 | ======= |
@@ -57,5 +57,4 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 59 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 60 | +$a |
|
| 62 | 61 | \ No newline at end of file |
@@ -57,5 +57,4 @@ |
||
| 57 | 57 | |
| 58 | 58 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 59 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 60 | +$a |
|
| 62 | 61 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -var_dump(1 << -1); |
|
| 4 | -var_dump(1 >> -1); |
|
| 3 | +var_dump( 1 << -1 ); |
|
| 4 | +var_dump( 1 >> -1 ); |
|
| 5 | 5 | var_dump( |
| 6 | 6 | 1 |
| 7 | 7 | << |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | { |
| 24 | 24 | $arr = array( |
| 25 | 25 | 'a' => 'a' |
| 26 | -<<<<<<< HEAD |
|
| 26 | +<< << <<< HEAD |
|
| 27 | 27 | 'b' => 'b' |
| 28 | 28 | ======= |
| 29 | 29 | 'c' => 'c' |
@@ -57,5 +57,4 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 59 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 60 | +$a |
|
| 62 | 61 | \ No newline at end of file |
@@ -19,8 +19,7 @@ discard block |
||
| 19 | 19 | * <<<<<<< HEAD |
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | -function test() |
|
| 23 | - { |
|
| 22 | +function test() { |
|
| 24 | 23 | $arr = array( |
| 25 | 24 | 'a' => 'a' |
| 26 | 25 | <<<<<<< HEAD |
@@ -57,5 +56,4 @@ discard block |
||
| 57 | 56 | |
| 58 | 57 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 58 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 59 | +$a |
|
| 62 | 60 | \ No newline at end of file |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | $string = |
| 12 | 12 | <<<EOD |
| 13 | 13 | This is a heredoc. |
| 14 | -EOD; |
|
| 14 | +eod; |
|
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * This is not a merge conflict, but a comment showing what |
@@ -57,5 +57,4 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | // Test that stray boundaries, i.e. an opener without closer and such, are detected. |
| 59 | 59 | <<<<<<< HEAD |
| 60 | -$a = 1; |
|
| 61 | -======= |
|
| 60 | +$a |
|
| 62 | 61 | \ No newline at end of file |
@@ -24,8 +24,3 @@ |
||
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | 26 | <<<<<<< HEAD |
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
@@ -24,8 +24,3 @@ |
||
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | 26 | <<<<<<< HEAD |
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
@@ -24,8 +24,3 @@ |
||
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | 26 | <<<<<<< HEAD |
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | <<<<<<< HEAD |
| 15 | 15 | * @var string $bar |
| 16 | 16 | */ |
| 17 | -public function foo($bar){ } |
|
| 17 | +public function foo( $bar ) { } |
|
| 18 | 18 | |
| 19 | 19 | /* |
| 20 | 20 | ======= |
@@ -23,9 +23,4 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | -<<<<<<< HEAD |
|
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
| 26 | +<< << <<< HEAD |
|
@@ -24,8 +24,3 @@ |
||
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | 26 | <<<<<<< HEAD |
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
@@ -24,8 +24,3 @@ |
||
| 24 | 24 | |
| 25 | 25 | // Comment |
| 26 | 26 | <<<<<<< HEAD |
| 27 | -// Second comment line. NOTE: The above opener breaks the tokenizer. |
|
| 28 | -======= |
|
| 29 | -// New second comment line |
|
| 30 | ->>>>>>> master |
|
| 31 | -// Third comment line |
|
@@ -22,22 +22,4 @@ |
||
| 22 | 22 | <<<<<<< HEAD |
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -22,22 +22,4 @@ |
||
| 22 | 22 | <<<<<<< HEAD |
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -22,22 +22,4 @@ |
||
| 22 | 22 | <<<<<<< HEAD |
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -19,25 +19,7 @@ |
||
| 19 | 19 | <?php |
| 20 | 20 | |
| 21 | 21 | // Break the tokenizer. |
| 22 | -<<<<<<< HEAD |
|
| 22 | +<< << <<< HEAD |
|
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -22,22 +22,4 @@ |
||
| 22 | 22 | <<<<<<< HEAD |
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -22,22 +22,4 @@ |
||
| 22 | 22 | <<<<<<< HEAD |
| 23 | 23 | $a = 1; |
| 24 | 24 | ======= |
| 25 | -$a = 2; |
|
| 26 | ->>>>>>> master |
|
| 27 | - |
|
| 28 | -/* |
|
| 29 | - * The above tests are based on "normal" tokens. |
|
| 30 | - * The below test checks that once the tokenizer breaks down because of |
|
| 31 | - * unexpected merge conflict boundaries - i.e. after the first merge conflict |
|
| 32 | - * opener in non-comment, non-heredoc/nowdoc, non-inline HTML code -, subsequent |
|
| 33 | - * merge conflict boundaries will still be detected correctly. |
|
| 34 | - */ |
|
| 35 | -?> |
|
| 36 | - |
|
| 37 | -<div class="abc"> |
|
| 38 | -<<<<<<< HEAD |
|
| 39 | - <p id="test-this">Testing a merge conflict.</p> |
|
| 40 | -======= |
|
| 41 | - <p id="test-that">Another text string.</p> |
|
| 42 | ->>>>>>> ref/heads/feature-branch |
|
| 43 | -</div> |
|
| 25 | +$a |
|
| 44 | 26 | \ No newline at end of file |
@@ -64,8 +64,4 @@ |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -64,8 +64,4 @@ |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -64,8 +64,4 @@ |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -22,11 +22,11 @@ discard block |
||
| 22 | 22 | ======= |
| 23 | 23 | should also be detected. |
| 24 | 24 | EOT; |
| 25 | ->>>>>>> ref/heads/other-branchname |
|
| 25 | +>> >> >> > ref / heads / other - branchname |
|
| 26 | 26 | |
| 27 | 27 | // Merge conflict starter outside with a closer inside of the heredoc. |
| 28 | 28 | // This breaks the tokenizer. |
| 29 | -<<<<<<< HEAD |
|
| 29 | +<< << <<< HEAD |
|
| 30 | 30 | $string = |
| 31 | 31 | <<<EOT |
| 32 | 32 | Merge conflicts in heredocs |
@@ -64,8 +64,4 @@ discard block |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -64,8 +64,4 @@ |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | should also be detected. |
| 10 | 10 | >>>>>>> ref/heads/other-branchname |
| 11 | 11 | And now they are. |
| 12 | -EOD; |
|
| 12 | +eod; |
|
| 13 | 13 | |
| 14 | 14 | // Heredoc with a merge conflict starter, the closer is outside the heredoc. |
| 15 | 15 | $string = |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | $a = 1; |
| 22 | 22 | ======= |
| 23 | 23 | should also be detected. |
| 24 | -EOT; |
|
| 24 | +eot; |
|
| 25 | 25 | >>>>>>> ref/heads/other-branchname |
| 26 | 26 | |
| 27 | 27 | // Merge conflict starter outside with a closer inside of the heredoc. |
@@ -64,8 +64,4 @@ discard block |
||
| 64 | 64 | <<<<<<< HEAD |
| 65 | 65 | can be problematic. |
| 66 | 66 | EOD; |
| 67 | -$a = 1; |
|
| 68 | -======= |
|
| 69 | -should also be detected. |
|
| 70 | -EOT; |
|
| 71 | ->>>>>>> ref/heads/other-branchname |
|
| 67 | +$a |
|
| 72 | 68 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | EOD; |
| 13 | 13 | |
| 14 | 14 | // Break the tokenizer. |
| 15 | -<<<<<<< HEAD |
|
| 15 | +<< << <<< HEAD |
|
| 16 | 16 | |
| 17 | 17 | /* |
| 18 | 18 | * The above tests are based on "normal" tokens. |
@@ -22,13 +22,4 @@ discard block |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | should also be detected. |
| 10 | 10 | >>>>>>> ref/heads/other-branchname |
| 11 | 11 | And now they are. |
| 12 | -EOD; |
|
| 12 | +eod; |
|
| 13 | 13 | |
| 14 | 14 | // Break the tokenizer. |
| 15 | 15 | <<<<<<< HEAD |
@@ -22,13 +22,4 @@ discard block |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | -<<<'EOD' |
|
| 27 | -can be problematic. |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | -were previously not detected. |
|
| 30 | -======= |
|
| 31 | -should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | -And now they are. |
|
| 34 | -EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |
@@ -22,13 +22,4 @@ |
||
| 22 | 22 | * merge conflict boundaries will still be detected correctly. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | -$string = |
|
| 26 | - <<<"EOD" |
|
| 27 | - Merge conflicts in PHP 7.3 indented heredocs |
|
| 28 | -<<<<<<< HEAD |
|
| 29 | - can be problematic. |
|
| 30 | -======= |
|
| 31 | - should also be detected. |
|
| 32 | ->>>>>>> ref/heads/other-branchname |
|
| 33 | - And now they are. |
|
| 34 | - EOD; |
|
| 25 | +$string |
|
| 35 | 26 | \ No newline at end of file |