Skip to content

Commit

Permalink
Added PSR12.Classes.ClosingBrace sniff to enforce that closing braces…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
gsherwood committed Sep 5, 2019
1 parent f1b421e commit b73f463
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Mponos George for the contribution
- Added Generic.PHP.RequireStrictTypes sniff
-- Enforce the use of a strict types declaration in PHP files
- Added PSR12.Classes.ClosingBrace sniff
-- Enforces that closing braces of classes/interfaces/traits/functions are not followed by a comment or statement
- Added PSR12.ControlStructures.BooleanOperatorPlacement sniff
-- Enforces that boolean operators between conditions are consistently at the start or end of the line
- Added PSR12.ControlStructures.ControlStructureSpacing sniff
Expand Down Expand Up @@ -1119,6 +1121,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<dir name="Sniffs">
<dir name="Classes">
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceSniff.php" role="php" />
</dir>
<dir name="ControlStructures">
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementSniff.php" role="php" />
Expand Down Expand Up @@ -1155,6 +1158,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.php" role="test" />
</dir>
<dir name="ControlStructures">
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementUnitTest.inc" role="test" />
Expand Down
67 changes: 67 additions & 0 deletions src/Standards/PSR12/Sniffs/Classes/ClosingBraceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Verifies that closing braces are the last content on a line.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2019 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\PSR12\Sniffs\Classes;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class ClosingBraceSniff implements Sniff
{


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

}//end register()


/**
* Processes this test, 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.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}

$closer = $tokens[$stackPtr]['scope_closer'];
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true);
if ($next === false
|| $tokens[$next]['line'] !== $tokens[$closer]['line']
) {
return;
}

$error = 'Closing brace must not be followed by any comment or statement on the same line';
$phpcsFile->addError($error, $closer, 'StatementAfter');

}//end process()


}//end class
47 changes: 47 additions & 0 deletions src/Standards/PSR12/Tests/Classes/ClosingBraceUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
class Foo
{
}

class Foo1
{
public function foo()
{
}
public function foo2()
{
}//end foo2()
}//end class

interface Foo2
{

}echo 'hi';

trait Foo3
{

}//end

function bar()
{
}
function bar2()
{
}//end bar2()

$foo->bar(
$arg1,
function ($arg2) use ($var1) {
// body
},
$arg3
);

$instance = new class extends \Foo implements \HandleableInterface {
// Class content
};

$app->get('/hello/{name}', function ($name) use ($app) {
return 'Hello ' . $app->escape($name);
});
54 changes: 54 additions & 0 deletions src/Standards/PSR12/Tests/Classes/ClosingBraceUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Unit test class for the ClosingBrace sniff.
*
* @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\PSR12\Tests\Classes;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ClosingBraceUnitTest 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 [
13 => 1,
14 => 1,
19 => 1,
24 => 1,
31 => 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 [];

}//end getWarningList()


}//end class
1 change: 1 addition & 0 deletions src/Standards/PSR12/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<!-- 4. Classes, Properties, and Methods -->

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

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

0 comments on commit b73f463

Please sign in to comment.