Skip to content

Commit 2019338

Browse files
authored
Merge pull request #151 from othercorey/global-defines
Fixed FQSEN generation for defines in global namespace
2 parents 8402c36 + 6a9ab9f commit 2019338

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/phpDocumentor/Reflection/Php/Factory/Define.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ private function determineFqsen(?Context $context, Arg $name) : Fqsen
115115
$nameString = $name->value;
116116
$namespace = $context ? $context->getNamespace() : '';
117117

118+
if (empty($namespace)) {
119+
return new Fqsen(sprintf('\\%s', $nameString->value));
120+
}
121+
118122
return new Fqsen(sprintf('\\%s\\%s', $namespace, $nameString->value));
119123
}
120124
}

tests/integration/ClassesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function testItHasAllConstants() : void
6060
$constant = $class->getConstants()[$constantName];
6161

6262
$this->assertInstanceOf(Constant::class, $constant);
63+
64+
$this->assertArrayHasKey('\\OVEN_TEMPERATURE', $file->getConstants());
65+
$this->assertArrayHasKey('\\MAX_OVEN_TEMPERATURE', $file->getConstants());
6366
}
6467

6568
public function testTypedPropertiesReturnTheirType() : void

tests/integration/data/Pizza.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
* @link http://phpdoc.org
1111
*/
1212

13+
/**
14+
* This needs a docblock to separate from
15+
* file docblock
16+
*/
17+
const OVEN_TEMPERATURE = 9001;
18+
define('MAX_OVEN_TEMPERATURE', 9002);
1319

1420
/**
1521
* Pizza base class

tests/unit/phpDocumentor/Reflection/Php/Factory/DefineTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ public function testCreate() : void
6767
new Context('Space\MyClass')
6868
);
6969

70-
$this->assertConstant($constant);
70+
$this->assertConstant($constant, '\\Space\\MyClass');
71+
}
72+
73+
public function testCreateGlobal() : void
74+
{
75+
$constantStub = $this->buildDefineStub();
76+
77+
/** @var ConstantDescriptor $constant */
78+
$constant = $this->fixture->create(
79+
$constantStub,
80+
new ProjectFactoryStrategies([]),
81+
new Context('')
82+
);
83+
84+
$this->assertConstant($constant, '');
7185
}
7286

7387
public function testCreateWithDocBlock() : void
@@ -105,7 +119,7 @@ public function testCreateWithDocBlock() : void
105119
$context
106120
);
107121

108-
$this->assertConstant($constant);
122+
$this->assertConstant($constant, '\\Space\\MyClass');
109123
$this->assertSame($docBlock, $constant->getDocBlock());
110124
}
111125

@@ -122,10 +136,10 @@ private function buildDefineStub() : Expression
122136
);
123137
}
124138

125-
private function assertConstant(ConstantDescriptor $constant) : void
139+
private function assertConstant(ConstantDescriptor $constant, string $namespace) : void
126140
{
127141
$this->assertInstanceOf(ConstantDescriptor::class, $constant);
128-
$this->assertEquals('\Space\MyClass\MY_CONST1', (string) $constant->getFqsen());
142+
$this->assertEquals($namespace . '\\MY_CONST1', (string) $constant->getFqsen());
129143
$this->assertEquals('\'a\'', $constant->getValue());
130144
$this->assertEquals('public', (string) $constant->getVisibility());
131145
}

0 commit comments

Comments
 (0)