From e83d550e24bdd186274402ae88b796e1526858df Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 12 Aug 2024 17:16:12 -0300 Subject: [PATCH] Tests/Tokenizer: test related to setting the scope for T_CASE This commit copies a sniff test from InlineControlStructureUnitTest.php to the Tokenizer::recurseScopeMap() tests for the `case` keyword. The copied test was added in 65ef053 before the Tokenizer tests were created. It ensures that the scope for the `T_CASE` token is correctly set when there is an inline control structure inside it with more than three lines. The original sniff test was modified to use `T_FOR` instead of `T_SWITCH`, but its purpose was not altered. The test is about adding braces to an `if` that returns a nested function call. --- .../InlineControlStructureUnitTest.1.inc | 20 +++++++++--------- ...InlineControlStructureUnitTest.1.inc.fixed | 21 ++++++++++--------- .../InlineControlStructureUnitTest.php | 3 ++- ...curseScopeMapCaseKeywordConditionsTest.inc | 13 ++++++++++++ ...curseScopeMapCaseKeywordConditionsTest.php | 7 +++++++ 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc index 879ebb5bcd..933b0e381a 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc @@ -201,16 +201,16 @@ if (true) catch(Exception $e) { } -switch ($num) { - case 0: - if (1 > $num) - return bar( - baz( - "foobarbaz" - ) - ); - break; -} +for ($i = 0; $i <= 4; $i++) + if ($i % 2) + return bar( + baz( + "foobarbaz" + ) + ); + + + do { $i++; diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed index 6c25496ed5..8c2574044f 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed @@ -230,18 +230,19 @@ if (true) { } } -switch ($num) { - case 0: - if (1 > $num) { - return bar( - baz( - "foobarbaz" - ) - ); - } - break; +for ($i = 0; $i <= 4; $i++) { + if ($i % 2) { + return bar( + baz( + "foobarbaz" + ) + ); + } } + + + do { $i++; } diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php index 19c78e0f96..22eeb075cc 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php @@ -70,7 +70,8 @@ public function getErrorList($testFile='') 191 => 1, 195 => 1, 198 => 1, - 206 => 1, + 204 => 1, + 205 => 1, 222 => 1, 232 => 1, 235 => 1, diff --git a/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc b/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc index 3b3199cb35..43f9118393 100644 --- a/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc +++ b/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc @@ -117,3 +117,16 @@ switch ($type) { } break; } + +// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/1590 +switch ($num) { + /* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */ + case 0: + if (1 > $num) + return bar( + baz( + "foobarbaz" + ) + ); + break; +} diff --git a/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php b/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php index 819d410020..b638c5ff3e 100644 --- a/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php +++ b/tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php @@ -208,6 +208,13 @@ public static function dataNotEnumCases() 'scope_closer' => T_BREAK, ], ], + 'switch case, nested inline if' => [ + 'testMarker' => '/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */', + 'expectedTokens' => [ + 'scope_opener' => T_COLON, + 'scope_closer' => T_BREAK, + ], + ], ]; }//end dataNotEnumCases()