Skip to content

Commit

Permalink
Fixed bug #3437 : PSR12 does not forbid blank lines at the start of t…
Browse files Browse the repository at this point in the history
…he class body
  • Loading branch information
gsherwood committed Nov 22, 2021
1 parent ef05ec2 commit 165d00b
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
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 <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\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 <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 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 165d00b

Please sign in to comment.