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

Breadcrumb

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

function LanguageConstructWithParenthesesSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $languageConstructPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/LanguageConstructWithParenthesesSniff.php, line 60

Class

LanguageConstructWithParenthesesSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $languageConstructPointer) : void {
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $openParenthesisPointer */
    $openParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $languageConstructPointer + 1);
    if ($tokens[$openParenthesisPointer]['code'] !== T_OPEN_PARENTHESIS) {
        return;
    }
    $closeParenthesisPointer = $tokens[$openParenthesisPointer]['parenthesis_closer'];
    $afterCloseParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $closeParenthesisPointer + 1);
    if (!in_array($tokens[$afterCloseParenthesisPointer]['code'], [
        T_SEMICOLON,
        T_CLOSE_PARENTHESIS,
        T_CLOSE_SHORT_ARRAY,
    ], true)) {
        return;
    }
    $containsContentBetweenParentheses = TokenHelper::findNextEffective($phpcsFile, $openParenthesisPointer + 1, $closeParenthesisPointer) !== null;
    if ($tokens[$languageConstructPointer]['code'] === T_EXIT && $containsContentBetweenParentheses) {
        return;
    }
    $fix = $phpcsFile->addFixableError(sprintf('Usage of language construct "%s" with parentheses is disallowed.', $tokens[$languageConstructPointer]['content']), $languageConstructPointer, self::CODE_USED_WITH_PARENTHESES);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->replaceToken($openParenthesisPointer, '');
    if ($tokens[$openParenthesisPointer - 1]['code'] !== T_WHITESPACE && $containsContentBetweenParentheses) {
        $phpcsFile->fixer
            ->addContent($openParenthesisPointer, ' ');
    }
    $phpcsFile->fixer
        ->replaceToken($closeParenthesisPointer, '');
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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