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

Breadcrumb

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

function FunctionDeclarationArgumentSpacingSniff::processBracket

Processes the contents of a single set of brackets.

Parameters

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

int $openBracket The position of the open bracket: in the stack.

Return value

void

1 call to FunctionDeclarationArgumentSpacingSniff::processBracket()
FunctionDeclarationArgumentSpacingSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php, line 104

Class

FunctionDeclarationArgumentSpacingSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions

Code

public function processBracket($phpcsFile, $openBracket) {
    $tokens = $phpcsFile->getTokens();
    $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
    $multiLine = $tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line'];
    if (isset($tokens[$openBracket]['parenthesis_owner']) === true) {
        $stackPtr = $tokens[$openBracket]['parenthesis_owner'];
    }
    else {
        $stackPtr = $phpcsFile->findPrevious(T_USE, $openBracket - 1);
    }
    $params = $phpcsFile->getMethodParameters($stackPtr);
    if (empty($params) === true) {
        // Check spacing around parenthesis.
        $next = $phpcsFile->findNext(T_WHITESPACE, $openBracket + 1, $closeBracket, true);
        if ($next === false) {
            if ($closeBracket - $openBracket !== 1) {
                if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
                    $found = 'newline';
                }
                else {
                    $found = $tokens[$openBracket + 1]['length'];
                }
                $error = 'Expected 0 spaces between parenthesis of function declaration; %s found';
                $data = [
                    $found,
                ];
                $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpacingBetween', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($openBracket + 1, '');
                }
            }
            // No params, so we don't check normal spacing rules.
            return;
        }
    }
    
    //end if
    foreach ($params as $paramNumber => $param) {
        if ($param['pass_by_reference'] === true) {
            $refToken = $param['reference_token'];
            $gap = 0;
            if ($tokens[$refToken + 1]['code'] === T_WHITESPACE) {
                $gap = $tokens[$refToken + 1]['length'];
            }
            if ($gap !== 0) {
                $error = 'Expected 0 spaces after reference operator for argument "%s"; %s found';
                $data = [
                    $param['name'],
                    $gap,
                ];
                $fix = $phpcsFile->addFixableError($error, $refToken, 'SpacingAfterReference', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($refToken + 1, '');
                }
            }
        }
        
        //end if
        if ($param['variable_length'] === true) {
            $variadicToken = $param['variadic_token'];
            $gap = 0;
            if ($tokens[$variadicToken + 1]['code'] === T_WHITESPACE) {
                $gap = $tokens[$variadicToken + 1]['length'];
            }
            if ($gap !== 0) {
                $error = 'Expected 0 spaces after variadic operator for argument "%s"; %s found';
                $data = [
                    $param['name'],
                    $gap,
                ];
                $fix = $phpcsFile->addFixableError($error, $variadicToken, 'SpacingAfterVariadic', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($variadicToken + 1, '');
                }
            }
        }
        
        //end if
        if (isset($param['default_equal_token']) === true) {
            $equalToken = $param['default_equal_token'];
            $spacesBefore = 0;
            if ($equalToken - $param['token'] > 1) {
                $spacesBefore = $tokens[$param['token'] + 1]['length'];
            }
            if ($spacesBefore !== $this->equalsSpacing) {
                $error = 'Incorrect spacing between argument "%s" and equals sign; expected ' . $this->equalsSpacing . ' but found %s';
                $data = [
                    $param['name'],
                    $spacesBefore,
                ];
                $fix = $phpcsFile->addFixableError($error, $equalToken, 'SpaceBeforeEquals', $data);
                if ($fix === true) {
                    $padding = str_repeat(' ', $this->equalsSpacing);
                    if ($spacesBefore === 0) {
                        $phpcsFile->fixer
                            ->addContentBefore($equalToken, $padding);
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($equalToken - 1, $padding);
                    }
                }
            }
            
            //end if
            $spacesAfter = 0;
            if ($tokens[$equalToken + 1]['code'] === T_WHITESPACE) {
                $spacesAfter = $tokens[$equalToken + 1]['length'];
            }
            if ($spacesAfter !== $this->equalsSpacing) {
                $error = 'Incorrect spacing between default value and equals sign for argument "%s"; expected ' . $this->equalsSpacing . ' but found %s';
                $data = [
                    $param['name'],
                    $spacesAfter,
                ];
                $fix = $phpcsFile->addFixableError($error, $equalToken, 'SpaceAfterEquals', $data);
                if ($fix === true) {
                    $padding = str_repeat(' ', $this->equalsSpacing);
                    if ($spacesAfter === 0) {
                        $phpcsFile->fixer
                            ->addContent($equalToken, $padding);
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($equalToken + 1, $padding);
                    }
                }
            }
            
            //end if
        }
        
        //end if
        if ($param['type_hint_token'] !== false) {
            $typeHintToken = $param['type_hint_end_token'];
            $gap = 0;
            if ($tokens[$typeHintToken + 1]['code'] === T_WHITESPACE) {
                $gap = $tokens[$typeHintToken + 1]['length'];
            }
            if ($gap !== 1) {
                $error = 'Expected 1 space between type hint and argument "%s"; %s found';
                $data = [
                    $param['name'],
                    $gap,
                ];
                $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);
                if ($fix === true) {
                    if ($gap === 0) {
                        $phpcsFile->fixer
                            ->addContent($typeHintToken, ' ');
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($typeHintToken + 1, ' ');
                    }
                }
            }
        }
        
        //end if
        $commaToken = false;
        if ($paramNumber > 0 && $params[$paramNumber - 1]['comma_token'] !== false) {
            $commaToken = $params[$paramNumber - 1]['comma_token'];
        }
        if ($commaToken !== false) {
            if ($tokens[$commaToken - 1]['code'] === T_WHITESPACE) {
                $error = 'Expected 0 spaces between argument "%s" and comma; %s found';
                $data = [
                    $params[$paramNumber - 1]['name'],
                    $tokens[$commaToken - 1]['length'],
                ];
                $fix = $phpcsFile->addFixableError($error, $commaToken, 'SpaceBeforeComma', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($commaToken - 1, '');
                }
            }
            // Don't check spacing after the comma if it is the last content on the line.
            $checkComma = true;
            if ($multiLine === true) {
                $next = $phpcsFile->findNext(Tokens::$emptyTokens, $commaToken + 1, $closeBracket, true);
                if ($tokens[$next]['line'] !== $tokens[$commaToken]['line']) {
                    $checkComma = false;
                }
            }
            if ($checkComma === true) {
                if ($param['type_hint_token'] === false) {
                    $spacesAfter = 0;
                    if ($tokens[$commaToken + 1]['code'] === T_WHITESPACE) {
                        $spacesAfter = $tokens[$commaToken + 1]['length'];
                    }
                    if ($spacesAfter === 0) {
                        $error = 'Expected 1 space between comma and argument "%s"; 0 found';
                        $data = [
                            $param['name'],
                        ];
                        $fix = $phpcsFile->addFixableError($error, $commaToken, 'NoSpaceBeforeArg', $data);
                        if ($fix === true) {
                            $phpcsFile->fixer
                                ->addContent($commaToken, ' ');
                        }
                    }
                    else {
                        if ($spacesAfter !== 1) {
                            $error = 'Expected 1 space between comma and argument "%s"; %s found';
                            $data = [
                                $param['name'],
                                $spacesAfter,
                            ];
                            $fix = $phpcsFile->addFixableError($error, $commaToken, 'SpacingBeforeArg', $data);
                            if ($fix === true) {
                                $phpcsFile->fixer
                                    ->replaceToken($commaToken + 1, ' ');
                            }
                        }
                    }
                    
                    //end if
                }
                else {
                    $hint = $phpcsFile->getTokensAsString($param['type_hint_token'], $param['type_hint_end_token'] - $param['type_hint_token'] + 1);
                    if ($param['nullable_type'] === true) {
                        $hint = '?' . $hint;
                    }
                    if ($tokens[$commaToken + 1]['code'] !== T_WHITESPACE) {
                        $error = 'Expected 1 space between comma and type hint "%s"; 0 found';
                        $data = [
                            $hint,
                        ];
                        $fix = $phpcsFile->addFixableError($error, $commaToken, 'NoSpaceBeforeHint', $data);
                        if ($fix === true) {
                            $phpcsFile->fixer
                                ->addContent($commaToken, ' ');
                        }
                    }
                    else {
                        $gap = $tokens[$commaToken + 1]['length'];
                        if ($gap !== 1) {
                            $error = 'Expected 1 space between comma and type hint "%s"; %s found';
                            $data = [
                                $hint,
                                $gap,
                            ];
                            $fix = $phpcsFile->addFixableError($error, $commaToken, 'SpacingBeforeHint', $data);
                            if ($fix === true) {
                                $phpcsFile->fixer
                                    ->replaceToken($commaToken + 1, ' ');
                            }
                        }
                    }
                    
                    //end if
                }
                
                //end if
            }
            
            //end if
        }
        
        //end if
    }
    
    //end foreach
    // Only check spacing around parenthesis for single line definitions.
    if ($multiLine === true) {
        return;
    }
    $gap = 0;
    if ($tokens[$closeBracket - 1]['code'] === T_WHITESPACE) {
        $gap = $tokens[$closeBracket - 1]['length'];
    }
    if ($gap !== $this->requiredSpacesBeforeClose) {
        $error = 'Expected %s spaces before closing parenthesis; %s found';
        $data = [
            $this->requiredSpacesBeforeClose,
            $gap,
        ];
        $fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpacingBeforeClose', $data);
        if ($fix === true) {
            $padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
            if ($gap === 0) {
                $phpcsFile->fixer
                    ->addContentBefore($closeBracket, $padding);
            }
            else {
                $phpcsFile->fixer
                    ->replaceToken($closeBracket - 1, $padding);
            }
        }
    }
    $gap = 0;
    if ($tokens[$openBracket + 1]['code'] === T_WHITESPACE) {
        $gap = $tokens[$openBracket + 1]['length'];
    }
    if ($gap !== $this->requiredSpacesAfterOpen) {
        $error = 'Expected %s spaces after opening parenthesis; %s found';
        $data = [
            $this->requiredSpacesAfterOpen,
            $gap,
        ];
        $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpacingAfterOpen', $data);
        if ($fix === true) {
            $padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
            if ($gap === 0) {
                $phpcsFile->fixer
                    ->addContent($openBracket, $padding);
            }
            else {
                $phpcsFile->fixer
                    ->replaceToken($openBracket + 1, $padding);
            }
        }
    }
}

API Navigation

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