Skip to content

Unexpected scope_condition for scope_closer of a default case in a switch statement #810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rodrigoprimo opened this issue Feb 11, 2025 · 0 comments

Comments

@rodrigoprimo
Copy link
Contributor

rodrigoprimo commented Feb 11, 2025

While working on adding more tests to tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php, I noticed a behavior in how scope_condition is set that I thought is worth highlighting to check if something should be changed or not.

In the code below, the switch and default keywords share endswitch as their scope closer.

switch ($i):
    default:
        echo 'one';
endswitch;

The test RecurseScopeMapDefaultKeywordConditionsTest::testSwitchDefault() expects that the value of the scope_condition of the scope_closer token of the default keyword (endswitch) points to the token that represents default:

$this->assertSame($token, $tokens[$closer]['scope_condition'], 'Closer scope condition is not the T_DEFAULT token');

That expectation is not valid for the code sample above. The scope_condition of the token that represents the endswitch keyword contains the index of the switch token and not the default token. I don't know enough about the tokenizer to know if this is a problem or not.

The following patch can be used to add a new failing test with the code sample above that illustrates the situation described in this issue:

Patch
diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc
index 648149d2f..337ad300d 100644
--- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc
+++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc
@@ -201,3 +201,11 @@ class Foo {
     /* testMethodDeclaration */
     public function default() {}
 }
+
+function switchAndDefaultSharingScopeCloser($i) {
+    switch ($i):
+        /* testSwitchAndDefaultSharingScopeCloser */
+        default:
+            echo 'one';
+    endswitch;
+}
diff --git a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php
index 064ce8f40..ca0fdf61c 100644
--- a/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php
+++ b/tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php
@@ -213,6 +213,11 @@ final class RecurseScopeMapDefaultKeywordConditionsTest extends AbstractTokenize
                 'openerOffset' => 1,
                 'closerOffset' => 18,
             ],
+            'switch_and_default_sharing_scope_closer' => [
+                'testMarker'   => '/* testSwitchAndDefaultSharingScopeCloser */',
+                'openerOffset' => 1,
+                'closerOffset' => 10,
+            ],
         ];
 
     }//end dataSwitchDefault()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant