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

Breadcrumb

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

function FunctionDeclarationSniff::processMultiLineDeclaration

Processes multi-line 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.

array $tokens The stack of tokens that make up: the file.

Return value

void

4 calls to FunctionDeclarationSniff::processMultiLineDeclaration()
FunctionDeclarationSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Processes this test, when one of its tokens is encountered.
MultiLineFunctionDeclarationSniff::processMultiLineDeclaration in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Processes multi-line declarations.
MultiLineFunctionDeclarationSniff::processMultiLineDeclaration in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Processes multi-line declarations.
MultiLineFunctionDeclarationSniff::processMultiLineDeclaration in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Processes multi-line declarations.
1 method overrides FunctionDeclarationSniff::processMultiLineDeclaration()
MultiLineFunctionDeclarationSniff::processMultiLineDeclaration in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Processes multi-line declarations.

File

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

Class

FunctionDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions

Code

public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens) {
    $this->processArgumentList($phpcsFile, $stackPtr, $this->indent);
    $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
    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'];
        }
    }
    if (isset($tokens[$stackPtr]['scope_opener']) === false) {
        return;
    }
    // The opening brace needs to be on the same line as the closing parenthesis.
    // There should only be one space between the closing parenthesis - or the end of the
    // return type - and the opening brace.
    $opener = $tokens[$stackPtr]['scope_opener'];
    if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) {
        $error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line';
        $code = 'NewlineBeforeOpenBrace';
        $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $opener - 1, $closeBracket, true);
        if ($tokens[$prev]['line'] === $tokens[$opener]['line']) {
            // End of the return type is not on the same line as the close parenthesis.
            $phpcsFile->addError($error, $opener, $code);
        }
        else {
            $fix = $phpcsFile->addFixableError($error, $opener, $code);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->beginChangeset();
                $phpcsFile->fixer
                    ->addContent($prev, ' {');
                // If the opener is on a line by itself, removing it will create
                // an empty line, so remove the entire line instead.
                $prev = $phpcsFile->findPrevious(T_WHITESPACE, $opener - 1, $closeBracket, true);
                $next = $phpcsFile->findNext(T_WHITESPACE, $opener + 1, null, true);
                if ($tokens[$prev]['line'] < $tokens[$opener]['line'] && $tokens[$next]['line'] > $tokens[$opener]['line']) {
                    // Clear the whole line.
                    for ($i = $prev + 1; $i < $next; $i++) {
                        if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
                            $phpcsFile->fixer
                                ->replaceToken($i, '');
                        }
                    }
                }
                else {
                    // Just remove the opener.
                    $phpcsFile->fixer
                        ->replaceToken($opener, '');
                    if ($tokens[$next]['line'] === $tokens[$opener]['line'] && $opener + 1 !== $next) {
                        $phpcsFile->fixer
                            ->replaceToken($opener + 1, '');
                    }
                }
                $phpcsFile->fixer
                    ->endChangeset();
            }
            
            //end if
            return;
        }
        
        //end if
    }
    
    //end if
    $prev = $tokens[$opener - 1];
    if ($prev['code'] !== T_WHITESPACE) {
        $length = 0;
    }
    else {
        $length = strlen($prev['content']);
    }
    if ($length !== 1) {
        $error = 'There must be a single space between the closing parenthesis/return type and the opening brace of a multi-line function declaration; found %s spaces';
        $fix = $phpcsFile->addFixableError($error, $opener - 1, 'SpaceBeforeOpenBrace', [
            $length,
        ]);
        if ($fix === true) {
            if ($length === 0) {
                $phpcsFile->fixer
                    ->addContentBefore($opener, ' ');
            }
            else {
                $phpcsFile->fixer
                    ->replaceToken($opener - 1, ' ');
            }
        }
    }
}

API Navigation

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