From 6a9ab9f157a3d76294f5085a542b94d8fd2e9a47 Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Wed, 5 Feb 2020 20:26:20 -0600 Subject: [PATCH] Fixed FQSEN generation for defines in global namespace --- .../Reflection/Php/Factory/Define.php | 4 ++++ tests/integration/ClassesTest.php | 3 +++ tests/integration/data/Pizza.php | 6 +++++ .../Reflection/Php/Factory/DefineTest.php | 22 +++++++++++++++---- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/phpDocumentor/Reflection/Php/Factory/Define.php b/src/phpDocumentor/Reflection/Php/Factory/Define.php index 7dc8db4a..8b6ce28f 100644 --- a/src/phpDocumentor/Reflection/Php/Factory/Define.php +++ b/src/phpDocumentor/Reflection/Php/Factory/Define.php @@ -115,6 +115,10 @@ private function determineFqsen(?Context $context, Arg $name) : Fqsen $nameString = $name->value; $namespace = $context ? $context->getNamespace() : ''; + if (empty($namespace)) { + return new Fqsen(sprintf('\\%s', $nameString->value)); + } + return new Fqsen(sprintf('\\%s\\%s', $namespace, $nameString->value)); } } diff --git a/tests/integration/ClassesTest.php b/tests/integration/ClassesTest.php index a17a328a..ca74d3fe 100644 --- a/tests/integration/ClassesTest.php +++ b/tests/integration/ClassesTest.php @@ -60,6 +60,9 @@ public function testItHasAllConstants() : void $constant = $class->getConstants()[$constantName]; $this->assertInstanceOf(Constant::class, $constant); + + $this->assertArrayHasKey('\\OVEN_TEMPERATURE', $file->getConstants()); + $this->assertArrayHasKey('\\MAX_OVEN_TEMPERATURE', $file->getConstants()); } public function testTypedPropertiesReturnTheirType() : void diff --git a/tests/integration/data/Pizza.php b/tests/integration/data/Pizza.php index 78e7621d..05818e1e 100644 --- a/tests/integration/data/Pizza.php +++ b/tests/integration/data/Pizza.php @@ -10,6 +10,12 @@ * @link http://phpdoc.org */ +/** + * This needs a docblock to separate from + * file docblock + */ +const OVEN_TEMPERATURE = 9001; +define('MAX_OVEN_TEMPERATURE', 9002); /** * Pizza base class diff --git a/tests/unit/phpDocumentor/Reflection/Php/Factory/DefineTest.php b/tests/unit/phpDocumentor/Reflection/Php/Factory/DefineTest.php index 19fe2882..76755015 100644 --- a/tests/unit/phpDocumentor/Reflection/Php/Factory/DefineTest.php +++ b/tests/unit/phpDocumentor/Reflection/Php/Factory/DefineTest.php @@ -67,7 +67,21 @@ public function testCreate() : void new Context('Space\MyClass') ); - $this->assertConstant($constant); + $this->assertConstant($constant, '\\Space\\MyClass'); + } + + public function testCreateGlobal() : void + { + $constantStub = $this->buildDefineStub(); + + /** @var ConstantDescriptor $constant */ + $constant = $this->fixture->create( + $constantStub, + new ProjectFactoryStrategies([]), + new Context('') + ); + + $this->assertConstant($constant, ''); } public function testCreateWithDocBlock() : void @@ -105,7 +119,7 @@ public function testCreateWithDocBlock() : void $context ); - $this->assertConstant($constant); + $this->assertConstant($constant, '\\Space\\MyClass'); $this->assertSame($docBlock, $constant->getDocBlock()); } @@ -122,10 +136,10 @@ private function buildDefineStub() : Expression ); } - private function assertConstant(ConstantDescriptor $constant) : void + private function assertConstant(ConstantDescriptor $constant, string $namespace) : void { $this->assertInstanceOf(ConstantDescriptor::class, $constant); - $this->assertEquals('\Space\MyClass\MY_CONST1', (string) $constant->getFqsen()); + $this->assertEquals($namespace . '\\MY_CONST1', (string) $constant->getFqsen()); $this->assertEquals('\'a\'', $constant->getValue()); $this->assertEquals('public', (string) $constant->getVisibility()); }