-
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.
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.
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
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
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
47
src/Standards/PSR12/Tests/Classes/ClosingBraceUnitTest.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,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
54
src/Standards/PSR12/Tests/Classes/ClosingBraceUnitTest.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,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 |
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