Skip to content

Commit 0ed1d8c

Browse files
committed
Fixed bug #3437 : PSR12 does not forbid blank lines at the start of the class body
1 parent 5fb9b64 commit 0ed1d8c

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

package.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3232
-- Thanks to Juliette Reinders Folmer and Graham Wharton for the patch
3333
- Fixed bug #3422 : Squiz.WhiteSpace.ScopeClosingBrace fixer removes HTML content when fixing closing brace alignment
3434
-- Thanks to Juliette Reinders Folmer for the patch
35+
- Fixed bug #3437 : PSR12 does not forbid blank lines at the start of the class body
36+
-- Added new PSR12.Classes.OpeningBraceSpace sniff to enforce this
3537
- Fixed bug #3448 : PHP 8.1 deprecation notice while generating running time value
3638
-- Thanks to Juliette Reinders Folmer and Andy Postnikov for the patch
3739
</notes>
@@ -1109,6 +1111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
11091111
<file baseinstalldir="PHP/CodeSniffer" name="AnonClassDeclarationSniff.php" role="php" />
11101112
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationSniff.php" role="php" />
11111113
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceSniff.php" role="php" />
1114+
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceSniff.php" role="php" />
11121115
</dir>
11131116
<dir name="ControlStructures">
11141117
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementSniff.php" role="php" />
@@ -1150,6 +1153,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
11501153
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.php" role="test" />
11511154
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.inc" role="test" />
11521155
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceUnitTest.php" role="test" />
1156+
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.inc" role="test" />
1157+
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.inc.fixed" role="test" />
1158+
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.php" role="test" />
11531159
</dir>
11541160
<dir name="ControlStructures">
11551161
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementUnitTest.inc" role="test" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Verifies that opening braces are not followed by blank lines.
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\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
15+
16+
class OpeningBraceSpaceSniff 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 Tokens::$ooScopeTokens;
28+
29+
}//end register()
30+
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token in the
37+
* stack passed in $tokens.
38+
*
39+
* @return void
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
45+
return;
46+
}
47+
48+
$opener = $tokens[$stackPtr]['scope_opener'];
49+
$next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);
50+
if ($next === false
51+
|| $tokens[$next]['line'] <= ($tokens[$opener]['line'] + 1)
52+
) {
53+
return;
54+
}
55+
56+
$error = 'Opening brace must not be followed by a blank line';
57+
$fix = $phpcsFile->addFixableError($error, $opener, 'Found');
58+
if ($fix === false) {
59+
return;
60+
}
61+
62+
$phpcsFile->fixer->beginChangeset();
63+
for ($i = ($opener + 1); $i < $next; $i++) {
64+
if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
65+
continue;
66+
}
67+
68+
if ($tokens[$i]['line'] === $tokens[$next]['line']) {
69+
break;
70+
}
71+
72+
$phpcsFile->fixer->replaceToken($i, '');
73+
}
74+
75+
$phpcsFile->fixer->endChangeset();
76+
77+
}//end process()
78+
79+
80+
}//end class
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
class Foo
3+
{}
4+
5+
class Foo1
6+
{
7+
}
8+
9+
class Foo2
10+
{
11+
12+
public function foo()
13+
{
14+
}
15+
}
16+
17+
interface Foo3
18+
{
19+
20+
21+
}
22+
23+
trait Foo4
24+
{
25+
26+
}
27+
28+
class Foo5
29+
{
30+
// Comment here
31+
}
32+
33+
class Foo6
34+
{
35+
36+
37+
38+
public const X = 1;
39+
}
40+
41+
$instance = new class extends \Foo implements \HandleableInterface {
42+
43+
// Class content
44+
};
45+
46+
$instance = new class extends \Foo implements \HandleableInterface {
47+
// Class content
48+
};
49+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
class Foo
3+
{}
4+
5+
class Foo1
6+
{
7+
}
8+
9+
class Foo2
10+
{
11+
public function foo()
12+
{
13+
}
14+
}
15+
16+
interface Foo3
17+
{
18+
}
19+
20+
trait Foo4
21+
{
22+
}
23+
24+
class Foo5
25+
{
26+
// Comment here
27+
}
28+
29+
class Foo6
30+
{
31+
public const X = 1;
32+
}
33+
34+
$instance = new class extends \Foo implements \HandleableInterface {
35+
// Class content
36+
};
37+
38+
$instance = new class extends \Foo implements \HandleableInterface {
39+
// Class content
40+
};
41+
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 OpeningBraceSpace 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 OpeningBraceSpaceUnitTest 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+
10 => 1,
30+
18 => 1,
31+
24 => 1,
32+
34 => 1,
33+
41 => 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
@@ -123,6 +123,7 @@
123123
<!-- Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line. -->
124124
<!-- Closing braces MUST be on their own line and MUST NOT be preceded by a blank line. -->
125125
<!-- Lists of implements and, in the case of interfaces, extends MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. -->
126+
<!-- checked by PSR12.Classes.OpeningBraceSpace -->
126127
<rule ref="PSR2.Classes.ClassDeclaration"/>
127128

128129
<!-- 4.2 Using traits -->

0 commit comments

Comments
 (0)