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

Breadcrumb

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

function NewWithParenthesesSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $newPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/NewWithParenthesesSniff.php, line 42

Class

NewWithParenthesesSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $newPointer) : void {
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $nextPointer */
    $nextPointer = TokenHelper::findNextEffective($phpcsFile, $newPointer + 1);
    if ($tokens[$nextPointer]['code'] === T_ATTRIBUTE) {
        $nextPointer = AttributeHelper::getAttributeTarget($phpcsFile, $nextPointer);
    }
    if ($tokens[$nextPointer]['code'] === T_ANON_CLASS) {
        return;
    }
    if ($tokens[$nextPointer]['code'] === T_OPEN_PARENTHESIS) {
        $nextPointer = $tokens[$nextPointer]['parenthesis_closer'];
    }
    $shouldBeOpenParenthesisPointer = $nextPointer + 1;
    do {
        $shouldBeOpenParenthesisPointer = 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,
        ], $shouldBeOpenParenthesisPointer);
        if ($shouldBeOpenParenthesisPointer === null || $tokens[$shouldBeOpenParenthesisPointer]['code'] !== T_CLOSE_SQUARE_BRACKET || $tokens[$shouldBeOpenParenthesisPointer]['bracket_opener'] <= $newPointer) {
            break;
        }
        $shouldBeOpenParenthesisPointer++;
    } while (true);
    if ($shouldBeOpenParenthesisPointer !== null && $tokens[$shouldBeOpenParenthesisPointer]['code'] === T_OPEN_PARENTHESIS) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Usage of "new" without parentheses is disallowed.', $newPointer, self::CODE_MISSING_PARENTHESES);
    if (!$fix) {
        return;
    }
    
    /** @var int $classNameEndPointer */
    $classNameEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $shouldBeOpenParenthesisPointer - 1);
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->addContent($classNameEndPointer, '()');
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal