diff --git a/src/Types/ContextFactory.php b/src/Types/ContextFactory.php index fc3f384..147df69 100644 --- a/src/Types/ContextFactory.php +++ b/src/Types/ContextFactory.php @@ -43,9 +43,15 @@ public function createFromReflector(\Reflector $reflector) if (method_exists($reflector, 'getDeclaringClass')) { $reflector = $reflector->getDeclaringClass(); } + $fileName = $reflector->getFileName(); + $namespace = $reflector->getNamespaceName(); + + if (file_exists($fileName)) { + return $this->createForNamespace($namespace, file_get_contents($fileName)); + } - return $this->createForNamespace($reflector->getNamespaceName(), file_get_contents($fileName)); + return new Context($namespace, []); } /** diff --git a/tests/unit/Types/ContextFactoryTest.php b/tests/unit/Types/ContextFactoryTest.php index 4a6aece..20d63c9 100644 --- a/tests/unit/Types/ContextFactoryTest.php +++ b/tests/unit/Types/ContextFactoryTest.php @@ -149,6 +149,36 @@ public function bar() $this->assertSame([], $context->getNamespaceAliases()); } + + /** + * @covers ::createFromReflector + */ + public function testEmptyFileName() + { + $fixture = new ContextFactory(); + $context = $fixture->createFromReflector(new \ReflectionClass('stdClass')); + + $this->assertSame([], $context->getNamespaceAliases()); + } + + /** + * @covers ::createFromReflector + */ + public function testEvalDClass() + { + eval(<<createFromReflector(new \ReflectionClass('Foo\Bar')); + + $this->assertSame([], $context->getNamespaceAliases()); + } } }