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

Breadcrumb

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

function StrictCallSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $stringPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/StrictCallSniff.php, line 47

Class

StrictCallSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

public function process(File $phpcsFile, $stringPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $parenthesisOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $stringPointer + 1);
    if ($tokens[$parenthesisOpenerPointer]['code'] !== T_OPEN_PARENTHESIS) {
        return;
    }
    $parenthesisCloserPointer = $tokens[$parenthesisOpenerPointer]['parenthesis_closer'];
    $functionName = ltrim(strtolower($tokens[$stringPointer]['content']), '\\');
    if (!array_key_exists($functionName, self::FUNCTIONS)) {
        return;
    }
    $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $stringPointer - 1);
    if (in_array($tokens[$previousPointer]['code'], [
        T_OBJECT_OPERATOR,
        T_DOUBLE_COLON,
        T_FUNCTION,
    ], true)) {
        return;
    }
    $commaPointers = [];
    for ($i = $parenthesisOpenerPointer + 1; $i < $parenthesisCloserPointer; $i++) {
        if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
            $i = $tokens[$i]['parenthesis_closer'];
            continue;
        }
        if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
            $i = $tokens[$i]['bracket_closer'];
            continue;
        }
        if ($tokens[$i]['code'] === T_COMMA) {
            $commaPointers[] = $i;
        }
    }
    $commaPointersCount = count($commaPointers);
    $parametersCount = $commaPointersCount + 1;
    $lastCommaPointer = $commaPointersCount > 0 ? $commaPointers[$commaPointersCount - 1] : null;
    $hasTrailingComma = false;
    if ($lastCommaPointer !== null && TokenHelper::findNextEffective($phpcsFile, $lastCommaPointer + 1, $parenthesisCloserPointer) === null) {
        $hasTrailingComma = true;
        $parametersCount--;
    }
    if ($parametersCount === self::FUNCTIONS[$functionName]) {
        $strictParameterValue = TokenHelper::getContent($phpcsFile, $commaPointers[self::FUNCTIONS[$functionName] - 2] + 1, ($hasTrailingComma ? $lastCommaPointer : $parenthesisCloserPointer) - 1);
        if (strtolower(trim($strictParameterValue)) !== 'false') {
            return;
        }
        $phpcsFile->addError(sprintf('Strict parameter should be set to true in %s() call.', $functionName), $stringPointer, self::CODE_NON_STRICT_COMPARISON);
    }
    elseif ($parametersCount === self::FUNCTIONS[$functionName] - 1) {
        $phpcsFile->addError(sprintf('Strict parameter missing in %s() call.', $functionName), $stringPointer, self::CODE_STRICT_PARAMETER_MISSING);
    }
}
RSS feed
Powered by Drupal