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

Breadcrumb

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

class NewWithoutParenthesesSniff

Hierarchy

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

Expanded class hierarchy of NewWithoutParenthesesSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/NewWithoutParenthesesSniff.php, line 22

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures
View source
class NewWithoutParenthesesSniff implements Sniff {
    public const CODE_USELESS_PARENTHESES = 'UselessParentheses';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_NEW,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $newPointer
     */
    public function process(File $phpcsFile, $newPointer) : void {
        $tokens = $phpcsFile->getTokens();
        
        /** @var int $nextPointer */
        $nextPointer = TokenHelper::findNextEffective($phpcsFile, $newPointer + 1);
        if ($tokens[$nextPointer]['code'] === T_ANON_CLASS) {
            return;
        }
        $parenthesisOpenerPointer = $nextPointer + 1;
        do {
            
            /** @var int $parenthesisOpenerPointer */
            $parenthesisOpenerPointer = TokenHelper::findNext($phpcsFile, [
                T_OPEN_PARENTHESIS,
                T_SEMICOLON,
                T_COMMA,
                T_INLINE_THEN,
                T_INLINE_ELSE,
                T_COALESCE,
                T_CLOSE_SHORT_ARRAY,
                T_CLOSE_SQUARE_BRACKET,
                T_CLOSE_PARENTHESIS,
                T_DOUBLE_ARROW,
            ], $parenthesisOpenerPointer);
            if ($tokens[$parenthesisOpenerPointer]['code'] !== T_CLOSE_SQUARE_BRACKET || $tokens[$parenthesisOpenerPointer]['bracket_opener'] <= $newPointer) {
                break;
            }
            $parenthesisOpenerPointer++;
        } while (true);
        if ($tokens[$parenthesisOpenerPointer]['code'] !== T_OPEN_PARENTHESIS) {
            return;
        }
        $nextPointer = TokenHelper::findNextNonWhitespace($phpcsFile, $parenthesisOpenerPointer + 1);
        if ($nextPointer !== $tokens[$parenthesisOpenerPointer]['parenthesis_closer']) {
            return;
        }
        $fix = $phpcsFile->addFixableError('Useless parentheses in "new".', $newPointer, self::CODE_USELESS_PARENTHESES);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        FixerHelper::removeBetweenIncluding($phpcsFile, $parenthesisOpenerPointer, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']);
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

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