|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Checks that control structures have boolean operators in the correct place. |
| 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 | + |
| 16 | +class BooleanOperatorPlacementSniff implements Sniff |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * Returns an array of tokens this test wants to listen for. |
| 22 | + * |
| 23 | + * @return array |
| 24 | + */ |
| 25 | + public function register() |
| 26 | + { |
| 27 | + return [ |
| 28 | + T_IF, |
| 29 | + T_WHILE, |
| 30 | + T_SWITCH, |
| 31 | + T_ELSEIF, |
| 32 | + ]; |
| 33 | + |
| 34 | + }//end register() |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * Processes this test, when one of its tokens is encountered. |
| 39 | + * |
| 40 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 41 | + * @param int $stackPtr The position of the current token |
| 42 | + * in the stack passed in $tokens. |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function process(File $phpcsFile, $stackPtr) |
| 47 | + { |
| 48 | + $tokens = $phpcsFile->getTokens(); |
| 49 | + |
| 50 | + if (isset($tokens[$stackPtr]['parenthesis_opener']) === false |
| 51 | + || isset($tokens[$stackPtr]['parenthesis_closer']) === false |
| 52 | + ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $parenOpener = $tokens[$stackPtr]['parenthesis_opener']; |
| 57 | + $parenCloser = $tokens[$stackPtr]['parenthesis_closer']; |
| 58 | + |
| 59 | + if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) { |
| 60 | + // Conditions are all on the same line. |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $find = [ |
| 65 | + T_BOOLEAN_AND, |
| 66 | + T_BOOLEAN_OR, |
| 67 | + ]; |
| 68 | + |
| 69 | + $operator = $parenOpener; |
| 70 | + $position = null; |
| 71 | + $error = false; |
| 72 | + $operators = []; |
| 73 | + |
| 74 | + do { |
| 75 | + $operator = $phpcsFile->findNext($find, ($operator + 1), $parenCloser); |
| 76 | + if ($operator === false) { |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + $operators[] = $operator; |
| 81 | + |
| 82 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true); |
| 83 | + if ($prev === false) { |
| 84 | + // Parse error. |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if ($tokens[$prev]['line'] < $tokens[$operator]['line']) { |
| 89 | + // The boolean operator is the first content on the line. |
| 90 | + if ($position === null) { |
| 91 | + $position = 'first'; |
| 92 | + } |
| 93 | + |
| 94 | + if ($position !== 'first') { |
| 95 | + $error = true; |
| 96 | + } |
| 97 | + |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true); |
| 102 | + if ($next === false) { |
| 103 | + // Parse error. |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if ($tokens[$next]['line'] > $tokens[$operator]['line']) { |
| 108 | + // The boolean operator is the last content on the line. |
| 109 | + if ($position === null) { |
| 110 | + $position = 'last'; |
| 111 | + } |
| 112 | + |
| 113 | + if ($position !== 'last') { |
| 114 | + $error = true; |
| 115 | + } |
| 116 | + |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if ($position === null) { |
| 121 | + $position = 'middle'; |
| 122 | + } |
| 123 | + |
| 124 | + // Error here regardless as boolean operators need to be at start/end of line. |
| 125 | + $msg = 'Boolean operators between conditions must be at the beginning or end of the line'; |
| 126 | + $phpcsFile->addError($msg, $next, 'FoundMiddle'); |
| 127 | + |
| 128 | + if ($position !== 'middle') { |
| 129 | + $error = true; |
| 130 | + } |
| 131 | + } while ($operator !== false); |
| 132 | + |
| 133 | + if ($error === false) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + $error = 'Boolean operators between conditions must be at the beginning or end of the line, but not both'; |
| 138 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'FoundMixed'); |
| 139 | + if ($fix === false) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + $phpcsFile->fixer->beginChangeset(); |
| 144 | + foreach ($operators as $operator) { |
| 145 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true); |
| 146 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true); |
| 147 | + |
| 148 | + if ($position === 'last') { |
| 149 | + if ($tokens[$next]['line'] === $tokens[$operator]['line']) { |
| 150 | + if ($tokens[$prev]['line'] === $tokens[$operator]['line']) { |
| 151 | + // Move the content after the operator to the next line. |
| 152 | + if ($tokens[($operator + 1)]['code'] === T_WHITESPACE) { |
| 153 | + $phpcsFile->fixer->replaceToken(($operator + 1), ''); |
| 154 | + } |
| 155 | + |
| 156 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $operator, true); |
| 157 | + $padding = str_repeat(' ', ($tokens[$first]['column'] - 1)); |
| 158 | + $phpcsFile->fixer->addContent($operator, $phpcsFile->eolChar.$padding); |
| 159 | + } else { |
| 160 | + // Move the operator to the end of the previous line. |
| 161 | + if ($tokens[($operator + 1)]['code'] === T_WHITESPACE) { |
| 162 | + $phpcsFile->fixer->replaceToken(($operator + 1), ''); |
| 163 | + } |
| 164 | + |
| 165 | + $phpcsFile->fixer->addContent($prev, ' '.$tokens[$operator]['content']); |
| 166 | + $phpcsFile->fixer->replaceToken($operator, ''); |
| 167 | + } |
| 168 | + }//end if |
| 169 | + } else { |
| 170 | + if ($tokens[$prev]['line'] === $tokens[$operator]['line']) { |
| 171 | + if ($tokens[$next]['line'] === $tokens[$operator]['line']) { |
| 172 | + // Move the operator, and the rest of the expression, to the next line. |
| 173 | + if ($tokens[($operator - 1)]['code'] === T_WHITESPACE) { |
| 174 | + $phpcsFile->fixer->replaceToken(($operator - 1), ''); |
| 175 | + } |
| 176 | + |
| 177 | + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $operator, true); |
| 178 | + $padding = str_repeat(' ', ($tokens[$first]['column'] - 1)); |
| 179 | + $phpcsFile->fixer->addContentBefore($operator, $phpcsFile->eolChar.$padding); |
| 180 | + } else { |
| 181 | + // Move the operator to the start of the next line. |
| 182 | + if ($tokens[($operator - 1)]['code'] === T_WHITESPACE) { |
| 183 | + $phpcsFile->fixer->replaceToken(($operator - 1), ''); |
| 184 | + } |
| 185 | + |
| 186 | + $phpcsFile->fixer->addContentBefore($next, $tokens[$operator]['content'].' '); |
| 187 | + $phpcsFile->fixer->replaceToken($operator, ''); |
| 188 | + } |
| 189 | + }//end if |
| 190 | + }//end if |
| 191 | + }//end foreach |
| 192 | + |
| 193 | + $phpcsFile->fixer->endChangeset(); |
| 194 | + |
| 195 | + }//end process() |
| 196 | + |
| 197 | + |
| 198 | +}//end class |
0 commit comments