Skip to content

Commit 992cc6b

Browse files
jrfnlgsherwood
authored andcommitted
PHP 8.0 | Squiz/LongConditionClosingComment: include match expressions
Include match expressions in the control structures for which an end comment is expected and make sure the comment is after the semi-colon/comma. Includes unit test.
1 parent bda987e commit 992cc6b

File tree

4 files changed

+183
-26
lines changed

4 files changed

+183
-26
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class LongConditionClosingCommentSniff implements Sniff
2828
T_WHILE,
2929
T_TRY,
3030
T_CASE,
31+
T_MATCH,
3132
];
3233

3334
/**
@@ -144,6 +145,17 @@ public function process(File $phpcsFile, $stackPtr)
144145
} while (isset($tokens[$nextToken]['scope_closer']) === true);
145146
}
146147

148+
if ($startCondition['code'] === T_MATCH) {
149+
// Move the stackPtr to after the semi-colon/comma if there is one.
150+
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
151+
if ($nextToken !== false
152+
&& ($tokens[$nextToken]['code'] === T_SEMICOLON
153+
|| $tokens[$nextToken]['code'] === T_COMMA)
154+
) {
155+
$stackPtr = $nextToken;
156+
}
157+
}
158+
147159
$lineDifference = ($endBrace['line'] - $startBrace['line']);
148160

149161
$expected = sprintf($this->commentFormat, $startCondition['content']);

src/Standards/Squiz/Tests/Commenting/LongConditionClosingCommentUnitTest.inc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,74 @@ try {
960960
} finally {
961961
// some code here.
962962
}
963+
964+
$expr = match ($foo) {
965+
// Line 1
966+
// Line 2
967+
// Line 3
968+
// Line 4
969+
// Line 5
970+
// Line 6
971+
// Line 7
972+
// Line 8
973+
// Line 9
974+
// Line 10
975+
// Line 11
976+
// Line 12
977+
// Line 13
978+
// Line 14
979+
// Line 15
980+
// Line 16
981+
// Line 17
982+
// Line 18
983+
// Line 19
984+
// Line 20
985+
}; //end switch
986+
987+
$expr = match ($foo) {
988+
// Line 1
989+
// Line 2
990+
// Line 3
991+
// Line 4
992+
// Line 5
993+
// Line 6
994+
// Line 7
995+
// Line 8
996+
// Line 9
997+
// Line 10
998+
// Line 11
999+
// Line 12
1000+
// Line 13
1001+
// Line 14
1002+
// Line 15
1003+
// Line 16
1004+
// Line 17
1005+
// Line 18
1006+
// Line 19
1007+
// Line 20
1008+
};
1009+
1010+
$array = [
1011+
'match' => match ($foo) {
1012+
// Line 1
1013+
// Line 2
1014+
// Line 3
1015+
// Line 4
1016+
// Line 5
1017+
// Line 6
1018+
// Line 7
1019+
// Line 8
1020+
// Line 9
1021+
// Line 10
1022+
// Line 11
1023+
// Line 12
1024+
// Line 13
1025+
// Line 14
1026+
// Line 15
1027+
// Line 16
1028+
// Line 17
1029+
// Line 18
1030+
// Line 19
1031+
// Line 20
1032+
},
1033+
];

src/Standards/Squiz/Tests/Commenting/LongConditionClosingCommentUnitTest.inc.fixed

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,74 @@ try {
960960
} finally {
961961
// some code here.
962962
}//end try
963+
964+
$expr = match ($foo) {
965+
// Line 1
966+
// Line 2
967+
// Line 3
968+
// Line 4
969+
// Line 5
970+
// Line 6
971+
// Line 7
972+
// Line 8
973+
// Line 9
974+
// Line 10
975+
// Line 11
976+
// Line 12
977+
// Line 13
978+
// Line 14
979+
// Line 15
980+
// Line 16
981+
// Line 17
982+
// Line 18
983+
// Line 19
984+
// Line 20
985+
}; //end match
986+
987+
$expr = match ($foo) {
988+
// Line 1
989+
// Line 2
990+
// Line 3
991+
// Line 4
992+
// Line 5
993+
// Line 6
994+
// Line 7
995+
// Line 8
996+
// Line 9
997+
// Line 10
998+
// Line 11
999+
// Line 12
1000+
// Line 13
1001+
// Line 14
1002+
// Line 15
1003+
// Line 16
1004+
// Line 17
1005+
// Line 18
1006+
// Line 19
1007+
// Line 20
1008+
};//end match
1009+
1010+
$array = [
1011+
'match' => match ($foo) {
1012+
// Line 1
1013+
// Line 2
1014+
// Line 3
1015+
// Line 4
1016+
// Line 5
1017+
// Line 6
1018+
// Line 7
1019+
// Line 8
1020+
// Line 9
1021+
// Line 10
1022+
// Line 11
1023+
// Line 12
1024+
// Line 13
1025+
// Line 14
1026+
// Line 15
1027+
// Line 16
1028+
// Line 17
1029+
// Line 18
1030+
// Line 19
1031+
// Line 20
1032+
},//end match
1033+
];

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,35 @@ class LongConditionClosingCommentUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
49 => 1,
30-
99 => 1,
31-
146 => 1,
32-
192 => 1,
33-
215 => 1,
34-
238 => 1,
35-
261 => 1,
36-
286 => 1,
37-
309 => 1,
38-
332 => 1,
39-
355 => 1,
40-
378 => 1,
41-
493 => 1,
42-
531 => 1,
43-
536 => 1,
44-
540 => 1,
45-
562 => 1,
46-
601 => 1,
47-
629 => 1,
48-
663 => 1,
49-
765 => 1,
50-
798 => 1,
51-
811 => 1,
52-
897 => 1,
53-
931 => 1,
54-
962 => 1,
29+
49 => 1,
30+
99 => 1,
31+
146 => 1,
32+
192 => 1,
33+
215 => 1,
34+
238 => 1,
35+
261 => 1,
36+
286 => 1,
37+
309 => 1,
38+
332 => 1,
39+
355 => 1,
40+
378 => 1,
41+
493 => 1,
42+
531 => 1,
43+
536 => 1,
44+
540 => 1,
45+
562 => 1,
46+
601 => 1,
47+
629 => 1,
48+
663 => 1,
49+
765 => 1,
50+
798 => 1,
51+
811 => 1,
52+
897 => 1,
53+
931 => 1,
54+
962 => 1,
55+
985 => 2,
56+
1008 => 1,
57+
1032 => 1,
5558
];
5659

5760
}//end getErrorList()

0 commit comments

Comments
 (0)