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

Breadcrumb

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

class RequireShortTernaryOperatorSniff

Hierarchy

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

Expanded class hierarchy of RequireShortTernaryOperatorSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class RequireShortTernaryOperatorSniff implements Sniff {
    public const CODE_REQUIRED_SHORT_TERNARY_OPERATOR = 'RequiredShortTernaryOperator';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_INLINE_THEN,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $inlineThenPointer
     */
    public function process(File $phpcsFile, $inlineThenPointer) : void {
        $tokens = $phpcsFile->getTokens();
        $nextPointer = TokenHelper::findNextEffective($phpcsFile, $inlineThenPointer + 1);
        if ($tokens[$nextPointer]['code'] === T_INLINE_ELSE) {
            return;
        }
        $conditionStartPointer = TernaryOperatorHelper::getStartPointer($phpcsFile, $inlineThenPointer);
        $inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
        $inlineElseEndPointer = TernaryOperatorHelper::getEndPointer($phpcsFile, $inlineThenPointer, $inlineElsePointer);
        $thenContent = trim(TokenHelper::getContent($phpcsFile, $inlineThenPointer + 1, $inlineElsePointer - 1));
        $elseContent = trim(TokenHelper::getContent($phpcsFile, $inlineElsePointer + 1, $inlineElseEndPointer));
        $conditionEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1);
        $condition = TokenHelper::getContent($phpcsFile, $conditionStartPointer, $conditionEndPointer);
        if ($tokens[$conditionStartPointer]['code'] === T_BOOLEAN_NOT) {
            if ($elseContent !== ltrim($condition, '!')) {
                return;
            }
        }
        else {
            if ($thenContent !== $condition) {
                return;
            }
        }
        $fix = $phpcsFile->addFixableError('Use short ternary operator.', $inlineThenPointer, self::CODE_REQUIRED_SHORT_TERNARY_OPERATOR);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        if ($tokens[$conditionStartPointer]['code'] === T_BOOLEAN_NOT) {
            $phpcsFile->fixer
                ->replaceToken($conditionStartPointer, '');
            FixerHelper::change($phpcsFile, $inlineThenPointer, $inlineElseEndPointer, sprintf('?: %s', $thenContent));
        }
        else {
            FixerHelper::removeBetween($phpcsFile, $inlineThenPointer, $inlineElsePointer);
        }
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RequireShortTernaryOperatorSniff::CODE_REQUIRED_SHORT_TERNARY_OPERATOR public constant
RequireShortTernaryOperatorSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
RequireShortTernaryOperatorSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal