Skip to content

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

Open
@rodrigoprimo

Description

@rodrigoprimo

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()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions