Skip to content

Commit

Permalink
Fixed bug squizlabs#3437 : PSR12 does not forbid blank lines at the s…
Browse files Browse the repository at this point in the history
…tart of the class body
  • Loading branch information
gsherwood committed Nov 22, 2021
1 parent 5fb9b64 commit 0ed1d8c
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Juliette Reinders Folmer and Graham Wharton for the patch
- Fixed bug #3422 : Squiz.WhiteSpace.ScopeClosingBrace fixer removes HTML content when fixing closing brace alignment
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3437 : PSR12 does not forbid blank lines at the start of the class body
-- Added new PSR12.Classes.OpeningBraceSpace sniff to enforce this
- Fixed bug #3448 : PHP 8.1 deprecation notice while generating running time value
-- Thanks to Juliette Reinders Folmer and Andy Postnikov for the patch
</notes>
Expand Down Expand Up @@ -1109,6 +1111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="AnonClassDeclarationSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingBraceSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceSniff.php" role="php" />
</dir>
<dir name="ControlStructures">
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementSniff.php" role="php" />
Expand Down Expand Up @@ -1150,6 +1153,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<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" />
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSpaceUnitTest.php" role="test" />
</dir>
<dir name="ControlStructures">
<file baseinstalldir="PHP/CodeSniffer" name="BooleanOperatorPlacementUnitTest.inc" role="test" />
Expand Down
80 changes: 80 additions & 0 deletions src/Standards/PSR12/Sniffs/Classes/OpeningBraceSpaceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Verifies that opening braces are not followed by blank lines.
*
* @author Greg Sherwood <[email protected]>
* @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\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class OpeningBraceSpaceSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return Tokens::$ooScopeTokens;

}//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_opener']) === false) {
return;
}

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

$error = 'Opening brace must not be followed by a blank line';
$fix = $phpcsFile->addFixableError($error, $opener, 'Found');
if ($fix === false) {
return;
}

$phpcsFile->fixer->beginChangeset();
for ($i = ($opener + 1); $i < $next; $i++) {
if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
continue;
}

if ($tokens[$i]['line'] === $tokens[$next]['line']) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();

}//end process()


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

class Foo1
{
}

class Foo2
{

public function foo()
{
}
}

interface Foo3
{


}

trait Foo4
{

}

class Foo5
{
// Comment here
}

class Foo6
{



public const X = 1;
}

$instance = new class extends \Foo implements \HandleableInterface {

// Class content
};

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
class Foo
{}

class Foo1
{
}

class Foo2
{
public function foo()
{
}
}

interface Foo3
{
}

trait Foo4
{
}

class Foo5
{
// Comment here
}

class Foo6
{
public const X = 1;
}

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

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

54 changes: 54 additions & 0 deletions src/Standards/PSR12/Tests/Classes/OpeningBraceSpaceUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Unit test class for the OpeningBraceSpace sniff.
*
* @author Greg Sherwood <[email protected]>
* @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 OpeningBraceSpaceUnitTest 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 [
10 => 1,
18 => 1,
24 => 1,
34 => 1,
41 => 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 @@ -123,6 +123,7 @@
<!-- Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line. -->
<!-- Closing braces MUST be on their own line and MUST NOT be preceded by a blank line. -->
<!-- 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. -->
<!-- checked by PSR12.Classes.OpeningBraceSpace -->
<rule ref="PSR2.Classes.ClassDeclaration"/>

<!-- 4.2 Using traits -->
Expand Down

0 comments on commit 0ed1d8c

Please sign in to comment.