|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Checks that control structures have the correct spacing. |
| 4 | + * |
| 5 | + * @author Greg Sherwood <gsherwood@squiz.net> |
| 6 | + * @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\ControlStructures; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Util\Tokens; |
| 15 | +use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2Spacing; |
| 16 | + |
| 17 | +class ControlStructureSpacingSniff implements Sniff |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * The number of spaces code should be indented. |
| 22 | + * |
| 23 | + * @var integer |
| 24 | + */ |
| 25 | + public $indent = 4; |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns an array of tokens this test wants to listen for. |
| 30 | + * |
| 31 | + * @return array |
| 32 | + */ |
| 33 | + public function register() |
| 34 | + { |
| 35 | + return [ |
| 36 | + T_IF, |
| 37 | + T_WHILE, |
| 38 | + T_FOREACH, |
| 39 | + T_FOR, |
| 40 | + T_SWITCH, |
| 41 | + T_ELSE, |
| 42 | + T_ELSEIF, |
| 43 | + T_CATCH, |
| 44 | + ]; |
| 45 | + |
| 46 | + }//end register() |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Processes this test, when one of its tokens is encountered. |
| 51 | + * |
| 52 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 53 | + * @param int $stackPtr The position of the current token |
| 54 | + * in the stack passed in $tokens. |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function process(File $phpcsFile, $stackPtr) |
| 59 | + { |
| 60 | + $tokens = $phpcsFile->getTokens(); |
| 61 | + |
| 62 | + if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
| 63 | + || isset($tokens[$stackPtr]['parenthesis_closer']) === false |
| 64 | + ) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $parenOpener = $tokens[$stackPtr]['parenthesis_opener']; |
| 69 | + $parenCloser = $tokens[$stackPtr]['parenthesis_closer']; |
| 70 | + |
| 71 | + if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) { |
| 72 | + // Conditions are all on the same line, so follow PSR2. |
| 73 | + $sniff = new PSR2Spacing(); |
| 74 | + return $sniff->process($phpcsFile, $stackPtr); |
| 75 | + } |
| 76 | + |
| 77 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), $parenCloser, true); |
| 78 | + if ($next === false) { |
| 79 | + // No conditions; parse error. |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Check the first expression. |
| 84 | + if ($tokens[$next]['line'] !== ($tokens[$parenOpener]['line'] + 1)) { |
| 85 | + $error = 'The first expression of a multi-line control structure must be on the line after the opening parenthesis'; |
| 86 | + $fix = $phpcsFile->addFixableError($error, $next, 'FirstExpressionLine'); |
| 87 | + if ($fix === true) { |
| 88 | + $phpcsFile->fixer->addNewline($parenOpener); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Check the indent of each line. |
| 93 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true); |
| 94 | + $requiredIndent = ($tokens[$first]['column'] + $this->indent - 1); |
| 95 | + for ($i = $parenOpener; $i < $parenCloser; $i++) { |
| 96 | + if ($tokens[$i]['column'] !== 1 |
| 97 | + || $tokens[($i + 1)]['line'] > $tokens[$i]['line'] |
| 98 | + ) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (($i + 1) === $parenCloser) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + if ($tokens[$i]['code'] !== T_WHITESPACE) { |
| 107 | + $foundIndent = 0; |
| 108 | + } else { |
| 109 | + $foundIndent = $tokens[$i]['length']; |
| 110 | + } |
| 111 | + |
| 112 | + if ($foundIndent < $requiredIndent) { |
| 113 | + $error = 'Each line in a multi-line control structure must be indented at least once; expected at least %s spaces, but found %s'; |
| 114 | + $data = [ |
| 115 | + $requiredIndent, |
| 116 | + $foundIndent, |
| 117 | + ]; |
| 118 | + $fix = $phpcsFile->addFixableError($error, $i, 'LineIndent', $data); |
| 119 | + if ($fix === true) { |
| 120 | + $padding = str_repeat(' ', $requiredIndent); |
| 121 | + if ($foundIndent === 0) { |
| 122 | + $phpcsFile->fixer->addContentBefore($i, $padding); |
| 123 | + } else { |
| 124 | + $phpcsFile->fixer->replaceToken($i, $padding); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + }//end for |
| 129 | + |
| 130 | + // Check the closing parenthesis. |
| 131 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($parenCloser - 1), $parenOpener, true); |
| 132 | + if ($tokens[$parenCloser]['line'] !== ($tokens[$prev]['line'] + 1)) { |
| 133 | + $error = 'The closing parenthesis of a multi-line control structure must be on the line after the last expression'; |
| 134 | + $fix = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisLine'); |
| 135 | + if ($fix === true) { |
| 136 | + if ($tokens[$parenCloser]['line'] === $tokens[$prev]['line']) { |
| 137 | + $phpcsFile->fixer->addNewlineBefore($parenCloser); |
| 138 | + } else { |
| 139 | + $phpcsFile->fixer->beginChangeset(); |
| 140 | + for ($i = ($prev + 1); $i < $parenCloser; $i++) { |
| 141 | + // Maintian existing newline. |
| 142 | + if ($tokens[$i]['line'] === $tokens[$prev]['line']) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + // Maintain existing indent. |
| 147 | + if ($tokens[$i]['line'] === $tokens[$parenCloser]['line']) { |
| 148 | + break; |
| 149 | + } |
| 150 | + |
| 151 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 152 | + } |
| 153 | + |
| 154 | + $phpcsFile->fixer->endChangeset(); |
| 155 | + } |
| 156 | + }//end if |
| 157 | + }//end if |
| 158 | + |
| 159 | + if ($tokens[$parenCloser]['line'] !== $tokens[$prev]['line']) { |
| 160 | + $requiredIndent = ($tokens[$first]['column'] - 1); |
| 161 | + $foundIndent = ($tokens[$parenCloser]['column'] - 1); |
| 162 | + if ($foundIndent !== $requiredIndent) { |
| 163 | + $error = 'The closing parenthesis of a multi-line control structure must be indented to the same level as start of the control structure; expected %s spaces but found %s'; |
| 164 | + $data = [ |
| 165 | + $requiredIndent, |
| 166 | + $foundIndent, |
| 167 | + ]; |
| 168 | + $fix = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisIndent', $data); |
| 169 | + if ($fix === true) { |
| 170 | + $padding = str_repeat(' ', $requiredIndent); |
| 171 | + if ($foundIndent === 0) { |
| 172 | + $phpcsFile->fixer->addContentBefore($parenCloser, $padding); |
| 173 | + } else { |
| 174 | + $phpcsFile->fixer->replaceToken(($parenCloser - 1), $padding); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + }//end process() |
| 181 | + |
| 182 | + |
| 183 | +}//end class |
0 commit comments