From 4d9d2376332343c26a1292b03c99ba0f6eaa5e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 1 Mar 2016 14:11:25 +0100 Subject: [PATCH 1/2] Fix file_get_contents warning in ContextFactory --- src/Types/ContextFactory.php | 6 ++++++ tests/unit/Types/ContextFactoryTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Types/ContextFactory.php b/src/Types/ContextFactory.php index fc3f384..c904d9d 100644 --- a/src/Types/ContextFactory.php +++ b/src/Types/ContextFactory.php @@ -37,13 +37,19 @@ final class ContextFactory * @see Context for more information on Contexts. * * @return Context + * + * @throws \InvalidArgumentException */ public function createFromReflector(\Reflector $reflector) { if (method_exists($reflector, 'getDeclaringClass')) { $reflector = $reflector->getDeclaringClass(); } + $fileName = $reflector->getFileName(); + if (!$fileName) { + throw new \InvalidArgumentException('There is no file name associated with this reflector.'); + } return $this->createForNamespace($reflector->getNamespaceName(), file_get_contents($fileName)); } diff --git a/tests/unit/Types/ContextFactoryTest.php b/tests/unit/Types/ContextFactoryTest.php index 4a6aece..327acbe 100644 --- a/tests/unit/Types/ContextFactoryTest.php +++ b/tests/unit/Types/ContextFactoryTest.php @@ -149,6 +149,16 @@ public function bar() $this->assertSame([], $context->getNamespaceAliases()); } + + /** + * @expectedException \InvalidArgumentException + * @covers ::createFromReflector + */ + public function testThrowExceptionWhenEmptyFileName() + { + $fixture = new ContextFactory(); + $fixture->createFromReflector(new \ReflectionClass('stdClass')); + } } } From 17156bc7e90f0257e8ea8725b98711a504bb2d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 1 Mar 2016 16:45:11 +0100 Subject: [PATCH 2/2] Return an empty context when no file related to the reflector --- src/Types/ContextFactory.php | 10 +++++----- tests/unit/Types/ContextFactoryTest.php | 26 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Types/ContextFactory.php b/src/Types/ContextFactory.php index c904d9d..147df69 100644 --- a/src/Types/ContextFactory.php +++ b/src/Types/ContextFactory.php @@ -37,8 +37,6 @@ final class ContextFactory * @see Context for more information on Contexts. * * @return Context - * - * @throws \InvalidArgumentException */ public function createFromReflector(\Reflector $reflector) { @@ -47,11 +45,13 @@ public function createFromReflector(\Reflector $reflector) } $fileName = $reflector->getFileName(); - if (!$fileName) { - throw new \InvalidArgumentException('There is no file name associated with this reflector.'); + $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 327acbe..20d63c9 100644 --- a/tests/unit/Types/ContextFactoryTest.php +++ b/tests/unit/Types/ContextFactoryTest.php @@ -151,13 +151,33 @@ public function bar() } /** - * @expectedException \InvalidArgumentException * @covers ::createFromReflector */ - public function testThrowExceptionWhenEmptyFileName() + public function testEmptyFileName() { $fixture = new ContextFactory(); - $fixture->createFromReflector(new \ReflectionClass('stdClass')); + $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()); } } }