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

Breadcrumb

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

class RequireSingleLineConditionSniff

Hierarchy

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

Expanded class hierarchy of RequireSingleLineConditionSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireSingleLineConditionSniff.php, line 13

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class RequireSingleLineConditionSniff extends AbstractLineCondition {
    public const CODE_REQUIRED_SINGLE_LINE_CONDITION = 'RequiredSingleLineCondition';
    
    /** @var int */
    public $maxLineLength = 120;
    
    /** @var bool */
    public $alwaysForSimpleConditions = true;
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $controlStructurePointer
     */
    public function process(File $phpcsFile, $controlStructurePointer) : void {
        $this->maxLineLength = SniffSettingsHelper::normalizeInteger($this->maxLineLength);
        if ($this->shouldBeSkipped($phpcsFile, $controlStructurePointer)) {
            return;
        }
        $tokens = $phpcsFile->getTokens();
        $parenthesisOpenerPointer = $tokens[$controlStructurePointer]['parenthesis_opener'];
        $parenthesisCloserPointer = $tokens[$controlStructurePointer]['parenthesis_closer'];
        if ($tokens[$parenthesisOpenerPointer]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
            return;
        }
        if (TokenHelper::findNext($phpcsFile, TokenHelper::$inlineCommentTokenCodes, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) !== null) {
            return;
        }
        $lineStart = $this->getLineStart($phpcsFile, $parenthesisOpenerPointer);
        $condition = $this->getCondition($phpcsFile, $parenthesisOpenerPointer, $parenthesisCloserPointer);
        $lineEnd = $this->getLineEnd($phpcsFile, $parenthesisCloserPointer);
        $lineLength = strlen($lineStart . $condition . $lineEnd);
        $isSimpleCondition = TokenHelper::findNext($phpcsFile, Tokens::$booleanOperators, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) === null;
        if (!$this->shouldReportError($lineLength, $isSimpleCondition)) {
            return;
        }
        $fix = $phpcsFile->addFixableError(sprintf('Condition of "%s" should be placed on a single line.', $this->getControlStructureName($phpcsFile, $controlStructurePointer)), $controlStructurePointer, self::CODE_REQUIRED_SINGLE_LINE_CONDITION);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->addContent($parenthesisOpenerPointer, $condition);
        FixerHelper::removeBetween($phpcsFile, $parenthesisOpenerPointer, $parenthesisCloserPointer);
        $phpcsFile->fixer
            ->endChangeset();
    }
    private function shouldReportError(int $lineLength, bool $isSimpleCondition) : bool {
        if ($this->maxLineLength === 0) {
            return true;
        }
        if ($lineLength <= $this->maxLineLength) {
            return true;
        }
        return $isSimpleCondition && $this->alwaysForSimpleConditions;
    }

}

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
RequireSingleLineConditionSniff::$alwaysForSimpleConditions public property @var bool
RequireSingleLineConditionSniff::$maxLineLength public property @var int
RequireSingleLineConditionSniff::CODE_REQUIRED_SINGLE_LINE_CONDITION public constant
RequireSingleLineConditionSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
RequireSingleLineConditionSniff::shouldReportError private function
RSS feed
Powered by Drupal