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

Breadcrumb

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

class MultiLineFunctionDeclarationSniff

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff

Multi-line function declarations need to have a trailing comma on the last parameter. Modified from Squiz, whenever there is a function declaration closing parenthesis on a new line we treat it as multi-line.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionDeclarationSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff extends \PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionDeclarationSniff
      • class \Drupal\Sniffs\Functions\MultiLineFunctionDeclarationSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff

Expanded class hierarchy of MultiLineFunctionDeclarationSniff

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php, line 26

Namespace

Drupal\Sniffs\Functions
View source
class MultiLineFunctionDeclarationSniff extends SquizFunctionDeclarationSniff {
    
    /**
     * The number of spaces code should be indented.
     *
     * @var integer
     */
    public $indent = 2;
    
    /**
     * Processes single-line declarations.
     *
     * Just uses the Generic Kernighan Ritchie sniff.
     *
     * @param \PHP_CodeSniffer\Files\File      $phpcsFile The file being scanned.
     * @param int                              $stackPtr  The position of the current token
     *                                                    in the stack passed in $tokens.
     * @param array<int, array<string, mixed>> $tokens    The stack of tokens that make up
     *                                                    the file.
     *
     * @return void
     */
    public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens) {
        $sniff = new OpeningFunctionBraceKernighanRitchieSniff();
        $sniff->checkClosures = true;
        $sniff->process($phpcsFile, $stackPtr);
    }
    
    //end processSingleLineDeclaration()
    
    /**
     * Determine if this is a multi-line function declaration.
     *
     * @param \PHP_CodeSniffer\Files\File      $phpcsFile   The file being scanned.
     * @param int                              $stackPtr    The position of the current token
     *                                                      in the stack passed in $tokens.
     * @param int                              $openBracket The position of the opening bracket
     *                                                      in the stack passed in $tokens.
     * @param array<int, array<string, mixed>> $tokens      The stack of tokens that make up
     *                                                      the file.
     *
     * @return bool
     */
    public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) {
        $function = $tokens[$stackPtr];
        if ($tokens[$function['parenthesis_opener']]['line'] === $tokens[$function['parenthesis_closer']]['line']) {
            return false;
        }
        return true;
    }
    
    //end isMultiLineDeclaration()
    
    /**
     * Processes multi-line declarations.
     *
     * @param \PHP_CodeSniffer\Files\File      $phpcsFile The file being scanned.
     * @param int                              $stackPtr  The position of the current token
     *                                                    in the stack passed in $tokens.
     * @param array<int, array<string, mixed>> $tokens    The stack of tokens that make up
     *                                                    the file.
     *
     * @return void
     */
    public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens) {
        // We do everything the grandparent sniff does, and a bit more.
        PearFunctionDeclarationSniff::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
        $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
        $this->processBracket($phpcsFile, $openBracket, $tokens, 'function');
        // Trailing commas on the last function parameter are only possible in
        // PHP 8.0+.
        if (version_compare(PHP_VERSION, '8.0.0') < 0) {
            return;
        }
        $function = $tokens[$stackPtr];
        $lastTrailingComma = $phpcsFile->findPrevious(Tokens::$emptyTokens, $function['parenthesis_closer'] - 1, $function['parenthesis_opener'], true);
        if ($tokens[$lastTrailingComma]['code'] !== T_COMMA) {
            $error = 'Multi-line function declarations must have a trailing comma after the last parameter';
            $fix = $phpcsFile->addFixableError($error, $lastTrailingComma, 'MissingTrailingComma');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContent($lastTrailingComma, ',');
            }
        }
    }
    
    //end processMultiLineDeclaration()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FunctionDeclarationSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
FunctionDeclarationSniff::processArgumentList public function Processes multi-line argument list declarations.
FunctionDeclarationSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
MultiLineFunctionDeclarationSniff::$indent public property The number of spaces code should be indented. Overrides FunctionDeclarationSniff::$indent
MultiLineFunctionDeclarationSniff::$supportedTokenizers public property A list of tokenizers this sniff supports. Overrides FunctionDeclarationSniff::$supportedTokenizers
MultiLineFunctionDeclarationSniff::isMultiLineDeclaration public function Determine if this is a multi-line function declaration. Overrides MultiLineFunctionDeclarationSniff::isMultiLineDeclaration
MultiLineFunctionDeclarationSniff::processBracket public function Processes the contents of a single set of brackets.
MultiLineFunctionDeclarationSniff::processMultiLineDeclaration public function Processes multi-line declarations. Overrides MultiLineFunctionDeclarationSniff::processMultiLineDeclaration
MultiLineFunctionDeclarationSniff::processSingleLineDeclaration public function Processes single-line declarations. Overrides MultiLineFunctionDeclarationSniff::processSingleLineDeclaration

API Navigation

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