Source for file AssignmentInConditionSniff.php
Documentation is available at AssignmentInConditionSniff.php
* Detects variable assignments being made within conditions.
* This is a typical code smell and more often than not a comparison was intended.
* Note: this sniff does not detect variable assignments in the conditional part of ternaries!
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2017 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class AssignmentInConditionSniff implements Sniff
* Assignment tokens to trigger on.
* Set in the register() method.
protected $assignmentTokens = array ();
* The tokens that indicate the start of a condition.
protected $conditionStartTokens = array ();
* Registers the tokens that this sniff wants to listen for.
public function register ()
$this->assignmentTokens = Tokens ::$assignmentTokens;
unset ($this->assignmentTokens[T_DOUBLE_ARROW ]);
$starters = Tokens ::$booleanOperators;
$this->conditionStartTokens = $starters;
* 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.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$token = $tokens[$stackPtr];
// Find the condition opener/closer.
if ($token['code'] === T_FOR ) {
if (isset ($token['parenthesis_opener'], $token['parenthesis_closer']) === false ) {
$semicolon = $phpcsFile->findNext (T_SEMICOLON, ($token['parenthesis_opener'] + 1 ), ($token['parenthesis_closer']));
if ($semicolon === false ) {
$semicolon = $phpcsFile->findNext (T_SEMICOLON, ($opener + 1 ), ($token['parenthesis_closer']));
if ($semicolon === false ) {
} else if ($token['code'] === T_CASE ) {
if (isset ($token['scope_opener']) === false ) {
$closer = $token['scope_opener'];
if (isset ($token['parenthesis_opener'], $token['parenthesis_closer']) === false ) {
$opener = $token['parenthesis_opener'];
$closer = $token['parenthesis_closer'];
$hasAssignment = $phpcsFile->findNext ($this->assignmentTokens, ($startPos + 1 ), $closer);
if ($hasAssignment === false ) {
// Examine whether the left side is a variable.
$conditionStart = $startPos;
$altConditionStart = $phpcsFile->findPrevious ($this->conditionStartTokens, ($hasAssignment - 1 ), $startPos);
if ($altConditionStart !== false ) {
$conditionStart = $altConditionStart;
for ($i = $hasAssignment; $i > $conditionStart; $i-- ) {
if (isset (Tokens ::$emptyTokens[$tokens[$i]['code']]) === true ) {
// If this is a variable or array, we've seen all we need to see.
// If this is a function call or something, we are OK.
if ($hasVariable === true ) {
'Variable assignment found within a condition. Did you mean to do a comparison ?',
$startPos = $hasAssignment;
} while ($startPos < $closer);
Documentation generated on Mon, 11 Mar 2019 15:27:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|