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

Breadcrumb

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

function ForbiddenFunctionsSniff::process

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token in: the stack passed in $tokens.

Return value

void

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php, line 118

Class

ForbiddenFunctionsSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\PHP

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $ignore = [
        T_DOUBLE_COLON => true,
        T_OBJECT_OPERATOR => true,
        T_NULLSAFE_OBJECT_OPERATOR => true,
        T_FUNCTION => true,
        T_CONST => true,
        T_PUBLIC => true,
        T_PRIVATE => true,
        T_PROTECTED => true,
        T_AS => true,
        T_NEW => true,
        T_INSTEADOF => true,
        T_NS_SEPARATOR => true,
        T_IMPLEMENTS => true,
    ];
    $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
    // If function call is directly preceded by a NS_SEPARATOR it points to the
    // global namespace, so we should still catch it.
    if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $prevToken - 1, null, true);
        if ($tokens[$prevToken]['code'] === T_STRING) {
            // Not in the global namespace.
            return;
        }
    }
    if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
        // Not a call to a PHP function.
        return;
    }
    $nextToken = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
        // Not a call to a PHP function.
        return;
    }
    if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
        // Not a call to a PHP function.
        return;
    }
    if (empty($tokens[$stackPtr]['nested_attributes']) === false) {
        // Class instantiation in attribute, not function call.
        return;
    }
    $function = strtolower($tokens[$stackPtr]['content']);
    $pattern = null;
    if ($this->patternMatch === true) {
        $count = 0;
        $pattern = preg_replace($this->forbiddenFunctionNames, $this->forbiddenFunctionNames, $function, 1, $count);
        if ($count === 0) {
            return;
        }
        // Remove the pattern delimiters and modifier.
        $pattern = substr($pattern, 1, -2);
    }
    else {
        if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
            return;
        }
    }
    
    //end if
    $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
}
RSS feed
Powered by Drupal