-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug #908 : PSR2 standard is not checking that closing brace is …
…on line following the body
- Loading branch information
Showing
6 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
CodeSniffer/Standards/PSR2/Sniffs/Methods/FunctionClosingBraceSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
70 changes: 70 additions & 0 deletions
70
CodeSniffer/Standards/PSR2/Tests/Methods/FunctionClosingBraceUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
||
}; |
61 changes: 61 additions & 0 deletions
61
CodeSniffer/Standards/PSR2/Tests/Methods/FunctionClosingBraceUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}; |
72 changes: 72 additions & 0 deletions
72
CodeSniffer/Standards/PSR2/Tests/Methods/FunctionClosingBraceUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters