Skip to content

Commit d93e019

Browse files
committed
Fix issue with non-named namespace
php supports a namespace reset using. ``` namespace { } ``` The odd thing is that a namespace doesn't always have a name like we expected before. To be able to process this I forced skipping the creation of fqsen. And create it when the real object is created. phpDocumentor/phpDocumentor#2967
1 parent 9ed1b6b commit d93e019

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

src/phpDocumentor/Reflection/NodeVisitor/ElementNameResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function enterNode(Node $node) : ?int
8787
{
8888
switch (get_class($node)) {
8989
case Namespace_::class:
90+
if ($node->name === null) {
91+
break;
92+
}
93+
9094
$this->resetState('\\' . $node->name . '\\');
9195
$node->fqsen = new Fqsen($this->buildName());
9296
break;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace phpDocumentor\Reflection\Php\Factory;
66

77
use InvalidArgumentException;
8+
use phpDocumentor\Reflection\Fqsen;
89
use phpDocumentor\Reflection\Php\File as FileElement;
910
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1011
use phpDocumentor\Reflection\Php\StrategyContainer;
@@ -40,7 +41,7 @@ public function create(ContextStack $context, object $object, StrategyContainer
4041

4142
$file = $context->peek();
4243
Assert::isInstanceOf($file, FileElement::class);
43-
$file->addNamespace($object->fqsen);
44+
$file->addNamespace($object->fqsen ?? new Fqsen('\\'));
4445
$typeContext = (new NamespaceNodeToContext())($object);
4546
foreach ($object->stmts as $stmt) {
4647
$strategy = $strategies->findMatching($context, $stmt);

tests/integration/FileDocblockTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,22 @@ public function testConditionalFunctionDefine() : void
6363
$project->getFiles()[$fileName]->getFunctions()
6464
);
6565
}
66+
67+
/**
68+
* @covers \phpDocumentor\Reflection\Php\Factory\File::create
69+
* @covers \phpDocumentor\Reflection\Php\Factory\File::<private>
70+
*/
71+
public function testGlobalNamespacedFunctionDefine() : void
72+
{
73+
$fileName = __DIR__ . '/data/GlobalFiles/global_namspaced_function.php';
74+
$project = $this->fixture->create(
75+
'MyProject',
76+
[new LocalFile($fileName)]
77+
);
78+
79+
$this->assertCount(
80+
1,
81+
$project->getFiles()[$fileName]->getFunctions()
82+
);
83+
}
6684
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* setup.php - Document the file.
4+
*
5+
* File long description goes here.
6+
*
7+
* @author Test Sample
8+
*/
9+
10+
namespace {
11+
/**
12+
* autoloader function.
13+
*
14+
* Function long description goes here.
15+
*
16+
* @param string $class Namespaced class name
17+
* @return void
18+
*/
19+
function libAutoload($class){
20+
echo 'Do something';
21+
}
22+
spl_autoload_register('libAutoload');
23+
}

tests/unit/phpDocumentor/Reflection/NodeVisitor/ElementNameResolverTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,18 @@ public function testNamespacedConstant() : void
161161

162162
$this->assertEquals('\\name\\MY_CLASS', (string) $const->fqsen);
163163
}
164+
165+
/**
166+
* @covers ::enterNode
167+
*/
168+
public function testNoNameNamespace() : void
169+
{
170+
$const = new Const_('MY_CLASS', new String_('value'));
171+
$namespace = new Namespace_(null);
172+
173+
$this->fixture->enterNode($namespace);
174+
$this->fixture->enterNode($const);
175+
176+
$this->assertEquals('\\MY_CLASS', (string) $const->fqsen);
177+
}
164178
}

0 commit comments

Comments
 (0)