Skip to content

Commit

Permalink
PHP 8.0 | Squiz/LongConditionClosingComment: include match expressions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Feb 23, 2021
1 parent 8b0137a commit 8bfae07
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class LongConditionClosingCommentSniff implements Sniff
T_WHILE,
T_TRY,
T_CASE,
T_MATCH,
];

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

if ($startCondition['code'] === T_MATCH) {
// Move the stackPtr to after the semi-colon/comma if there is one.
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($nextToken !== false
&& ($tokens[$nextToken]['code'] === T_SEMICOLON
|| $tokens[$nextToken]['code'] === T_COMMA)
) {
$stackPtr = $nextToken;
}
}

$lineDifference = ($endBrace['line'] - $startBrace['line']);

$expected = sprintf($this->commentFormat, $startCondition['content']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,3 +960,74 @@ try {
} finally {
// some code here.
}

$expr = match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
}; //end switch

$expr = match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
};

$array = [
'match' => match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -960,3 +960,74 @@ try {
} finally {
// some code here.
}//end try

$expr = match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
}; //end match

$expr = match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
};//end match

$array = [
'match' => match ($foo) {
// Line 1
// Line 2
// Line 3
// Line 4
// Line 5
// Line 6
// Line 7
// Line 8
// Line 9
// Line 10
// Line 11
// Line 12
// Line 13
// Line 14
// Line 15
// Line 16
// Line 17
// Line 18
// Line 19
// Line 20
},//end match
];
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,35 @@ public function getErrorList($testFile='LongConditionClosingCommentUnitTest.inc'
switch ($testFile) {
case 'LongConditionClosingCommentUnitTest.inc':
return [
49 => 1,
99 => 1,
146 => 1,
192 => 1,
215 => 1,
238 => 1,
261 => 1,
286 => 1,
309 => 1,
332 => 1,
355 => 1,
378 => 1,
493 => 1,
531 => 1,
536 => 1,
540 => 1,
562 => 1,
601 => 1,
629 => 1,
663 => 1,
765 => 1,
798 => 1,
811 => 1,
897 => 1,
931 => 1,
962 => 1,
49 => 1,
99 => 1,
146 => 1,
192 => 1,
215 => 1,
238 => 1,
261 => 1,
286 => 1,
309 => 1,
332 => 1,
355 => 1,
378 => 1,
493 => 1,
531 => 1,
536 => 1,
540 => 1,
562 => 1,
601 => 1,
629 => 1,
663 => 1,
765 => 1,
798 => 1,
811 => 1,
897 => 1,
931 => 1,
962 => 1,
985 => 2,
1008 => 1,
1032 => 1,
];
break;
case 'LongConditionClosingCommentUnitTest.js':
Expand Down

0 comments on commit 8bfae07

Please sign in to comment.