Skip to content

Commit 85318b7

Browse files
authored
Merge pull request #556 from PHPCSStandards/feature/psr2-classdeclaration-add-tests-close-tag
PSR2/ClassDeclaration: add tests and remove some redundant code
2 parents 64c2c23 + 8303a3f commit 85318b7

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,8 @@ public function processClose(File $phpcsFile, $stackPtr)
525525
$ignoreTokens[] = T_WHITESPACE;
526526
$ignoreTokens[] = T_COMMENT;
527527
$ignoreTokens[] = T_SEMICOLON;
528-
$ignoreTokens[] = T_COMMA;
529528
$nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true);
530-
if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
531-
&& $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
532-
) {
529+
if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) {
533530
$type = strtolower($tokens[$stackPtr]['content']);
534531
$error = 'Closing %s brace must be on a line by itself';
535532
$data = [$type];

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc

+28
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,31 @@ class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
316316
/* Comment. */ AnotherInterface
317317
{
318318
}
319+
320+
// Verify the `CloseBraceSameLine` error code is thrown when expected.
321+
class ClassBraceNotOnLineByItselfError
322+
{
323+
public $prop;
324+
} $foo = new ClassBraceNotOnLineByItselfError;
325+
326+
interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed
327+
{
328+
public function myMethod();
329+
} //end interface -- this comment is allowed.
330+
331+
trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed
332+
{
333+
} // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed.
334+
335+
// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678.
336+
$foo->bar(
337+
new class implements Bar {
338+
// ...
339+
},
340+
);
341+
342+
enum BraceNotOnLineByItselfCloseTagError
343+
{
344+
} ?>
345+
346+
<?php

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

+28
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,31 @@ class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
305305
AnotherInterface
306306
{
307307
}
308+
309+
// Verify the `CloseBraceSameLine` error code is thrown when expected.
310+
class ClassBraceNotOnLineByItselfError
311+
{
312+
public $prop;
313+
} $foo = new ClassBraceNotOnLineByItselfError;
314+
315+
interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed
316+
{
317+
public function myMethod();
318+
} //end interface -- this comment is allowed.
319+
320+
trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed
321+
{
322+
} // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed.
323+
324+
// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678.
325+
$foo->bar(
326+
new class implements Bar {
327+
// ...
328+
},
329+
);
330+
331+
enum BraceNotOnLineByItselfCloseTagError
332+
{
333+
} ?>
334+
335+
<?php

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function getErrorList()
7878
282 => 1,
7979
310 => 1,
8080
316 => 1,
81+
324 => 1,
82+
344 => 1,
8183
];
8284

8385
}//end getErrorList()

0 commit comments

Comments
 (0)