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

Breadcrumb

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

class DisallowStringExpressionPropertyFetchSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Classes\DisallowStringExpressionPropertyFetchSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DisallowStringExpressionPropertyFetchSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\Classes
View source
class DisallowStringExpressionPropertyFetchSniff implements Sniff {
    public const CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH = 'DisallowedStringExpressionPropertyFetch';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_OBJECT_OPERATOR,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $objectOperatorPointer
     */
    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();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DisallowStringExpressionPropertyFetchSniff::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH public constant
DisallowStringExpressionPropertyFetchSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
DisallowStringExpressionPropertyFetchSniff::register public function * Overrides Sniff::register

API Navigation

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