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

Breadcrumb

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

function FunctionDeclarationSniff::processArgumentList

Processes multi-line argument list declarations.

Parameters

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

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

int $indent The number of spaces code should be indented.:

string $type The type of the token the brackets: belong to.

Return value

void

1 call to FunctionDeclarationSniff::processArgumentList()
FunctionDeclarationSniff::processMultiLineDeclaration in vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Processes multi-line declarations.

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php, line 386

Class

FunctionDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions

Code

public function processArgumentList($phpcsFile, $stackPtr, $indent, $type = 'function') {
    $tokens = $phpcsFile->getTokens();
    // We need to work out how far indented the function
    // declaration itself is, so we can work out how far to
    // indent parameters.
    $functionIndent = 0;
    for ($i = $stackPtr - 1; $i >= 0; $i--) {
        if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
            break;
        }
    }
    // Move $i back to the line the function is or to 0.
    $i++;
    if ($tokens[$i]['code'] === T_WHITESPACE) {
        $functionIndent = $tokens[$i]['length'];
    }
    // The closing parenthesis must be on a new line, even
    // when checking abstract function definitions.
    $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
    $prev = $phpcsFile->findPrevious(T_WHITESPACE, $closeBracket - 1, null, true);
    if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line'] && $tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
        $error = 'The closing parenthesis of a multi-line ' . $type . ' declaration must be on a new line';
        $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addNewlineBefore($closeBracket);
        }
    }
    // If this is a closure and is using a USE statement, the closing
    // parenthesis we need to look at from now on is the closing parenthesis
    // of the USE statement.
    if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
        $use = $phpcsFile->findNext(T_USE, $closeBracket + 1, $tokens[$stackPtr]['scope_opener']);
        if ($use !== false) {
            $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $use + 1);
            $closeBracket = $tokens[$open]['parenthesis_closer'];
            $prev = $phpcsFile->findPrevious(T_WHITESPACE, $closeBracket - 1, null, true);
            if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line'] && $tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
                $error = 'The closing parenthesis of a multi-line use declaration must be on a new line';
                $fix = $phpcsFile->addFixableError($error, $closeBracket, 'UseCloseBracketLine');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->addNewlineBefore($closeBracket);
                }
            }
        }
        
        //end if
    }
    
    //end if
    // Each line between the parenthesis should be indented 4 spaces.
    $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
    $lastLine = $tokens[$openBracket]['line'];
    for ($i = $openBracket + 1; $i < $closeBracket; $i++) {
        if ($tokens[$i]['line'] !== $lastLine) {
            if ($i === $tokens[$stackPtr]['parenthesis_closer'] || $tokens[$i]['code'] === T_WHITESPACE && ($i + 1 === $closeBracket || $i + 1 === $tokens[$stackPtr]['parenthesis_closer'])) {
                // Closing braces need to be indented to the same level
                // as the function.
                $expectedIndent = $functionIndent;
            }
            else {
                $expectedIndent = $functionIndent + $indent;
            }
            // We changed lines, so this should be a whitespace indent token.
            $foundIndent = 0;
            if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['line'] !== $tokens[$i + 1]['line']) {
                $error = 'Blank lines are not allowed in a multi-line ' . $type . ' declaration';
                $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($i, '');
                }
                // This is an empty line, so don't check the indent.
                continue;
            }
            else {
                if ($tokens[$i]['code'] === T_WHITESPACE) {
                    $foundIndent = $tokens[$i]['length'];
                }
                else {
                    if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {
                        $foundIndent = $tokens[$i]['length'];
                        ++$expectedIndent;
                    }
                }
            }
            if ($expectedIndent !== $foundIndent) {
                $error = 'Multi-line ' . $type . ' declaration not indented correctly; expected %s spaces but found %s';
                $data = [
                    $expectedIndent,
                    $foundIndent,
                ];
                $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
                if ($fix === true) {
                    $spaces = str_repeat(' ', $expectedIndent);
                    if ($foundIndent === 0) {
                        $phpcsFile->fixer
                            ->addContentBefore($i, $spaces);
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($i, $spaces);
                    }
                }
            }
            $lastLine = $tokens[$i]['line'];
        }
        
        //end if
        if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS && isset($tokens[$i]['parenthesis_closer']) === true) {
            $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, $i - 1, null, true);
            if ($tokens[$prevNonEmpty]['code'] !== T_USE) {
                // Since PHP 8.1, a default value can contain a class instantiation.
                // Skip over these "function calls" as they have their own indentation rules.
                $i = $tokens[$i]['parenthesis_closer'];
                $lastLine = $tokens[$i]['line'];
                continue;
            }
        }
        if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
            // Skip arrays as they have their own indentation rules.
            if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
                $i = $tokens[$i]['bracket_closer'];
            }
            else {
                $i = $tokens[$i]['parenthesis_closer'];
            }
            $lastLine = $tokens[$i]['line'];
            continue;
        }
        if ($tokens[$i]['code'] === T_ATTRIBUTE) {
            // Skip attributes as they have their own indentation rules.
            $i = $tokens[$i]['attribute_closer'];
            $lastLine = $tokens[$i]['line'];
            continue;
        }
    }
    
    //end for
}

API Navigation

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