Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AssignmentInConditionSniff.php

class AssignmentInConditionSniff

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/CodeAnalysis/AssignmentInConditionSniff.php \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of AssignmentInConditionSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php, line 17

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class AssignmentInConditionSniff implements Sniff {
    public const CODE_ASSIGNMENT_IN_CONDITION = 'AssignmentInCondition';
    
    /** @var bool */
    public $ignoreAssignmentsInsideFunctionCalls = false;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_IF,
            T_ELSEIF,
            T_DO,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $conditionStartPointer
     */
    public function process(File $phpcsFile, $conditionStartPointer) : void {
        $tokens = $phpcsFile->getTokens();
        $token = $tokens[$conditionStartPointer];
        if ($token['code'] === T_DO) {
            $whilePointer = TokenHelper::findNext($phpcsFile, T_WHILE, $token['scope_closer'] + 1);
            $whileToken = $tokens[$whilePointer];
            $parenthesisOpener = $whileToken['parenthesis_opener'];
            $parenthesisCloser = $whileToken['parenthesis_closer'];
            $type = 'do-while';
        }
        else {
            $parenthesisOpener = $token['parenthesis_opener'];
            $parenthesisCloser = $token['parenthesis_closer'];
            $type = $token['code'] === T_IF ? 'if' : 'elseif';
        }
        if ($parenthesisOpener === null || $parenthesisCloser === null) {
            return;
        }
        $this->processCondition($phpcsFile, $parenthesisOpener, $parenthesisCloser, $type);
    }
    private function processCondition(File $phpcsFile, int $parenthesisOpener, int $parenthesisCloser, string $conditionType) : void {
        $equalsTokenPointers = TokenHelper::findNextAll($phpcsFile, T_EQUAL, $parenthesisOpener + 1, $parenthesisCloser);
        if ($equalsTokenPointers === []) {
            return;
        }
        if (!$this->ignoreAssignmentsInsideFunctionCalls) {
            $this->error($phpcsFile, $conditionType, $equalsTokenPointers[0]);
            return;
        }
        $tokens = $phpcsFile->getTokens();
        foreach ($equalsTokenPointers as $equalsTokenPointer) {
            $parenthesisStarts = array_keys($tokens[$equalsTokenPointer]['nested_parenthesis']);
            
            /** @var int $insideParenthesis */
            $insideParenthesis = max($parenthesisStarts);
            if ($insideParenthesis === $parenthesisOpener) {
                $this->error($phpcsFile, $conditionType, $equalsTokenPointer);
                continue;
            }
            $functionCall = TokenHelper::findPrevious($phpcsFile, TokenHelper::getOnlyNameTokenCodes(), $insideParenthesis, $parenthesisOpener);
            if ($functionCall !== null) {
                continue;
            }
            $this->error($phpcsFile, $conditionType, $equalsTokenPointer);
        }
    }
    private function error(File $phpcsFile, string $conditionType, int $equalsTokenPointer) : void {
        $phpcsFile->addError(sprintf('Assignment in %s condition is not allowed.', $conditionType), $equalsTokenPointer, self::CODE_ASSIGNMENT_IN_CONDITION);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AssignmentInConditionSniff::$ignoreAssignmentsInsideFunctionCalls public property @var bool
AssignmentInConditionSniff::CODE_ASSIGNMENT_IN_CONDITION public constant
AssignmentInConditionSniff::error private function
AssignmentInConditionSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
AssignmentInConditionSniff::processCondition private function
AssignmentInConditionSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal