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

Breadcrumb

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

function FunctionDeclarationSniff::process

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

Processes this test, when one of its tokens is encountered.

Parameters

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

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

Return value

void

Overrides Sniff::process

File

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

Class

FunctionDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr]['parenthesis_opener']) === false || isset($tokens[$stackPtr]['parenthesis_closer']) === false || $tokens[$stackPtr]['parenthesis_opener'] === null || $tokens[$stackPtr]['parenthesis_closer'] === null) {
        return;
    }
    $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
    $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
    if (strtolower($tokens[$stackPtr]['content']) === 'function') {
        // Must be one space after the FUNCTION keyword.
        if ($tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar) {
            $spaces = 'newline';
        }
        else {
            if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
                $spaces = $tokens[$stackPtr + 1]['length'];
            }
            else {
                $spaces = 0;
            }
        }
        if ($spaces !== 1) {
            $error = 'Expected 1 space after FUNCTION keyword; %s found';
            $data = [
                $spaces,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFunction', $data);
            if ($fix === true) {
                if ($spaces === 0) {
                    $phpcsFile->fixer
                        ->addContent($stackPtr, ' ');
                }
                else {
                    $phpcsFile->fixer
                        ->replaceToken($stackPtr + 1, ' ');
                }
            }
        }
    }
    
    //end if
    // Must be no space before the opening parenthesis. For closures, this is
    // enforced by the previous check because there is no content between the keywords
    // and the opening parenthesis.
    // Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
    // by checking for the scope_opener.
    $methodProps = $phpcsFile->getMethodProperties($stackPtr);
    if ($tokens[$stackPtr]['code'] === T_FUNCTION && (isset($tokens[$stackPtr]['scope_opener']) === true || $methodProps['has_body'] === false)) {
        if ($tokens[$openBracket - 1]['content'] === $phpcsFile->eolChar) {
            $spaces = 'newline';
        }
        else {
            if ($tokens[$openBracket - 1]['code'] === T_WHITESPACE) {
                $spaces = $tokens[$openBracket - 1]['length'];
            }
            else {
                $spaces = 0;
            }
        }
        if ($spaces !== 0) {
            $error = 'Expected 0 spaces before opening parenthesis; %s found';
            $data = [
                $spaces,
            ];
            $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($openBracket - 1, '');
            }
        }
        // Must be no space before semicolon in abstract/interface methods.
        if ($methodProps['has_body'] === false) {
            $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket);
            if ($end !== false) {
                if ($tokens[$end - 1]['content'] === $phpcsFile->eolChar) {
                    $spaces = 'newline';
                }
                else {
                    if ($tokens[$end - 1]['code'] === T_WHITESPACE) {
                        $spaces = $tokens[$end - 1]['length'];
                    }
                    else {
                        $spaces = 0;
                    }
                }
                if ($spaces !== 0) {
                    $error = 'Expected 0 spaces before semicolon; %s found';
                    $data = [
                        $spaces,
                    ];
                    $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data);
                    if ($fix === true) {
                        $phpcsFile->fixer
                            ->replaceToken($end - 1, '');
                    }
                }
            }
        }
        
        //end if
    }
    
    //end if
    // Must be one space before and after USE keyword for closures.
    if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
        $use = $phpcsFile->findNext(T_USE, $closeBracket + 1, $tokens[$stackPtr]['scope_opener']);
        if ($use !== false) {
            if ($tokens[$use + 1]['code'] !== T_WHITESPACE) {
                $length = 0;
            }
            else {
                if ($tokens[$use + 1]['content'] === "\t") {
                    $length = '\\t';
                }
                else {
                    $length = $tokens[$use + 1]['length'];
                }
            }
            if ($length !== 1) {
                $error = 'Expected 1 space after USE keyword; found %s';
                $data = [
                    $length,
                ];
                $fix = $phpcsFile->addFixableError($error, $use, 'SpaceAfterUse', $data);
                if ($fix === true) {
                    if ($length === 0) {
                        $phpcsFile->fixer
                            ->addContent($use, ' ');
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($use + 1, ' ');
                    }
                }
            }
            if ($tokens[$use - 1]['code'] !== T_WHITESPACE) {
                $length = 0;
            }
            else {
                if ($tokens[$use - 1]['content'] === "\t") {
                    $length = '\\t';
                }
                else {
                    $length = $tokens[$use - 1]['length'];
                }
            }
            if ($length !== 1) {
                $error = 'Expected 1 space before USE keyword; found %s';
                $data = [
                    $length,
                ];
                $fix = $phpcsFile->addFixableError($error, $use, 'SpaceBeforeUse', $data);
                if ($fix === true) {
                    if ($length === 0) {
                        $phpcsFile->fixer
                            ->addContentBefore($use, ' ');
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($use - 1, ' ');
                    }
                }
            }
        }
        
        //end if
    }
    
    //end if
    if ($this->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
        $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
    }
    else {
        $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
    }
}

API Navigation

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