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

Breadcrumb

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

function AjaxNullComparisonSniff::process

Processes this sniff, 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/MySource/Sniffs/PHP/AjaxNullComparisonSniff.php, line 45

Class

AjaxNullComparisonSniff

Namespace

PHP_CodeSniffer\Standards\MySource\Sniffs\PHP

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Make sure it is an API function. We know this by the doc comment.
    $commentEnd = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr);
    $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $commentEnd - 1);
    // If function doesn't contain any doc comments - skip it.
    if ($commentEnd === false || $commentStart === false) {
        return;
    }
    $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart);
    if (strpos($comment, '* @api') === false) {
        return;
    }
    // Find all the vars passed in as we are only interested in comparisons
    // to NULL for these specific variables.
    $foundVars = [];
    $open = $tokens[$stackPtr]['parenthesis_opener'];
    $close = $tokens[$stackPtr]['parenthesis_closer'];
    for ($i = $open + 1; $i < $close; $i++) {
        if ($tokens[$i]['code'] === T_VARIABLE) {
            $foundVars[$tokens[$i]['content']] = true;
        }
    }
    if (empty($foundVars) === true) {
        return;
    }
    $start = $tokens[$stackPtr]['scope_opener'];
    $end = $tokens[$stackPtr]['scope_closer'];
    for ($i = $start + 1; $i < $end; $i++) {
        if ($tokens[$i]['code'] !== T_VARIABLE || isset($foundVars[$tokens[$i]['content']]) === false) {
            continue;
        }
        $operator = $phpcsFile->findNext(T_WHITESPACE, $i + 1, null, true);
        if ($tokens[$operator]['code'] !== T_IS_IDENTICAL && $tokens[$operator]['code'] !== T_IS_NOT_IDENTICAL) {
            continue;
        }
        $nullValue = $phpcsFile->findNext(T_WHITESPACE, $operator + 1, null, true);
        if ($tokens[$nullValue]['code'] !== T_NULL) {
            continue;
        }
        $error = 'Values submitted via Ajax requests should not be compared directly to NULL; use empty() instead';
        $phpcsFile->addWarning($error, $nullValue, 'Found');
    }
    
    //end for
}

API Navigation

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