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

Breadcrumb

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

function ValidDefaultValueSniff::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/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php, line 44

Class

ValidDefaultValueSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions

Code

public function process(File $phpcsFile, $stackPtr) {
    // Flag for when we have found a default in our arg list.
    // If there is a value without a default after this, it is an error.
    $defaultFound = false;
    $params = $phpcsFile->getMethodParameters($stackPtr);
    foreach ($params as $param) {
        if ($param['variable_length'] === true) {
            continue;
        }
        if (array_key_exists('default', $param) === true) {
            $defaultFound = true;
            // Check if the arg is type hinted and using NULL for the default.
            // This does not make the argument optional - it just allows NULL
            // to be passed in.
            if ($param['type_hint'] !== '' && strtolower($param['default']) === 'null') {
                $defaultFound = false;
            }
            continue;
        }
        if ($defaultFound === true) {
            $error = 'Arguments with default values must be at the end of the argument list';
            $phpcsFile->addError($error, $param['token'], 'NotAtEnd');
            return;
        }
    }
    
    //end foreach
}
RSS feed
Powered by Drupal