Skip to content

Commit 8bfae07

Browse files
committed
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 8b0137a commit 8bfae07

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
@@ -38,6 +38,7 @@ class LongConditionClosingCommentSniff implements Sniff
3838
T_WHILE,
3939
T_TRY,
4040
T_CASE,
41+
T_MATCH,
4142
];
4243

4344
/**
@@ -154,6 +155,17 @@ public function process(File $phpcsFile, $stackPtr)
154155
} while (isset($tokens[$nextToken]['scope_closer']) === true);
155156
}
156157

158+
if ($startCondition['code'] === T_MATCH) {
159+
// Move the stackPtr to after the semi-colon/comma if there is one.
160+
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
161+
if ($nextToken !== false
162+
&& ($tokens[$nextToken]['code'] === T_SEMICOLON
163+
|| $tokens[$nextToken]['code'] === T_COMMA)
164+
) {
165+
$stackPtr = $nextToken;
166+
}
167+
}
168+
157169
$lineDifference = ($endBrace['line'] - $startBrace['line']);
158170

159171
$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
@@ -30,32 +30,35 @@ public function getErrorList($testFile='LongConditionClosingCommentUnitTest.inc'
3030
switch ($testFile) {
3131
case 'LongConditionClosingCommentUnitTest.inc':
3232
return [
33-
49 => 1,
34-
99 => 1,
35-
146 => 1,
36-
192 => 1,
37-
215 => 1,
38-
238 => 1,
39-
261 => 1,
40-
286 => 1,
41-
309 => 1,
42-
332 => 1,
43-
355 => 1,
44-
378 => 1,
45-
493 => 1,
46-
531 => 1,
47-
536 => 1,
48-
540 => 1,
49-
562 => 1,
50-
601 => 1,
51-
629 => 1,
52-
663 => 1,
53-
765 => 1,
54-
798 => 1,
55-
811 => 1,
56-
897 => 1,
57-
931 => 1,
58-
962 => 1,
33+
49 => 1,
34+
99 => 1,
35+
146 => 1,
36+
192 => 1,
37+
215 => 1,
38+
238 => 1,
39+
261 => 1,
40+
286 => 1,
41+
309 => 1,
42+
332 => 1,
43+
355 => 1,
44+
378 => 1,
45+
493 => 1,
46+
531 => 1,
47+
536 => 1,
48+
540 => 1,
49+
562 => 1,
50+
601 => 1,
51+
629 => 1,
52+
663 => 1,
53+
765 => 1,
54+
798 => 1,
55+
811 => 1,
56+
897 => 1,
57+
931 => 1,
58+
962 => 1,
59+
985 => 2,
60+
1008 => 1,
61+
1032 => 1,
5962
];
6063
break;
6164
case 'LongConditionClosingCommentUnitTest.js':

0 commit comments

Comments
 (0)