Skip to content

Commit da7e043

Browse files
committed
[PHP7] type resolver supports anonymous classes and is improved
1 parent b86ec3c commit da7e043

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/Hal/Component/OOP/Extractor/ClassExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function extract(&$n, TokenCollection $tokens)
5151
$previous = $tokens->get($n - 2);
5252
if($previous && T_NEW === $previous->getType()) {
5353
// anonymous class
54-
$class = new ReflectedAnonymousClass($this->namespace, trim('class@anonymous'));
54+
$class = new ReflectedAnonymousClass($this->namespace, 'class@anonymous');
5555
return $class;
5656
}
5757

src/Hal/Component/OOP/Resolver/TypeResolver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TypeResolver
2626
const TYPE_NULL = 'null';
2727
const TYPE_UNKNWON = 'unknown';
2828
const TYPE_FLUENT_INTERFACE = 'fluent';
29+
const TYPE_ANONYMOUS_CLASS = 'anonymous@class';
2930

3031
/**
3132
* Resolves type of given string
@@ -58,12 +59,12 @@ public function resolve($string)
5859
return self::TYPE_ARRAY;
5960
}
6061

61-
if(preg_match('!(^\[|^array\()!', $string)) {
62-
return self::TYPE_ARRAY;
62+
if(preg_match('!^new\s+class\s+!', $string, $matches)) {
63+
return self::TYPE_ANONYMOUS_CLASS;
6364
}
6465

65-
if(preg_match('!^new\s*(.*)!', $string, $matches)) {
66-
return trim(preg_replace('!^(new\s+)!', '', $cased));
66+
if(preg_match('!^(new\s+)(.*?)([\(;].*|$)!', $cased, $matches)) {
67+
return $matches[2];
6768
}
6869

6970
if(preg_match('!^\$this$!', $string, $matches)) {

tests/Hal/Component/OOP/Resolver/TypeResolverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function providesTypes()
3232
array('', TypeResolver::TYPE_VOID),
3333
array('$this->foo();', TypeResolver::TYPE_UNKNWON),
3434
array('new \StdClass', '\StdClass'),
35+
array('new \StdClass($a, $b)', '\StdClass'),
36+
array('new class implements Countable, Iterator {}', 'anonymous@class')
3537
);
3638
}
3739
}

0 commit comments

Comments
 (0)