forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug squizlabs#3437 : PSR12 does not forbid blank lines at the s…
…tart of the class body
- Loading branch information
Showing
6 changed files
with
231 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
80 changes: 80 additions & 0 deletions
80
src/Standards/PSR12/Sniffs/Classes/OpeningBraceSpaceSniff.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,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
49
src/Standards/PSR12/Tests/Classes/OpeningBraceSpaceUnitTest.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,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 | ||
}; | ||
|
41 changes: 41 additions & 0 deletions
41
src/Standards/PSR12/Tests/Classes/OpeningBraceSpaceUnitTest.inc.fixed
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,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
54
src/Standards/PSR12/Tests/Classes/OpeningBraceSpaceUnitTest.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 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 |
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