forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PSR12.ControlStructures.BooleanOperatorPlacement sniff to enfor…
…ce that boolean operators between conditions are consistently at the start or end of the line (ref squizlabs#750)
- Loading branch information
Showing
5 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
src/Standards/PSR12/Sniffs/ControlStructures/BooleanOperatorPlacementSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php | ||
/** | ||
* Checks that control structures have boolean operators in the correct place. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\ControlStructures; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
class BooleanOperatorPlacementSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_IF, | ||
T_WHILE, | ||
T_SWITCH, | ||
T_ELSEIF, | ||
]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false | ||
|| isset($tokens[$stackPtr]['parenthesis_closer']) === false | ||
) { | ||
return; | ||
} | ||
|
||
$parenOpener = $tokens[$stackPtr]['parenthesis_opener']; | ||
$parenCloser = $tokens[$stackPtr]['parenthesis_closer']; | ||
|
||
if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) { | ||
// Conditions are all on the same line. | ||
return; | ||
} | ||
|
||
$find = [ | ||
T_BOOLEAN_AND, | ||
T_BOOLEAN_OR, | ||
]; | ||
|
||
$operator = $parenOpener; | ||
$position = null; | ||
$error = false; | ||
$operators = []; | ||
|
||
do { | ||
$operator = $phpcsFile->findNext($find, ($operator + 1), $parenCloser); | ||
if ($operator === false) { | ||
break; | ||
} | ||
|
||
$operators[] = $operator; | ||
|
||
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true); | ||
if ($prev === false) { | ||
// Parse error. | ||
return; | ||
} | ||
|
||
if ($tokens[$prev]['line'] < $tokens[$operator]['line']) { | ||
// The boolean operator is the first content on the line. | ||
if ($position === null) { | ||
$position = 'first'; | ||
} | ||
|
||
if ($position !== 'first') { | ||
$error = true; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true); | ||
if ($next === false) { | ||
// Parse error. | ||
return; | ||
} | ||
|
||
if ($tokens[$next]['line'] > $tokens[$operator]['line']) { | ||
// The boolean operator is the last content on the line. | ||
if ($position === null) { | ||
$position = 'last'; | ||
} | ||
|
||
if ($position !== 'last') { | ||
$error = true; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if ($position === null) { | ||
$position = 'middle'; | ||
} | ||
|
||
// Error here regardless as boolean operators need to be at start/end of line. | ||
$msg = 'Boolean operators between conditions must be at the beginning or end of the line'; | ||
$phpcsFile->addError($msg, $next, 'FoundMiddle'); | ||
|
||
if ($position !== 'middle') { | ||
$error = true; | ||
} | ||
} while ($operator !== false); | ||
|
||
if ($error === false) { | ||
return; | ||
} | ||
|
||
$error = 'Boolean operators between conditions must be at the beginning or end of the line, but not both'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'FoundMixed'); | ||
if ($fix === false) { | ||
return; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
foreach ($operators as $operator) { | ||
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true); | ||
$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true); | ||
|
||
if ($position === 'last') { | ||
if ($tokens[$next]['line'] === $tokens[$operator]['line']) { | ||
if ($tokens[$prev]['line'] === $tokens[$operator]['line']) { | ||
// Move the content after the operator to the next line. | ||
if ($tokens[($operator + 1)]['code'] === T_WHITESPACE) { | ||
$phpcsFile->fixer->replaceToken(($operator + 1), ''); | ||
} | ||
|
||
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $operator, true); | ||
$padding = str_repeat(' ', ($tokens[$first]['column'] - 1)); | ||
$phpcsFile->fixer->addContent($operator, $phpcsFile->eolChar.$padding); | ||
} else { | ||
// Move the operator to the end of the previous line. | ||
if ($tokens[($operator + 1)]['code'] === T_WHITESPACE) { | ||
$phpcsFile->fixer->replaceToken(($operator + 1), ''); | ||
} | ||
|
||
$phpcsFile->fixer->addContent($prev, ' '.$tokens[$operator]['content']); | ||
$phpcsFile->fixer->replaceToken($operator, ''); | ||
} | ||
}//end if | ||
} else { | ||
if ($tokens[$prev]['line'] === $tokens[$operator]['line']) { | ||
if ($tokens[$next]['line'] === $tokens[$operator]['line']) { | ||
// Move the operator, and the rest of the expression, to the next line. | ||
if ($tokens[($operator - 1)]['code'] === T_WHITESPACE) { | ||
$phpcsFile->fixer->replaceToken(($operator - 1), ''); | ||
} | ||
|
||
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $operator, true); | ||
$padding = str_repeat(' ', ($tokens[$first]['column'] - 1)); | ||
$phpcsFile->fixer->addContentBefore($operator, $phpcsFile->eolChar.$padding); | ||
} else { | ||
// Move the operator to the start of the next line. | ||
if ($tokens[($operator - 1)]['code'] === T_WHITESPACE) { | ||
$phpcsFile->fixer->replaceToken(($operator - 1), ''); | ||
} | ||
|
||
$phpcsFile->fixer->addContentBefore($next, $tokens[$operator]['content'].' '); | ||
$phpcsFile->fixer->replaceToken($operator, ''); | ||
} | ||
}//end if | ||
}//end if | ||
}//end foreach | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
41 changes: 41 additions & 0 deletions
41
src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
if ( | ||
$expr1 | ||
&& $expr2 | ||
&& ($expr3 | ||
|| $expr4) | ||
&& $expr5 | ||
) { | ||
// if body | ||
} elseif ( | ||
$expr1 && | ||
($expr3 || $expr4) | ||
&& $expr5 | ||
) { | ||
// elseif body | ||
} elseif ( | ||
$expr1 | ||
&& ($expr3 || $expr4) && | ||
$expr5 | ||
) { | ||
// elseif body | ||
} | ||
|
||
if ($expr1 || $expr2) { | ||
} | ||
|
||
do { | ||
} while ( | ||
$expr1 || $expr2 | ||
|| $expr3 || | ||
$expr4 | ||
); | ||
|
||
switch ( | ||
$expr1 | ||
&& $expr2 && | ||
$expr3 || $expr4 || $expr5 && $expr6 && | ||
$expr7 | ||
) { | ||
// structure body | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
if ( | ||
$expr1 | ||
&& $expr2 | ||
&& ($expr3 | ||
|| $expr4) | ||
&& $expr5 | ||
) { | ||
// if body | ||
} elseif ( | ||
$expr1 && | ||
($expr3 || | ||
$expr4) && | ||
$expr5 | ||
) { | ||
// elseif body | ||
} elseif ( | ||
$expr1 | ||
&& ($expr3 | ||
|| $expr4) | ||
&& $expr5 | ||
) { | ||
// elseif body | ||
} | ||
|
||
if ($expr1 || $expr2) { | ||
} | ||
|
||
do { | ||
} while ( | ||
$expr1 | ||
|| $expr2 | ||
|| $expr3 | ||
|| $expr4 | ||
); | ||
|
||
switch ( | ||
$expr1 | ||
&& $expr2 | ||
&& $expr3 | ||
|| $expr4 | ||
|| $expr5 | ||
&& $expr6 | ||
&& $expr7 | ||
) { | ||
// structure body | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Unit test class for the BooleanOperatorPlacement sniff. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Tests\ControlStructures; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class BooleanOperatorPlacementUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
10 => 1, | ||
12 => 1, | ||
16 => 1, | ||
18 => 1, | ||
28 => 1, | ||
29 => 1, | ||
34 => 1, | ||
37 => 3, | ||
]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |