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

Breadcrumb

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

function RequireNonCapturingCatchSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $catchPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Exceptions/RequireNonCapturingCatchSniff.php, line 46

Class

RequireNonCapturingCatchSniff

Namespace

SlevomatCodingStandard\Sniffs\Exceptions

Code

public function process(File $phpcsFile, $catchPointer) : void {
    $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 80000);
    if (!$this->enable) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $variablePointer = TokenHelper::findNext($phpcsFile, T_VARIABLE, $tokens[$catchPointer]['parenthesis_opener'], $tokens[$catchPointer]['parenthesis_closer']);
    if ($variablePointer === null) {
        return;
    }
    $variableName = $tokens[$variablePointer]['content'];
    if ($this->isVariableUsedInCodePart($phpcsFile, $tokens[$catchPointer]['scope_opener'], $tokens[$catchPointer]['scope_closer'], $variableName)) {
        return;
    }
    $tryEndPointer = CatchHelper::getTryEndPointer($phpcsFile, $catchPointer);
    $possibleFinallyPointer = $tokens[$tryEndPointer]['scope_condition'];
    if ($tokens[$possibleFinallyPointer]['code'] === T_FINALLY && $this->isVariableUsedInCodePart($phpcsFile, $tokens[$possibleFinallyPointer]['scope_opener'], $tokens[$possibleFinallyPointer]['scope_closer'], $variableName)) {
        return;
    }
    $nextScopeEnd = count($tokens) - 1;
    foreach (array_reverse($tokens[$tryEndPointer]['conditions'], true) as $conditionPointer => $conditionCode) {
        if (in_array($conditionCode, TokenHelper::$functionTokenCodes, true)) {
            $nextScopeEnd = $tokens[$conditionPointer]['scope_closer'];
            break;
        }
    }
    if ($this->isVariableUsedInCodePart($phpcsFile, $tryEndPointer, $nextScopeEnd, $variableName)) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Non-capturing catch is required.', $catchPointer, self::CODE_NON_CAPTURING_CATCH_REQUIRED);
    if (!$fix) {
        return;
    }
    $pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
    $fixEndPointer = TokenHelper::findNextContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $variablePointer + 1, $tokens[$catchPointer]['parenthesis_closer']);
    if ($fixEndPointer === null) {
        $fixEndPointer = $tokens[$catchPointer]['parenthesis_closer'];
    }
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeVariable, $fixEndPointer);
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal