Skip to content

Commit

Permalink
Fixed bug #908 : PSR2 standard is not checking that closing brace is …
Browse files Browse the repository at this point in the history
…on line following the body
  • Loading branch information
gsherwood committed Jun 27, 2016
1 parent f1b7158 commit 3c35670
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* PSR2_Sniffs_Methods_FunctionClosingBraceSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* PSR2_Sniffs_Methods_FunctionClosingBraceSniff.
*
* Checks that the closing brace of a function goes directly after the body.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class PSR2_Sniffs_Methods_FunctionClosingBraceSniff implements PHP_CodeSniffer_Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_FUNCTION,
T_CLOSURE,
);

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// Probably an interface method.
return;
}

$closeBrace = $tokens[$stackPtr]['scope_closer'];
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
$found = ($tokens[$closeBrace]['line'] - $tokens[$prevContent]['line'] - 1);

if ($found < 0) {
// Brace isn't on a new line, so not handled by us.
return;
}

if ($found === 0) {
// All is good.
return;
}

$error = 'Function closing brace must go on the next line following the body; found %s blank lines before brace';
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBeforeClose', $data);

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
if ($tokens[$i]['line'] === $tokens[$prevContent]['line']) {
continue;
}

// Don't remove any identation before the brace.
if ($tokens[$i]['line'] === $tokens[$closeBrace]['line']) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
}

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

function test()
{
// Body here.
}

function test()
{
echo 'foo';}

function test()
{
// Body here.

}

function test()
{
// Body here.


}

class MyClass
{
function test()
{
// Body here.
}

function test()
{
echo 'foo';}

function test()
{
// Body here.

}

function test()
{
// Body here.


}
}

$foo = function test()
{
// Body here.
};

$foo = function test()
{
echo 'foo';};

$foo = function test()
{
// Body here.

};

$foo = function test()
{
// Body here.


};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

function test()
{
// Body here.
}

function test()
{
echo 'foo';}

function test()
{
// Body here.
}

function test()
{
// Body here.
}

class MyClass
{
function test()
{
// Body here.
}

function test()
{
echo 'foo';}

function test()
{
// Body here.
}

function test()
{
// Body here.
}
}

$foo = function test()
{
// Body here.
};

$foo = function test()
{
echo 'foo';};

$foo = function test()
{
// Body here.
};

$foo = function test()
{
// Body here.
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Unit test class for the FunctionClosingBrace sniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* Unit test class for the FunctionClosingBrace sniff.
*
* A sniff unit test checks a .inc file for expected violations of a single
* coding standard. Expected errors and warnings are stored in this class.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class PSR2_Tests_Methods_FunctionClosingBraceUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return array(
16 => 1,
23 => 1,
40 => 1,
47 => 1,
63 => 1,
70 => 1,
);

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array<int, int>
*/
public function getWarningList()
{
return array();

}//end getWarningList()


}//end class

?>
1 change: 1 addition & 0 deletions CodeSniffer/Standards/PSR2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<!-- checked in Methods/MethodDeclarationSniff -->

<!-- Method names MUST NOT be declared with a space after the method name. The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. -->
<!-- checked in Methods/FunctionClosingBraceSniff -->
<rule ref="Squiz.Functions.FunctionDeclaration"/>
<rule ref="Squiz.Functions.LowercaseFunctionKeywords"/>

Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Michael Butler for the patch
- Settings extensions in a ruleset no longer causes PHP notices during unit testing
-- Thanks to Klaus Purer for the patch
- Fixed bug #908 : PSR2 standard is not checking that closing brace is on line following the body
- Fixed bug #945 : Incorrect indent behavior using deep-nested function and arrays
- Fixed bug #961 : Two anonymous functions passed as function/method arguments cause indentation false positive
- Fixed bug #1007 : Squiz Unreachable code detection is not working properly with a closure inside a case
Expand Down

0 comments on commit 3c35670

Please sign in to comment.