Skip to content

Commit

Permalink
Merge pull request #556 from PHPCSStandards/feature/psr2-classdeclara…
Browse files Browse the repository at this point in the history
…tion-add-tests-close-tag

PSR2/ClassDeclaration: add tests and remove some redundant code
  • Loading branch information
jrfnl authored Jul 20, 2024
2 parents 64c2c23 + 8303a3f commit 85318b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,8 @@ public function processClose(File $phpcsFile, $stackPtr)
$ignoreTokens[] = T_WHITESPACE;
$ignoreTokens[] = T_COMMENT;
$ignoreTokens[] = T_SEMICOLON;
$ignoreTokens[] = T_COMMA;
$nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true);
if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
&& $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
) {
if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) {
$type = strtolower($tokens[$stackPtr]['content']);
$error = 'Closing %s brace must be on a line by itself';
$data = [$type];
Expand Down
28 changes: 28 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,31 @@ class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
/* Comment. */ AnotherInterface
{
}

// Verify the `CloseBraceSameLine` error code is thrown when expected.
class ClassBraceNotOnLineByItselfError
{
public $prop;
} $foo = new ClassBraceNotOnLineByItselfError;

interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed
{
public function myMethod();
} //end interface -- this comment is allowed.

trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed
{
} // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed.

// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678.
$foo->bar(
new class implements Bar {
// ...
},
);

enum BraceNotOnLineByItselfCloseTagError
{
} ?>

<?php
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,31 @@ class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
AnotherInterface
{
}

// Verify the `CloseBraceSameLine` error code is thrown when expected.
class ClassBraceNotOnLineByItselfError
{
public $prop;
} $foo = new ClassBraceNotOnLineByItselfError;

interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed
{
public function myMethod();
} //end interface -- this comment is allowed.

trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed
{
} // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed.

// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678.
$foo->bar(
new class implements Bar {
// ...
},
);

enum BraceNotOnLineByItselfCloseTagError
{
} ?>

<?php
2 changes: 2 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function getErrorList()
282 => 1,
310 => 1,
316 => 1,
324 => 1,
344 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 85318b7

Please sign in to comment.