Skip to content
Permalink
Browse files

Fix NodeExpressionResolver to reflect system classes (#50)

Use `\ReflectionClass` for system classes as they have no file to parse, resolves #48
  • Loading branch information...
pdelre authored and lisachenko committed Dec 7, 2016
1 parent b573575 commit 0d5b500615c7a92dc45d7702e50a86a56008a87f
@@ -45,7 +45,7 @@ class NodeExpressionResolver
/**
* Current reflection context for parsing
*
* @var mixed
* @var mixed|\Go\ParserReflection\ReflectionClass
*/
private $context;
@@ -444,6 +444,14 @@ private function fetchReflectionClass(Node\Name $node)
$className = $node->toString();
$isFQNClass = $node instanceof Node\Name\FullyQualified;
if ($isFQNClass) {
// check to see if the class is already loaded and is safe to use
// PHP's ReflectionClass to determine if the class is user defined
if (class_exists($className, false)) {
$refClass = new \ReflectionClass($className);
if (!$refClass->isUserDefined()) {
return $refClass;
}
}
return new ReflectionClass($className);
}
@@ -127,7 +127,7 @@ public function testGetDeclaringClassMethodReturnsObject()
$parameters = $parsedFunction->getParameters();
$this->assertSame($parsedClass->getName(), $parameters[0]->getDeclaringClass()->getName());
}
public function testParamWithDefaultConstValue()
{
$parsedNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
@@ -137,14 +137,25 @@ public function testParamWithDefaultConstValue()
$parameters = $parsedFunction->getParameters();
$this->assertTrue($parameters[0]->isDefaultValueConstant());
$this->assertSame('self::CLASS_CONST', $parameters[0]->getDefaultValueConstantName());
$this->assertTrue($parameters[2]->isDefaultValueConstant());
$this->assertSame('Go\ParserReflection\Stub\TEST_PARAMETER', $parameters[2]->getDefaultValueConstantName());
$this->assertTrue($parameters[3]->isDefaultValueConstant());
$this->assertSame('Go\ParserReflection\Stub\SubFoo::ANOTHER_CLASS_CONST', $parameters[3]->getDefaultValueConstantName());
}
public function testParamBuiltInClassConst()
{
$parsedNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
$parsedClass = $parsedNamespace->getClass(Foo::class);
$parsedFunction = $parsedClass->getMethod('methodParamBuiltInClassConst');
$parameters = $parsedFunction->getParameters();
$this->assertTrue($parameters[0]->isDefaultValueConstant());
$this->assertSame('DateTime::ATOM', $parameters[0]->getDefaultValueConstantName());
}
public function testGetDeclaringClassMethodReturnsNull()
{
$parsedNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
@@ -14,6 +14,7 @@ class ClassWithComplexConstantsAndInheritance extends ClassWithArrayConstants
{
const K = array(1, NS_CONST56);
const L = [self::class, ClassWithArrayConstants::A, parent::B];
const M = \DateTime::ATOM;
}
const ONE = 1;
@@ -55,12 +55,16 @@ public function methodParam($firstParam, $optionalParam = null)
public function methodParamConst($firstParam = self::CLASS_CONST, $another = __CLASS__, $ns = TEST_PARAMETER, $someOther = SubFoo::ANOTHER_CLASS_CONST)
{
}
public function methodParamBuiltInClassConst($firstParam = \DateTime::ATOM)
{
}
}
class SubFoo extends Foo
{
const ANOTHER_CLASS_CONST = __CLASS__;
public function anotherMethodParam(self $selfParam, parent $parentParam)
{
}

0 comments on commit 0d5b500

Please sign in to comment.
You can’t perform that action at this time.