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

Breadcrumb

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

function MultiLineFunctionDeclarationSniff::processSingleLineDeclaration

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php \Drupal\Sniffs\Functions\MultiLineFunctionDeclarationSniff::processSingleLineDeclaration()

Processes single-line declarations.

Just uses the Generic BSD-Allman brace sniff.

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

Overrides FunctionDeclarationSniff::processSingleLineDeclaration

1 method overrides MultiLineFunctionDeclarationSniff::processSingleLineDeclaration()
MultiLineFunctionDeclarationSniff::processSingleLineDeclaration in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Processes single-line declarations.

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php, line 109

Class

MultiLineFunctionDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions

Code

public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens) {
    // We do everything the parent sniff does, and a bit more because we
    // define multi-line declarations a bit differently.
    parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
    $openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
    $closingBracket = $tokens[$stackPtr]['parenthesis_closer'];
    $prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, $closingBracket - 1, $openingBracket, true);
    if ($tokens[$prevNonWhiteSpace]['line'] !== $tokens[$closingBracket]['line']) {
        $error = 'There must not be a newline before the closing parenthesis of a single-line function declaration';
        if (isset(Tokens::$emptyTokens[$tokens[$prevNonWhiteSpace]['code']]) === true) {
            $phpcsFile->addError($error, $closingBracket, 'CloseBracketNewLine');
        }
        else {
            $fix = $phpcsFile->addFixableError($error, $closingBracket, 'CloseBracketNewLine');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->beginChangeset();
                for ($i = $closingBracket - 1; $i > $openingBracket; $i--) {
                    if ($tokens[$i]['code'] !== T_WHITESPACE) {
                        break;
                    }
                    $phpcsFile->fixer
                        ->replaceToken($i, '');
                }
                $phpcsFile->fixer
                    ->endChangeset();
            }
        }
    }
    
    //end if
}
RSS feed
Powered by Drupal