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

Breadcrumb

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

class DisallowReferenceSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\PHP\DisallowReferenceSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DisallowReferenceSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/PHP/DisallowReferenceSniff.php, line 20

Namespace

SlevomatCodingStandard\Sniffs\PHP
View source
class DisallowReferenceSniff implements Sniff {
    public const CODE_DISALLOWED_PASSING_BY_REFERENCE = 'DisallowedPassingByReference';
    public const CODE_DISALLOWED_RETURNING_REFERENCE = 'DisallowedReturningReference';
    public const CODE_DISALLOWED_INHERITING_VARIABLE_BY_REFERENCE = 'DisallowedInheritingVariableByReference';
    public const CODE_DISALLOWED_ASSIGNING_BY_REFERENCE = 'DisallowedAssigningByReference';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_BITWISE_AND,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $referencePointer
     */
    public function process(File $phpcsFile, $referencePointer) : void {
        $tokens = $phpcsFile->getTokens();
        $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $referencePointer - 1);
        if (in_array($tokens[$previousPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
            $phpcsFile->addError('Returning reference is disallowed.', $referencePointer, self::CODE_DISALLOWED_RETURNING_REFERENCE);
            return;
        }
        $previousParenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $referencePointer - 1);
        if ($previousParenthesisOpenerPointer !== null && $tokens[$previousParenthesisOpenerPointer]['parenthesis_closer'] > $referencePointer) {
            if (array_key_exists('parenthesis_owner', $tokens[$previousParenthesisOpenerPointer])) {
                $parenthesisOwnerPointer = $tokens[$previousParenthesisOpenerPointer]['parenthesis_owner'];
                if (in_array($tokens[$parenthesisOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
                    $phpcsFile->addError('Passing by reference is disallowed.', $referencePointer, self::CODE_DISALLOWED_PASSING_BY_REFERENCE);
                    return;
                }
            }
            $pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $previousParenthesisOpenerPointer - 1);
            if ($pointerBeforeParenthesisOpener !== null && $tokens[$pointerBeforeParenthesisOpener]['code'] === T_USE) {
                $phpcsFile->addError('Inheriting variable by reference is disallowed.', $referencePointer, self::CODE_DISALLOWED_INHERITING_VARIABLE_BY_REFERENCE);
                return;
            }
        }
        
        /** @var int $variableStartPointer */
        $variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $referencePointer + 1);
        $variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
        if ($variableEndPointer === null) {
            return;
        }
        $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $referencePointer - 1);
        if (!in_array($tokens[$previousPointer]['code'], [
            T_EQUAL,
            T_DOUBLE_ARROW,
            T_OPEN_SHORT_ARRAY,
            T_COMMA,
            T_AS,
        ], true)) {
            return;
        }
        $phpcsFile->addError('Assigning by reference is disallowed.', $referencePointer, self::CODE_DISALLOWED_ASSIGNING_BY_REFERENCE);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DisallowReferenceSniff::CODE_DISALLOWED_ASSIGNING_BY_REFERENCE public constant
DisallowReferenceSniff::CODE_DISALLOWED_INHERITING_VARIABLE_BY_REFERENCE public constant
DisallowReferenceSniff::CODE_DISALLOWED_PASSING_BY_REFERENCE public constant
DisallowReferenceSniff::CODE_DISALLOWED_RETURNING_REFERENCE public constant
DisallowReferenceSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
DisallowReferenceSniff::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