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

Breadcrumb

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

function DisallowStringExpressionPropertyFetchSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $objectOperatorPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/DisallowStringExpressionPropertyFetchSniff.php, line 32

Class

DisallowStringExpressionPropertyFetchSniff

Namespace

SlevomatCodingStandard\Sniffs\Classes

Code

public function process(File $phpcsFile, $objectOperatorPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $curlyBracketOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $objectOperatorPointer + 1);
    if ($tokens[$curlyBracketOpenerPointer]['code'] !== T_OPEN_CURLY_BRACKET) {
        return;
    }
    $curlyBracketCloserPointer = $tokens[$curlyBracketOpenerPointer]['bracket_closer'];
    if (TokenHelper::findNextExcluding($phpcsFile, T_CONSTANT_ENCAPSED_STRING, $curlyBracketOpenerPointer + 1, $curlyBracketCloserPointer) !== null) {
        return;
    }
    $pointerAfterCurlyBracketCloser = TokenHelper::findNextEffective($phpcsFile, $curlyBracketCloserPointer + 1);
    if ($tokens[$pointerAfterCurlyBracketCloser]['code'] === T_OPEN_PARENTHESIS) {
        return;
    }
    if (preg_match('~^(["\'])([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\1$~', $tokens[$curlyBracketOpenerPointer + 1]['content'], $matches) !== 1) {
        return;
    }
    $fix = $phpcsFile->addFixableError('String expression property fetch is disallowed, use identifier property fetch.', $curlyBracketOpenerPointer, self::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::change($phpcsFile, $curlyBracketOpenerPointer, $curlyBracketCloserPointer, $matches[2]);
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal