Source for file FunctionClosingBraceSniff.php
Documentation is available at FunctionClosingBraceSniff.php
* Checks that the closing brace of a function goes directly after the body.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class FunctionClosingBraceSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
* Processes this sniff, when one of its tokens is encountered.
* @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.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
if (isset ($tokens[$stackPtr]['scope_closer']) === false ) {
// Probably an interface method.
$closeBrace = $tokens[$stackPtr]['scope_closer'];
$prevContent = $phpcsFile->findPrevious (T_WHITESPACE , ($closeBrace - 1 ), null , true );
$found = ($tokens[$closeBrace]['line'] - $tokens[$prevContent]['line'] - 1 );
// Brace isn't on a new line, so not handled by us.
$error = 'Function closing brace must go on the next line following the body; found %s blank lines before brace';
$fix = $phpcsFile->addFixableError ($error, $closeBrace, 'SpacingBeforeClose', $data);
$phpcsFile->fixer ->beginChangeset ();
for ($i = ($prevContent + 1 ); $i < $closeBrace; $i++ ) {
if ($tokens[$i]['line'] === $tokens[$prevContent]['line']) {
// Don't remove any identation before the brace.
if ($tokens[$i]['line'] === $tokens[$closeBrace]['line']) {
$phpcsFile->fixer ->replaceToken ($i, '');
$phpcsFile->fixer ->endChangeset ();
Documentation generated on Mon, 11 Mar 2019 15:27:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|