Skip to content

Commit 2d4783c

Browse files
authored
Merge pull request #381 from rodrigoprimo/closing-declaration-comment-improvements
Squiz/ClosingDeclarationComment: fix a non-problematic bug plus some other small improvements
2 parents 8a2b6b1 + 5b597d8 commit 2d4783c

10 files changed

+157
-20
lines changed

src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,10 @@ public function process(File $phpcsFile, $stackPtr)
8585

8686
$closingBracket = $tokens[$stackPtr]['scope_closer'];
8787

88-
if ($closingBracket === null) {
89-
// Possible inline structure. Other tests will handle it.
90-
return;
91-
}
92-
9388
$data = [$comment];
9489
if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) {
9590
$next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true);
96-
if (rtrim($tokens[$next]['content']) === $comment) {
91+
if ($next !== false && rtrim($tokens[$next]['content']) === $comment) {
9792
// The comment isn't really missing; it is just in the wrong place.
9893
$fix = $phpcsFile->addFixableError('Expected %s directly after closing brace', $closingBracket, 'Misplaced', $data);
9994
if ($fix === true) {

src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc renamed to src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,35 @@ enum MissingClosingComment {
8484

8585
enum HasClosingComment {
8686
}//end enum
87+
88+
function misplacedClosingCommentWhitespace() {
89+
} //end misplacedClosingCommentWhitespace()
90+
91+
function misplacedClosingCommentMultipleNewlines() {
92+
}
93+
94+
95+
//end misplacedClosingCommentMultipleNewlines()
96+
97+
function missingClosingComment() {
98+
}
99+
100+
function commentHasMoreIndentationThanFunction() {
101+
}
102+
//end commentHasMoreIndentationThanFunction()
103+
104+
class Foo {
105+
function commentHasLessIndentationThanFunction() {
106+
}
107+
//end commentHasLessIndentationThanFunction()
108+
109+
function misplacedClosingCommentWithIndentation() {
110+
}
111+
//end misplacedClosingCommentWithIndentation()
112+
}//end class
113+
114+
// Anonymous classes don't need end comments.
115+
$anon = new class {};
116+
117+
// Arrow functions don't need end comments.
118+
$arrow = fn($a) => $a;

src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.inc.fixed renamed to src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.1.inc.fixed

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,29 @@ enum MissingClosingComment {
8383

8484
enum HasClosingComment {
8585
}//end enum
86+
87+
function misplacedClosingCommentWhitespace() {
88+
}//end misplacedClosingCommentWhitespace()
89+
90+
function misplacedClosingCommentMultipleNewlines() {
91+
}//end misplacedClosingCommentMultipleNewlines()
92+
93+
function missingClosingComment() {
94+
}//end missingClosingComment()
95+
96+
function commentHasMoreIndentationThanFunction() {
97+
}//end commentHasMoreIndentationThanFunction()
98+
99+
class Foo {
100+
function commentHasLessIndentationThanFunction() {
101+
}//end commentHasLessIndentationThanFunction()
102+
103+
function misplacedClosingCommentWithIndentation() {
104+
}//end misplacedClosingCommentWithIndentation()
105+
}//end class
106+
107+
// Anonymous classes don't need end comments.
108+
$anon = new class {};
109+
110+
// Arrow functions don't need end comments.
111+
$arrow = fn($a) => $a;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening bracket).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is triggered.
6+
7+
class MissingOpeningBracket
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing closing bracket).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is triggered.
6+
7+
class MissingClosingBracket {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// This should be the only test in this file.
4+
// Testing that the sniff is triggered when the closing bracket is
5+
// the last character in the file (no newline after it).
6+
7+
function closingBraceAtEndOfFile() {
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// This should be the only test in this file.
4+
// Testing that the sniff is triggered when the closing bracket is
5+
// the last character in the file (no newline after it).
6+
7+
function closingBraceAtEndOfFile() {
8+
}//end closingBraceAtEndOfFile()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//end closingBraceAtEndOfFileMissingComment()
2+
3+
<?php
4+
5+
// This should be the only test in this file.
6+
// Testing that the sniff is triggered and the fixer works when the closing bracket is
7+
// the last character in the file (no newline after it) and the content of the first token
8+
// is a "//end closingBraceAtEndOfFileMissingComment()" comment.
9+
10+
function closingBraceAtEndOfFileMissingComment() {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//end closingBraceAtEndOfFileMissingComment()
2+
3+
<?php
4+
5+
// This should be the only test in this file.
6+
// Testing that the sniff is triggered and the fixer works when the closing bracket is
7+
// the last character in the file (no newline after it) and the content of the first token
8+
// is a "//end closingBraceAtEndOfFileMissingComment()" comment.
9+
10+
function closingBraceAtEndOfFileMissingComment() {
11+
}//end closingBraceAtEndOfFileMissingComment()

src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,41 @@ final class ClosingDeclarationCommentUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the test file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
13 => 1,
35-
17 => 1,
36-
31 => 1,
37-
41 => 1,
38-
59 => 1,
39-
63 => 1,
40-
67 => 1,
41-
79 => 1,
42-
83 => 1,
43-
];
35+
switch ($testFile) {
36+
case 'ClosingDeclarationCommentUnitTest.1.inc':
37+
return [
38+
13 => 1,
39+
17 => 1,
40+
31 => 1,
41+
41 => 1,
42+
59 => 1,
43+
63 => 1,
44+
67 => 1,
45+
79 => 1,
46+
83 => 1,
47+
89 => 1,
48+
92 => 1,
49+
98 => 1,
50+
101 => 1,
51+
106 => 1,
52+
110 => 1,
53+
];
54+
55+
case 'ClosingDeclarationCommentUnitTest.4.inc':
56+
return [8 => 1];
57+
58+
case 'ClosingDeclarationCommentUnitTest.5.inc':
59+
return [11 => 1];
60+
61+
default:
62+
return [];
63+
}//end switch
4464

4565
}//end getErrorList()
4666

@@ -51,11 +71,23 @@ public function getErrorList()
5171
* The key of the array should represent the line number and the value
5272
* should represent the number of warnings that should occur on that line.
5373
*
74+
* @param string $testFile The name of the test file being tested.
75+
*
5476
* @return array<int, int>
5577
*/
56-
public function getWarningList()
78+
public function getWarningList($testFile='')
5779
{
58-
return [71 => 1];
80+
switch ($testFile) {
81+
case 'ClosingDeclarationCommentUnitTest.1.inc':
82+
return [71 => 1];
83+
84+
case 'ClosingDeclarationCommentUnitTest.2.inc':
85+
case 'ClosingDeclarationCommentUnitTest.3.inc':
86+
return [7 => 1];
87+
88+
default:
89+
return [];
90+
}
5991

6092
}//end getWarningList()
6193

0 commit comments

Comments
 (0)