Permalink
Browse files

Extend parser to handle PHP 7's anonymous classes

  • Loading branch information...
1 parent 3626f2e commit f61f61eea936a2e90ec1390279bb8b0422e0abd9 @theseer committed Dec 20, 2016
Showing with 36 additions and 5 deletions.
  1. +14 −5 src/Parser.php
  2. +13 −0 tests/ParserTest.php
  3. +3 −0 tests/_data/parser/anonymousclass.php
  4. +6 −0 tests/_data/parser/anonymousclass2.php
View
@@ -113,6 +113,7 @@ public function parse(SourceFile $source) {
$tokList = array_keys($this->methodMap);
for($t=0; $t<$tokenCount; $t++) {
$current = (array)$this->tokenArray[$t];
+
if ($current[0]==T_STRING && $current[1]=='trait' && T_TRAIT==-1) {
// PHP < 5.4 compat fix
$current[0] = T_TRAIT_53;
@@ -155,6 +156,7 @@ private function processClass($pos) {
$stackSize = count($stack);
$classname = $this->inNamespace != '' ? $this->inNamespace . '\\' : '';
$extends = '';
+ $classnameFound = false;
$extendsFound = false;
$implementsFound = false;
$implementsList = array();
@@ -168,6 +170,9 @@ private function processClass($pos) {
continue;
}
case T_STRING: {
+ if ($mode === 'classname') {
+ $classnameFound = true;
+ }
$$mode .= $tok[1];
continue;
}
@@ -209,12 +214,16 @@ private function processClass($pos) {
), ParserException::ParseError
);
}
- $classname = $this->registerUnit($classname, $stack[0][0]);
- $this->dependencies[$classname] = $implementsList;
- if ($extendsFound) {
- $this->dependencies[$classname][] = $this->resolveDependencyName($extends);
+ if ($classnameFound) {
+ $classname = $this->registerUnit($classname, $stack[0][0]);
+ $this->dependencies[$classname] = $implementsList;
+ if ($extendsFound) {
+ $this->dependencies[$classname][] = $this->resolveDependencyName($extends);
+ }
+ $this->inUnit = $classname;
+ } else {
+ $this->inUnit = $classname . uniqid('#Anonymous', true);
}
- $this->inUnit = $classname;
$this->classBracket = $this->bracketLevel + 1;
return $pos + $stackSize - 1;
}
View
@@ -511,6 +511,19 @@ public function testGroupUseSyntaxWithConstIsHandeled() {
$this->assertEquals($units, $rc->getUnits());
$this->assertEquals($dependencies, $rc->getDependenciesForUnit('some\\name\\space\\classd'));
}
+
+ public function testAnonymousClassGetsIgnored() {
+ $parser = new Parser();
+ $rc = $parser->parse(new SourceFile((__DIR__.'/_data/parser/anonymousclass.php')));
+ $this->assertEmpty($rc->getUnits());
+ }
+
+ public function testAnonymousClassImplementingInterfaceGetsIgnored() {
+ $parser = new Parser();
+ $rc = $parser->parse(new SourceFile((__DIR__.'/_data/parser/anonymousclass2.php')));
+ $this->assertEquals(array('foo'), $rc->getUnits());
+ }
+
}
}
@@ -0,0 +1,3 @@
+<?php
+
+$x = new class {};
@@ -0,0 +1,6 @@
+<?php
+
+interface foo {}
+
+$x = new class implements foo {};
+

0 comments on commit f61f61e

Please sign in to comment.