Просмотр исходного кода

bug #3193 TokensAnalyzer::getClassyElements - sort result before returning (SpacePossum)

This PR was merged into the 2.2 branch.

Discussion
----------

TokensAnalyzer::getClassyElements - sort result before returning

Commits
-------

6154982f TokensAnalyzer::getClassyElements - sort result before returning.
Dariusz Ruminski 7 лет назад
Родитель
Сommit
95150fc237
2 измененных файлов с 75 добавлено и 0 удалено
  1. 2 0
      src/Tokenizer/TokensAnalyzer.php
  2. 73 0
      tests/Tokenizer/TokensAnalyzerTest.php

+ 2 - 0
src/Tokenizer/TokensAnalyzer.php

@@ -600,6 +600,8 @@ final class TokensAnalyzer
             }
         }
 
+        ksort($elements);
+
         return $elements;
     }
 }

+ 73 - 0
tests/Tokenizer/TokensAnalyzerTest.php

@@ -20,6 +20,7 @@ use PHPUnit\Framework\TestCase;
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  * @author Max Voloshin <voloshin.dp@gmail.com>
  * @author Gregor Harlan <gharlan@web.de>
+ * @author SpacePossum
  *
  * @internal
  *
@@ -169,6 +170,78 @@ PHP;
         );
     }
 
+    public function testGetClassyElementsWithMultipleAnonymousClass()
+    {
+        $source = <<<'PHP'
+<?php class A0
+{
+    public function AA0()
+    {
+        return new class
+        {
+            public function BB0()
+            {
+            }
+        };
+    }
+
+    public function otherFunction0()
+    {
+    }
+}
+
+class A1
+{
+    public function AA1()
+    {
+        return new class
+        {
+            public function BB1()
+            {
+            }
+        };
+    }
+
+    public function otherFunction1()
+    {
+    }
+}
+PHP;
+        $tokens = Tokens::fromCode($source);
+        $tokensAnalyzer = new TokensAnalyzer($tokens);
+        $elements = $tokensAnalyzer->getClassyElements();
+
+        $this->assertSame(
+            array(
+                9 => array(
+                    'token' => $tokens[9],
+                    'type' => 'method',
+                ),
+                27 => array(
+                    'token' => $tokens[27],
+                    'type' => 'method',
+                ),
+                44 => array(
+                    'token' => $tokens[44],
+                    'type' => 'method',
+                ),
+                64 => array(
+                    'token' => $tokens[64],
+                    'type' => 'method',
+                ),
+                82 => array(
+                    'token' => $tokens[82],
+                    'type' => 'method',
+                ),
+                99 => array(
+                    'token' => $tokens[99],
+                    'type' => 'method',
+                ),
+            ),
+            $elements
+        );
+    }
+
     /**
      * @param string $source
      *