diff --git a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php index afc7ac071a..f059d05f93 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php @@ -85,15 +85,10 @@ public function process(File $phpcsFile, $stackPtr) $closingBracket = $tokens[$stackPtr]['scope_closer']; - if ($closingBracket === null) { - // Possible inline structure. Other tests will handle it. - return; - } - $data = [$comment]; if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); - if (rtrim($tokens[$next]['content']) === $comment) { + if ($next !== false && rtrim($tokens[$next]['content']) === $comment) { // The comment isn't really missing; it is just in the wrong place. $fix = $phpcsFile->addFixableError('Expected %s directly after closing brace', $closingBracket, 'Misplaced', $data); if ($fix === true) { diff --git a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc similarity index 60% rename from src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc rename to src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc index 1a57149b26..5c9a9bcf4c 100644 --- a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc @@ -84,3 +84,35 @@ enum MissingClosingComment { enum HasClosingComment { }//end enum + +function misplacedClosingCommentWhitespace() { +} //end misplacedClosingCommentWhitespace() + +function misplacedClosingCommentMultipleNewlines() { +} + + +//end misplacedClosingCommentMultipleNewlines() + +function missingClosingComment() { +} + +function commentHasMoreIndentationThanFunction() { +} + //end commentHasMoreIndentationThanFunction() + +class Foo { + function commentHasLessIndentationThanFunction() { + } + //end commentHasLessIndentationThanFunction() + + function misplacedClosingCommentWithIndentation() { + } + //end misplacedClosingCommentWithIndentation() +}//end class + +// Anonymous classes don't need end comments. +$anon = new class {}; + +// Arrow functions don't need end comments. +$arrow = fn($a) => $a; diff --git a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc.fixed similarity index 61% rename from src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc.fixed index 4515c41a4b..0b1f3faa21 100644 --- a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc.fixed @@ -83,3 +83,29 @@ enum MissingClosingComment { enum HasClosingComment { }//end enum + +function misplacedClosingCommentWhitespace() { +}//end misplacedClosingCommentWhitespace() + +function misplacedClosingCommentMultipleNewlines() { +}//end misplacedClosingCommentMultipleNewlines() + +function missingClosingComment() { +}//end missingClosingComment() + +function commentHasMoreIndentationThanFunction() { +}//end commentHasMoreIndentationThanFunction() + +class Foo { + function commentHasLessIndentationThanFunction() { + }//end commentHasLessIndentationThanFunction() + + function misplacedClosingCommentWithIndentation() { + }//end misplacedClosingCommentWithIndentation() +}//end class + +// Anonymous classes don't need end comments. +$anon = new class {}; + +// Arrow functions don't need end comments. +$arrow = fn($a) => $a; diff --git a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.2.inc b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.2.inc new file mode 100644 index 0000000000..25913dd8fc --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 13 => 1, - 17 => 1, - 31 => 1, - 41 => 1, - 59 => 1, - 63 => 1, - 67 => 1, - 79 => 1, - 83 => 1, - ]; + switch ($testFile) { + case 'ClosingDeclarationCommentUnitTest.1.inc': + return [ + 13 => 1, + 17 => 1, + 31 => 1, + 41 => 1, + 59 => 1, + 63 => 1, + 67 => 1, + 79 => 1, + 83 => 1, + 89 => 1, + 92 => 1, + 98 => 1, + 101 => 1, + 106 => 1, + 110 => 1, + ]; + + case 'ClosingDeclarationCommentUnitTest.4.inc': + return [8 => 1]; + + case 'ClosingDeclarationCommentUnitTest.5.inc': + return [11 => 1]; + + default: + return []; + }//end switch }//end getErrorList() @@ -51,11 +71,23 @@ public function getErrorList() * 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. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [71 => 1]; + switch ($testFile) { + case 'ClosingDeclarationCommentUnitTest.1.inc': + return [71 => 1]; + + case 'ClosingDeclarationCommentUnitTest.2.inc': + case 'ClosingDeclarationCommentUnitTest.3.inc': + return [7 => 1]; + + default: + return []; + } }//end getWarningList()