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

Breadcrumb

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

class JumpStatementsSpacingSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\ControlStructures\AbstractControlStructureSpacing implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \SlevomatCodingStandard\Sniffs\ControlStructures\JumpStatementsSpacingSniff extends \SlevomatCodingStandard\Sniffs\ControlStructures\AbstractControlStructureSpacing

Expanded class hierarchy of JumpStatementsSpacingSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/JumpStatementsSpacingSniff.php, line 25

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class JumpStatementsSpacingSniff extends AbstractControlStructureSpacing {
    
    /** @var int */
    public $linesCountBefore = 1;
    
    /** @var int */
    public $linesCountBeforeFirst = 0;
    
    /** @var int|null */
    public $linesCountBeforeWhenFirstInCaseOrDefault = null;
    
    /** @var int */
    public $linesCountAfter = 1;
    
    /** @var int */
    public $linesCountAfterLast = 0;
    
    /** @var int|null */
    public $linesCountAfterWhenLastInCaseOrDefault = null;
    
    /** @var int|null */
    public $linesCountAfterWhenLastInLastCaseOrDefault = null;
    
    /** @var bool */
    public $allowSingleLineYieldStacking = true;
    
    /** @var list<string> */
    public $jumpStatements = [];
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $jumpStatementPointer
     */
    public function process(File $phpcsFile, $jumpStatementPointer) : void {
        $this->linesCountBefore = SniffSettingsHelper::normalizeInteger($this->linesCountBefore);
        $this->linesCountBeforeFirst = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirst);
        $this->linesCountBeforeWhenFirstInCaseOrDefault = SniffSettingsHelper::normalizeNullableInteger($this->linesCountBeforeWhenFirstInCaseOrDefault);
        $this->linesCountAfter = SniffSettingsHelper::normalizeInteger($this->linesCountAfter);
        $this->linesCountAfterLast = SniffSettingsHelper::normalizeInteger($this->linesCountAfterLast);
        $this->linesCountAfterWhenLastInCaseOrDefault = SniffSettingsHelper::normalizeNullableInteger($this->linesCountAfterWhenLastInCaseOrDefault);
        $this->linesCountAfterWhenLastInLastCaseOrDefault = SniffSettingsHelper::normalizeNullableInteger($this->linesCountAfterWhenLastInLastCaseOrDefault);
        if ($this->isOneOfYieldSpecialCases($phpcsFile, $jumpStatementPointer)) {
            return;
        }
        parent::process($phpcsFile, $jumpStatementPointer);
    }
    
    /**
     * @return list<string>
     */
    protected function getSupportedKeywords() : array {
        return [
            self::KEYWORD_GOTO,
            self::KEYWORD_BREAK,
            self::KEYWORD_CONTINUE,
            self::KEYWORD_RETURN,
            self::KEYWORD_THROW,
            self::KEYWORD_YIELD,
            self::KEYWORD_YIELD_FROM,
        ];
    }
    
    /**
     * @return list<string>
     */
    protected function getKeywordsToCheck() : array {
        return $this->jumpStatements;
    }
    protected function getLinesCountBefore() : int {
        return $this->linesCountBefore;
    }
    protected function getLinesCountBeforeFirst(File $phpcsFile, int $jumpStatementPointer) : int {
        if ($this->linesCountBeforeWhenFirstInCaseOrDefault !== null && $this->isFirstInCaseOrDefault($phpcsFile, $jumpStatementPointer)) {
            return $this->linesCountBeforeWhenFirstInCaseOrDefault;
        }
        return $this->linesCountBeforeFirst;
    }
    protected function getLinesCountAfter() : int {
        return $this->linesCountAfter;
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
     */
    protected function getLinesCountAfterLast(File $phpcsFile, int $jumpStatementPointer, int $jumpStatementEndPointer) : int {
        if ($this->linesCountAfterWhenLastInLastCaseOrDefault !== null && $this->isLastInLastCaseOrDefault($phpcsFile, $jumpStatementEndPointer)) {
            return $this->linesCountAfterWhenLastInLastCaseOrDefault;
        }
        if ($this->linesCountAfterWhenLastInCaseOrDefault !== null && $this->isLastInCaseOrDefault($phpcsFile, $jumpStatementEndPointer)) {
            return $this->linesCountAfterWhenLastInCaseOrDefault;
        }
        return $this->linesCountAfterLast;
    }
    protected function checkLinesBefore(File $phpcsFile, int $jumpStatementPointer) : void {
        if ($this->allowSingleLineYieldStacking && $this->isStackedSingleLineYield($phpcsFile, $jumpStatementPointer, true)) {
            return;
        }
        if ($this->isThrowExpression($phpcsFile, $jumpStatementPointer)) {
            return;
        }
        parent::checkLinesBefore($phpcsFile, $jumpStatementPointer);
    }
    protected function checkLinesAfter(File $phpcsFile, int $jumpStatementPointer) : void {
        if ($this->allowSingleLineYieldStacking && $this->isStackedSingleLineYield($phpcsFile, $jumpStatementPointer, false)) {
            return;
        }
        if ($this->isThrowExpression($phpcsFile, $jumpStatementPointer)) {
            return;
        }
        parent::checkLinesAfter($phpcsFile, $jumpStatementPointer);
    }
    private function isOneOfYieldSpecialCases(File $phpcsFile, int $jumpStatementPointer) : bool {
        $tokens = $phpcsFile->getTokens();
        $jumpStatementToken = $tokens[$jumpStatementPointer];
        if ($jumpStatementToken['code'] !== T_YIELD && $jumpStatementToken['code'] !== T_YIELD_FROM) {
            return false;
        }
        // check if yield is used inside parentheses (function call, while, ...)
        if (array_key_exists('nested_parenthesis', $jumpStatementToken)) {
            return true;
        }
        $pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $jumpStatementPointer - 1);
        // check if yield is used in assignment
        if (in_array($tokens[$pointerBefore]['code'], Tokens::$assignmentTokens, true)) {
            return true;
        }
        // check if yield is used in a return statement
        return $tokens[$pointerBefore]['code'] === T_RETURN;
    }
    private function isStackedSingleLineYield(File $phpcsFile, int $jumpStatementPointer, bool $previous) : bool {
        $tokens = $phpcsFile->getTokens();
        $yields = [
            T_YIELD,
            T_YIELD_FROM,
        ];
        if (!in_array($tokens[$jumpStatementPointer]['code'], $yields, true)) {
            return false;
        }
        $adjoiningYieldPointer = $previous ? TokenHelper::findPrevious($phpcsFile, $yields, $jumpStatementPointer - 1) : TokenHelper::findNext($phpcsFile, $yields, $jumpStatementPointer + 1);
        return $adjoiningYieldPointer !== null && abs($tokens[$adjoiningYieldPointer]['line'] - $tokens[$jumpStatementPointer]['line']) === 1;
    }
    private function isThrowExpression(File $phpcsFile, int $jumpStatementPointer) : bool {
        $tokens = $phpcsFile->getTokens();
        if ($tokens[$jumpStatementPointer]['code'] !== T_THROW) {
            return false;
        }
        $pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $jumpStatementPointer - 1);
        return !in_array($tokens[$pointerBefore]['code'], [
            T_SEMICOLON,
            T_COLON,
            T_OPEN_CURLY_BRACKET,
            T_CLOSE_CURLY_BRACKET,
            T_OPEN_TAG,
        ], true);
    }
    private function isFirstInCaseOrDefault(File $phpcsFile, int $jumpStatementPointer) : bool {
        $tokens = $phpcsFile->getTokens();
        $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $jumpStatementPointer - 1);
        if ($tokens[$previousPointer]['code'] !== T_COLON) {
            return false;
        }
        $firstPointerOnLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $previousPointer);
        return in_array($tokens[$firstPointerOnLine]['code'], [
            T_CASE,
            T_DEFAULT,
        ], true);
    }
    private function isLastInCaseOrDefault(File $phpcsFile, int $jumpStatementEndPointer) : bool {
        $tokens = $phpcsFile->getTokens();
        $nextPointer = TokenHelper::findNextEffective($phpcsFile, $jumpStatementEndPointer + 1);
        if (in_array($tokens[$nextPointer]['code'], [
            T_CASE,
            T_DEFAULT,
        ], true)) {
            return true;
        }
        return $tokens[$nextPointer]['code'] === T_CLOSE_CURLY_BRACKET && array_key_exists('scope_condition', $tokens[$nextPointer]) && $tokens[$tokens[$nextPointer]['scope_condition']]['code'] === T_SWITCH;
    }
    private function isLastInLastCaseOrDefault(File $phpcsFile, int $jumpStatementEndPointer) : bool {
        if (!$this->isLastInCaseOrDefault($phpcsFile, $jumpStatementEndPointer)) {
            return false;
        }
        $nextPointer = TokenHelper::findNextEffective($phpcsFile, $jumpStatementEndPointer + 1);
        return !in_array($phpcsFile->getTokens()[$nextPointer]['code'], [
            T_CASE,
            T_DEFAULT,
        ], true);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractControlStructureSpacing::$tokensToCheck private property @var array&lt;(string|int)&gt;|null
AbstractControlStructureSpacing::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE public constant
AbstractControlStructureSpacing::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE public constant
AbstractControlStructureSpacing::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE public constant
AbstractControlStructureSpacing::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE public constant
AbstractControlStructureSpacing::findControlStructureEnd private function
AbstractControlStructureSpacing::getTokensToCheck private function *
AbstractControlStructureSpacing::KEYWORD_BREAK protected constant
AbstractControlStructureSpacing::KEYWORD_CASE protected constant
AbstractControlStructureSpacing::KEYWORD_CONTINUE protected constant
AbstractControlStructureSpacing::KEYWORD_DEFAULT protected constant
AbstractControlStructureSpacing::KEYWORD_DO protected constant
AbstractControlStructureSpacing::KEYWORD_FOR protected constant
AbstractControlStructureSpacing::KEYWORD_FOREACH protected constant
AbstractControlStructureSpacing::KEYWORD_GOTO protected constant
AbstractControlStructureSpacing::KEYWORD_IF protected constant
AbstractControlStructureSpacing::KEYWORD_PARENT protected constant
AbstractControlStructureSpacing::KEYWORD_RETURN protected constant
AbstractControlStructureSpacing::KEYWORD_SWITCH protected constant
AbstractControlStructureSpacing::KEYWORD_THROW protected constant
AbstractControlStructureSpacing::KEYWORD_TRY protected constant
AbstractControlStructureSpacing::KEYWORD_WHILE protected constant
AbstractControlStructureSpacing::KEYWORD_YIELD protected constant
AbstractControlStructureSpacing::KEYWORD_YIELD_FROM protected constant
AbstractControlStructureSpacing::register public function * Overrides Sniff::register
JumpStatementsSpacingSniff::$allowSingleLineYieldStacking public property @var bool
JumpStatementsSpacingSniff::$jumpStatements public property @var list&lt;string&gt;
JumpStatementsSpacingSniff::$linesCountAfter public property @var int
JumpStatementsSpacingSniff::$linesCountAfterLast public property @var int
JumpStatementsSpacingSniff::$linesCountAfterWhenLastInCaseOrDefault public property @var int|null
JumpStatementsSpacingSniff::$linesCountAfterWhenLastInLastCaseOrDefault public property @var int|null
JumpStatementsSpacingSniff::$linesCountBefore public property @var int
JumpStatementsSpacingSniff::$linesCountBeforeFirst public property @var int
JumpStatementsSpacingSniff::$linesCountBeforeWhenFirstInCaseOrDefault public property @var int|null
JumpStatementsSpacingSniff::checkLinesAfter protected function Overrides AbstractControlStructureSpacing::checkLinesAfter
JumpStatementsSpacingSniff::checkLinesBefore protected function Overrides AbstractControlStructureSpacing::checkLinesBefore
JumpStatementsSpacingSniff::getKeywordsToCheck protected function * Overrides AbstractControlStructureSpacing::getKeywordsToCheck
JumpStatementsSpacingSniff::getLinesCountAfter protected function Overrides AbstractControlStructureSpacing::getLinesCountAfter
JumpStatementsSpacingSniff::getLinesCountAfterLast protected function * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter Overrides AbstractControlStructureSpacing::getLinesCountAfterLast
JumpStatementsSpacingSniff::getLinesCountBefore protected function Overrides AbstractControlStructureSpacing::getLinesCountBefore
JumpStatementsSpacingSniff::getLinesCountBeforeFirst protected function Overrides AbstractControlStructureSpacing::getLinesCountBeforeFirst
JumpStatementsSpacingSniff::getSupportedKeywords protected function * Overrides AbstractControlStructureSpacing::getSupportedKeywords
JumpStatementsSpacingSniff::isFirstInCaseOrDefault private function
JumpStatementsSpacingSniff::isLastInCaseOrDefault private function
JumpStatementsSpacingSniff::isLastInLastCaseOrDefault private function
JumpStatementsSpacingSniff::isOneOfYieldSpecialCases private function
JumpStatementsSpacingSniff::isStackedSingleLineYield private function
JumpStatementsSpacingSniff::isThrowExpression private function
JumpStatementsSpacingSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides AbstractControlStructureSpacing::process
RSS feed
Powered by Drupal