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

Breadcrumb

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

class RequireMultiLineConditionSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\ControlStructures\AbstractLineCondition implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \SlevomatCodingStandard\Sniffs\ControlStructures\RequireMultiLineConditionSniff extends \SlevomatCodingStandard\Sniffs\ControlStructures\AbstractLineCondition

Expanded class hierarchy of RequireMultiLineConditionSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireMultiLineConditionSniff.php, line 18

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class RequireMultiLineConditionSniff extends AbstractLineCondition {
    public const CODE_REQUIRED_MULTI_LINE_CONDITION = 'RequiredMultiLineCondition';
    
    /** @var int */
    public $minLineLength = 121;
    
    /** @var bool */
    public $booleanOperatorOnPreviousLine = false;
    
    /** @var bool */
    public $alwaysSplitAllConditionParts = false;
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $controlStructurePointer
     */
    public function process(File $phpcsFile, $controlStructurePointer) : void {
        $this->minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
        if ($this->shouldBeSkipped($phpcsFile, $controlStructurePointer)) {
            return;
        }
        $tokens = $phpcsFile->getTokens();
        $parenthesisOpenerPointer = $tokens[$controlStructurePointer]['parenthesis_opener'];
        $parenthesisCloserPointer = $tokens[$controlStructurePointer]['parenthesis_closer'];
        $booleanOperatorPointers = TokenHelper::findNextAll($phpcsFile, Tokens::$booleanOperators, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer);
        if ($booleanOperatorPointers === []) {
            return;
        }
        $conditionStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
        $conditionEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1);
        $conditionStartsOnNewLine = $tokens[$parenthesisOpenerPointer]['line'] !== $tokens[$conditionStartPointer]['line'];
        $conditionEndsOnNewLine = $tokens[$parenthesisCloserPointer]['line'] !== $tokens[$conditionEndPointer]['line'];
        $lineStart = $this->getLineStart($phpcsFile, $conditionStartsOnNewLine ? $conditionStartPointer - 1 : $parenthesisOpenerPointer);
        $lineEnd = $this->getLineEnd($phpcsFile, $conditionEndsOnNewLine ? $conditionEndPointer + 1 : $parenthesisCloserPointer);
        $condition = $this->getCondition($phpcsFile, $parenthesisOpenerPointer, $parenthesisCloserPointer);
        $lineLength = strlen($lineStart . $condition . $lineEnd);
        $conditionLinesCount = $tokens[$conditionEndPointer]['line'] - $tokens[$conditionStartPointer]['line'] + 1;
        if (!$this->shouldReportError($lineLength, $conditionLinesCount, count($booleanOperatorPointers))) {
            return;
        }
        $fix = $phpcsFile->addFixableError(sprintf('Condition of "%s" should be split to more lines so each condition part is on its own line.', $this->getControlStructureName($phpcsFile, $controlStructurePointer)), $controlStructurePointer, self::CODE_REQUIRED_MULTI_LINE_CONDITION);
        if (!$fix) {
            return;
        }
        $controlStructureIndentation = IndentationHelper::getIndentation($phpcsFile, $conditionStartsOnNewLine ? $conditionStartPointer : TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $parenthesisOpenerPointer));
        $conditionIndentation = $conditionStartsOnNewLine ? $controlStructureIndentation : IndentationHelper::addIndentation($controlStructureIndentation);
        $innerConditionLevel = 0;
        $phpcsFile->fixer
            ->beginChangeset();
        if (!$conditionStartsOnNewLine) {
            FixerHelper::removeWhitespaceBefore($phpcsFile, $conditionStartPointer);
            $phpcsFile->fixer
                ->addContentBefore($conditionStartPointer, $phpcsFile->eolChar . $conditionIndentation);
        }
        for ($i = $conditionStartPointer; $i <= $conditionEndPointer; $i++) {
            if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
                $containsBooleanOperator = TokenHelper::findNext($phpcsFile, Tokens::$booleanOperators, $i + 1, $tokens[$i]['parenthesis_closer']) !== null;
                $innerConditionLevel++;
                if ($containsBooleanOperator) {
                    FixerHelper::removeWhitespaceAfter($phpcsFile, $i);
                    $phpcsFile->fixer
                        ->addContent($i, $phpcsFile->eolChar . IndentationHelper::addIndentation($conditionIndentation, $innerConditionLevel));
                    FixerHelper::removeWhitespaceBefore($phpcsFile, $tokens[$i]['parenthesis_closer']);
                    $phpcsFile->fixer
                        ->addContentBefore($tokens[$i]['parenthesis_closer'], $phpcsFile->eolChar . IndentationHelper::addIndentation($conditionIndentation, $innerConditionLevel - 1));
                }
                continue;
            }
            if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
                $innerConditionLevel--;
                continue;
            }
            if (!in_array($tokens[$i]['code'], Tokens::$booleanOperators, true)) {
                continue;
            }
            $innerConditionIndentation = $conditionIndentation;
            if ($innerConditionLevel > 0) {
                $innerConditionIndentation = IndentationHelper::addIndentation($innerConditionIndentation, $innerConditionLevel);
            }
            if ($this->booleanOperatorOnPreviousLine) {
                $phpcsFile->fixer
                    ->addContent($i, $phpcsFile->eolChar . $innerConditionIndentation);
                FixerHelper::removeWhitespaceAfter($phpcsFile, $i);
                continue;
            }
            FixerHelper::removeWhitespaceBefore($phpcsFile, $i);
            $phpcsFile->fixer
                ->addContentBefore($i, $phpcsFile->eolChar . $innerConditionIndentation);
        }
        if (!$conditionEndsOnNewLine) {
            FixerHelper::removeWhitespaceAfter($phpcsFile, $conditionEndPointer);
            $phpcsFile->fixer
                ->addContent($conditionEndPointer, $phpcsFile->eolChar . $controlStructureIndentation);
        }
        $phpcsFile->fixer
            ->endChangeset();
    }
    private function shouldReportError(int $lineLength, int $conditionLinesCount, int $booleanOperatorPointersCount) : bool {
        if ($conditionLinesCount === 1) {
            return $this->minLineLength === 0 || $lineLength >= $this->minLineLength;
        }
        return $this->alwaysSplitAllConditionParts ? $conditionLinesCount < $booleanOperatorPointersCount + 1 : false;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractLineCondition::$checkedControlStructures public property @var list&lt;string&gt;
AbstractLineCondition::DO_CONTROL_STRUCTURE protected constant
AbstractLineCondition::getCondition protected function
AbstractLineCondition::getControlStructureName protected function
AbstractLineCondition::getLineEnd protected function
AbstractLineCondition::getLineStart protected function
AbstractLineCondition::IF_CONTROL_STRUCTURE protected constant
AbstractLineCondition::isPartOfDo protected function
AbstractLineCondition::register public function * Overrides Sniff::register
AbstractLineCondition::shouldBeSkipped protected function
AbstractLineCondition::WHILE_CONTROL_STRUCTURE protected constant
RequireMultiLineConditionSniff::$alwaysSplitAllConditionParts public property @var bool
RequireMultiLineConditionSniff::$booleanOperatorOnPreviousLine public property @var bool
RequireMultiLineConditionSniff::$minLineLength public property @var int
RequireMultiLineConditionSniff::CODE_REQUIRED_MULTI_LINE_CONDITION public constant
RequireMultiLineConditionSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
RequireMultiLineConditionSniff::shouldReportError private function
RSS feed
Powered by Drupal