Skip to content

Commit 50ffd77

Browse files
authored
Merge pull request #157 from dirx/issue-156
Fix namespace handling for defines and namespaced constants
2 parents 5e289b3 + 7140fe3 commit 50ffd77

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
2727
use RuntimeException;
2828
use function sprintf;
29+
use function strpos;
2930

3031
/**
3132
* Strategy to convert `define` expressions to ConstantElement
@@ -93,7 +94,7 @@ protected function doCreate($object, StrategyContainer $strategies, ?Context $co
9394
[$name, $value] = $expression->args;
9495

9596
return new ConstantElement(
96-
$this->determineFqsen($context, $name),
97+
$this->determineFqsen($name),
9798
$this->createDocBlock($strategies, $object->getDocComment(), $context),
9899
$this->determineValue($value),
99100
new Location($object->getLine())
@@ -109,16 +110,15 @@ private function determineValue(?Arg $value) : ?string
109110
return $this->valueConverter->prettyPrintExpr($value->value);
110111
}
111112

112-
private function determineFqsen(?Context $context, Arg $name) : Fqsen
113+
private function determineFqsen(Arg $name) : Fqsen
113114
{
114115
/** @var String_ $nameString */
115116
$nameString = $name->value;
116-
$namespace = $context ? $context->getNamespace() : '';
117117

118-
if (empty($namespace)) {
118+
if (strpos($nameString->value, '\\') === false) {
119119
return new Fqsen(sprintf('\\%s', $nameString->value));
120120
}
121121

122-
return new Fqsen(sprintf('\\%s\\%s', $namespace, $nameString->value));
122+
return new Fqsen(sprintf('%s', $nameString->value));
123123
}
124124
}

src/phpDocumentor/Reflection/Php/ProjectFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ private function buildNamespace(File $file, Namespace_ $namespace) : void
148148
}
149149

150150
foreach ($file->getConstants() as $constant) {
151-
if ($namespace->getFqsen() . '::' . $constant->getName() !== (string) $constant->getFqsen()) {
151+
if ($namespace->getFqsen() . '::' . $constant->getName() !== (string) $constant->getFqsen() &&
152+
$namespace->getFqsen() . '\\' . $constant->getName() !== (string) $constant->getFqsen()) {
152153
continue;
153154
}
154155

tests/integration/ProjectCreationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public function testWithGlobalConstants() : void
205205

206206
$this->assertArrayHasKey('\\Luigi\\OVEN_TEMPERATURE', $project->getFiles()[$fileName]->getConstants());
207207
$this->assertArrayHasKey('\\Luigi\\MAX_OVEN_TEMPERATURE', $project->getFiles()[$fileName]->getConstants());
208+
$this->assertArrayHasKey('\\OUTSIDE_OVEN_TEMPERATURE', $project->getFiles()[$fileName]->getConstants());
208209
}
209210

210211
public function testInterfaceExtends() : void

tests/integration/ProjectNamespaceTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,23 @@ public function testWithNamespacedClass() : void
4949
$project->getNamespaces()['\\Luigi']->getClasses()
5050
);
5151
}
52+
53+
public function testWithNamespacedConstant() : void
54+
{
55+
$fileName = __DIR__ . '/data/Luigi/constants.php';
56+
$project = $this->fixture->create(
57+
'My Project',
58+
[ new LocalFile($fileName) ]
59+
);
60+
61+
$this->assertArrayHasKey($fileName, $project->getFiles());
62+
$this->assertArrayHasKey('\\Luigi', $project->getNamespaces());
63+
$this->assertEquals(
64+
[
65+
'\\Luigi\\OVEN_TEMPERATURE' => new Fqsen('\\Luigi\\OVEN_TEMPERATURE'),
66+
'\\Luigi\\MAX_OVEN_TEMPERATURE' => new Fqsen('\\Luigi\\MAX_OVEN_TEMPERATURE'),
67+
],
68+
$project->getNamespaces()['\\Luigi']->getConstants()
69+
);
70+
}
5271
}

tests/integration/data/Luigi/constants.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
namespace Luigi;
44

55
const OVEN_TEMPERATURE = 9001;
6-
define('MAX_OVEN_TEMPERATURE', 9002);
6+
define('\\Luigi\\MAX_OVEN_TEMPERATURE', 9002);
7+
define('OUTSIDE_OVEN_TEMPERATURE', 9002);

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,24 @@ public function testCreate() : void
6464
$constant = $this->fixture->create(
6565
$constantStub,
6666
new ProjectFactoryStrategies([]),
67-
new Context('Space\MyClass')
67+
new Context('Space\\MyClass')
6868
);
6969

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

7387
public function testCreateGlobal() : void
@@ -99,7 +113,7 @@ public function testCreateWithDocBlock() : void
99113
),
100114
['comments' => [$doc]]
101115
);
102-
$context = new Context('Space\MyClass');
116+
$context = new Context('Space\\MyClass');
103117

104118
$strategyMock = m::mock(ProjectFactoryStrategy::class);
105119
$containerMock = m::mock(StrategyContainer::class);
@@ -119,17 +133,17 @@ public function testCreateWithDocBlock() : void
119133
$context
120134
);
121135

122-
$this->assertConstant($constant, '\\Space\\MyClass');
136+
$this->assertConstant($constant, '');
123137
$this->assertSame($docBlock, $constant->getDocBlock());
124138
}
125139

126-
private function buildDefineStub() : Expression
140+
private function buildDefineStub($namespace = '') : Expression
127141
{
128142
return new Expression(
129143
new FuncCall(
130144
new Name('define'),
131145
[
132-
new Arg(new String_('MY_CONST1')),
146+
new Arg(new String_($namespace ? $namespace . '\\MY_CONST1' : 'MY_CONST1')),
133147
new Arg(new String_('a')),
134148
]
135149
)

0 commit comments

Comments
 (0)