From 7954ce3fc7deb6ffec9201ab91f02ad85e8ab8b7 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 12 Feb 2025 10:45:06 -0300 Subject: [PATCH 1/2] Tests/Tokenizer: improve stability of the switch keyword tests This commits improves the stability of the switch keyword tests by ensuring that specific markers are used to get the scope closer and scope opener tokens. --- .../Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc | 6 ++++-- .../Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc index 71ef02897a..c77cfbca5c 100644 --- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc +++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc @@ -8,11 +8,12 @@ switch ($value) { default : echo 'other'; break; +/* testSwitchNormalSyntaxScopeCloser */ } // Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497 /* testSwitchAlternativeSyntax */ -switch ($value): +switch ($value) : /* testSwitchAlternativeSyntaxEnsureTestWillNotPickUpWrongColon */ case 1: echo 'one'; @@ -20,7 +21,7 @@ switch ($value): default: echo 'other'; break; -/* testSwitchAlternativeSyntaxEnd */ +/* testSwitchAlternativeSyntaxScopeCloser */ endswitch; // Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/543 @@ -30,4 +31,5 @@ switch((function () { })()) /* testSwitchClosureWithinConditionScopeOpener */ { case 1: return 'test'; +/* testSwitchClosureWithinConditionScopeCloser */ } diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php index 0977c0365c..4484be533d 100644 --- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php +++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php @@ -98,11 +98,13 @@ public static function dataSwitchScope() { return [ 'switch normal syntax' => [ - 'testMarker' => '/* testSwitchNormalSyntax */', - 'expectedTokens' => [ + 'testMarker' => '/* testSwitchNormalSyntax */', + 'expectedTokens' => [ 'scope_opener' => T_OPEN_CURLY_BRACKET, 'scope_closer' => T_CLOSE_CURLY_BRACKET, ], + 'testOpenerMarker' => null, + 'testCloserMarker' => '/* testSwitchNormalSyntaxScopeCloser */', ], 'switch alternative syntax' => [ 'testMarker' => '/* testSwitchAlternativeSyntax */', @@ -111,7 +113,7 @@ public static function dataSwitchScope() 'scope_closer' => T_ENDSWITCH, ], 'testOpenerMarker' => null, - 'testCloserMarker' => '/* testSwitchAlternativeSyntaxEnd */', + 'testCloserMarker' => '/* testSwitchAlternativeSyntaxScopeCloser */', ], 'switch with closure in the condition' => [ 'testMarker' => '/* testSwitchClosureWithinCondition */', @@ -120,7 +122,7 @@ public static function dataSwitchScope() 'scope_closer' => T_CLOSE_CURLY_BRACKET, ], 'testOpenerMarker' => '/* testSwitchClosureWithinConditionScopeOpener */', - 'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeOpener */', + 'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeCloser */', ], ]; From 0244678e6894b0c3dd9d92fd7cd40f0595d4342a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 12 Feb 2025 10:50:06 -0300 Subject: [PATCH 2/2] Tests/Tokenizer: add three more test cases to the switch keyword tests This commit expands a bit the switch keyword tests by adding three more test cases: - switch with closure containing return type in the condition - switch with arrow function in the condition - switch with arrow function containing return type in the condition --- .../RecurseScopeMapSwitchTokenScopeTest.inc | 25 ++++++++++++++ .../RecurseScopeMapSwitchTokenScopeTest.php | 34 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc index c77cfbca5c..6df910beda 100644 --- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc +++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc @@ -33,3 +33,28 @@ switch((function () { return 'test'; /* testSwitchClosureWithinConditionScopeCloser */ } + +/* testSwitchClosureWithReturnTypeWithinCondition */ +switch((function (): string { + return 'bar'; +})()) /* testSwitchClosureWithReturnTypeWithinConditionScopeOpener */ : + /* testSwitchClosureWithReturnTypeWithinConditionEnsureTestWillNotPickUpWrongColon */ + case 1: + return 'test'; +/* testSwitchClosureWithReturnTypeWithinConditionScopeCloser */ +endswitch; + +/* testSwitchArrowFunctionWithinCondition */ +switch((fn() => $obj->{$foo . $bar})()) /* testSwitchArrowFunctionWithinConditionScopeOpener */ { + case 1: + return 'test'; +/* testSwitchArrowFunctionWithinConditionScopeCloser */ +} + +/* testSwitchArrowFunctionWithReturnTypeWithinCondition */ +switch((fn(): string => $condition ? 'foo' : 'bar')()) /* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeOpener */ : + /* testSwitchArrowFunctionWithReturnTypeWithinConditionEnsureTestWillNotPickUpWrongColon */ + case 1: + return 'test'; +/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeCloser */ +endswitch; diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php index 4484be533d..f48b32401a 100644 --- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php +++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php @@ -97,7 +97,7 @@ public function testSwitchScope($testMarker, $expectedTokens, $testOpenerMarker= public static function dataSwitchScope() { return [ - 'switch normal syntax' => [ + 'switch normal syntax' => [ 'testMarker' => '/* testSwitchNormalSyntax */', 'expectedTokens' => [ 'scope_opener' => T_OPEN_CURLY_BRACKET, @@ -106,7 +106,7 @@ public static function dataSwitchScope() 'testOpenerMarker' => null, 'testCloserMarker' => '/* testSwitchNormalSyntaxScopeCloser */', ], - 'switch alternative syntax' => [ + 'switch alternative syntax' => [ 'testMarker' => '/* testSwitchAlternativeSyntax */', 'expectedTokens' => [ 'scope_opener' => T_COLON, @@ -115,7 +115,7 @@ public static function dataSwitchScope() 'testOpenerMarker' => null, 'testCloserMarker' => '/* testSwitchAlternativeSyntaxScopeCloser */', ], - 'switch with closure in the condition' => [ + 'switch with closure in the condition' => [ 'testMarker' => '/* testSwitchClosureWithinCondition */', 'expectedTokens' => [ 'scope_opener' => T_OPEN_CURLY_BRACKET, @@ -124,6 +124,34 @@ public static function dataSwitchScope() 'testOpenerMarker' => '/* testSwitchClosureWithinConditionScopeOpener */', 'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeCloser */', ], + 'switch alternative syntax with closure containing return type in the condition' => [ + 'testMarker' => '/* testSwitchClosureWithReturnTypeWithinCondition */', + 'expectedTokens' => [ + 'scope_opener' => T_COLON, + 'scope_closer' => T_ENDSWITCH, + ], + 'testOpenerMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeOpener */', + 'testCloserMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeCloser */', + ], + 'switch with arrow function in the condition' => [ + 'testMarker' => '/* testSwitchArrowFunctionWithinCondition */', + 'expectedTokens' => [ + 'scope_opener' => T_OPEN_CURLY_BRACKET, + 'scope_closer' => T_CLOSE_CURLY_BRACKET, + ], + 'testOpenerMarker' => '/* testSwitchArrowFunctionWithinConditionScopeOpener */', + 'testCloserMarker' => '/* testSwitchArrowFunctionWithinConditionScopeCloser */', + ], + 'switch alternative syntax with arrow function containing return type in the condition' => [ + 'testMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinCondition */', + 'expectedTokens' => [ + 'scope_opener' => T_COLON, + 'scope_closer' => T_ENDSWITCH, + ], + 'testOpenerMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeOpener */', + 'testCloserMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeCloser */', + ], + ]; }//end dataSwitchScope()