Skip to content

Commit b73f463

Browse files
committed
Added PSR12.Classes.ClosingBrace sniff to enforce that closing braces of classes/interfaces/traits/functions are not followed by a comment or statement (ref #750)
Fixers were not added to this sniff as it is likely that comments would be found more than anything else, and simply moving them to the next line is probably not the right fix. More likely, the comment should be removed, which only the developer should do.
1 parent f1b421e commit b73f463

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
6767
-- Thanks to Mponos George for the contribution
6868
- Added Generic.PHP.RequireStrictTypes sniff
6969
-- Enforce the use of a strict types declaration in PHP files
70+
- Added PSR12.Classes.ClosingBrace sniff
71+
-- Enforces that closing braces of classes/interfaces/traits/functions are not followed by a comment or statement
7072
- Added PSR12.ControlStructures.BooleanOperatorPlacement sniff
7173
-- Enforces that boolean operators between conditions are consistently at the start or end of the line
7274
- Added PSR12.ControlStructures.ControlStructureSpacing sniff
@@ -1119,6 +1121,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
11191121
<dir name="Sniffs">
11201122
<dir name="Classes">
11211123
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationSniff.php" role="php" />
1124+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceSniff.php" role="php" />
11221125
</dir>
11231126
<dir name="ControlStructures">
11241127
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementSniff.php" role="php" />
@@ -1155,6 +1158,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
11551158
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.inc" role="test" />
11561159
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.inc.fixed" role="test" />
11571160
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.php" role="test" />
1161+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.inc" role="test" />
1162+
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.php" role="test" />
11581163
</dir>
11591164
<dir name="ControlStructures">
11601165
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementUnitTest.inc" role="test" />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Verifies that closing braces are the last content on a line.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes;
11+
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Util\Tokens;
15+
16+
class ClosingBraceSniff implements Sniff
17+
{
18+
19+
20+
/**
21+
* Returns an array of tokens this test wants to listen for.
22+
*
23+
* @return array
24+
*/
25+
public function register()
26+
{
27+
return [
28+
T_CLASS,
29+
T_INTERFACE,
30+
T_TRAIT,
31+
T_FUNCTION,
32+
];
33+
34+
}//end register()
35+
36+
37+
/**
38+
* Processes this test, when one of its tokens is encountered.
39+
*
40+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
41+
* @param int $stackPtr The position of the current token in the
42+
* stack passed in $tokens.
43+
*
44+
* @return void
45+
*/
46+
public function process(File $phpcsFile, $stackPtr)
47+
{
48+
$tokens = $phpcsFile->getTokens();
49+
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
50+
return;
51+
}
52+
53+
$closer = $tokens[$stackPtr]['scope_closer'];
54+
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true);
55+
if ($next === false
56+
|| $tokens[$next]['line'] !== $tokens[$closer]['line']
57+
) {
58+
return;
59+
}
60+
61+
$error = 'Closing brace must not be followed by any comment or statement on the same line';
62+
$phpcsFile->addError($error, $closer, 'StatementAfter');
63+
64+
}//end process()
65+
66+
67+
}//end class
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
class Foo
3+
{
4+
}
5+
6+
class Foo1
7+
{
8+
public function foo()
9+
{
10+
}
11+
public function foo2()
12+
{
13+
}//end foo2()
14+
}//end class
15+
16+
interface Foo2
17+
{
18+
19+
}echo 'hi';
20+
21+
trait Foo3
22+
{
23+
24+
}//end
25+
26+
function bar()
27+
{
28+
}
29+
function bar2()
30+
{
31+
}//end bar2()
32+
33+
$foo->bar(
34+
$arg1,
35+
function ($arg2) use ($var1) {
36+
// body
37+
},
38+
$arg3
39+
);
40+
41+
$instance = new class extends \Foo implements \HandleableInterface {
42+
// Class content
43+
};
44+
45+
$app->get('/hello/{name}', function ($name) use ($app) {
46+
return 'Hello ' . $app->escape($name);
47+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Unit test class for the ClosingBrace sniff.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Classes;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class ClosingBraceUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @return array<int, int>
25+
*/
26+
public function getErrorList()
27+
{
28+
return [
29+
13 => 1,
30+
14 => 1,
31+
19 => 1,
32+
24 => 1,
33+
31 => 1,
34+
];
35+
36+
}//end getErrorList()
37+
38+
39+
/**
40+
* Returns the lines where warnings should occur.
41+
*
42+
* The key of the array should represent the line number and the value
43+
* should represent the number of warnings that should occur on that line.
44+
*
45+
* @return array<int, int>
46+
*/
47+
public function getWarningList()
48+
{
49+
return [];
50+
51+
}//end getWarningList()
52+
53+
54+
}//end class

src/Standards/PSR12/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<!-- 4. Classes, Properties, and Methods -->
116116

117117
<!-- Any closing brace MUST NOT be followed by any comment or statement on the same line. -->
118+
<!-- checked by PSR12.Classes.ClosingBrace -->
118119

119120
<!-- When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor. -->
120121
<!-- checked by PSR12.Classes.ClassInstantiation -->

0 commit comments

Comments
 (0)