Skip to content

Commit 5478413

Browse files
authored
Merge pull request #305 from phpDocumentor/fix/parse-constants-in-traits
Constants in traits cannot be parsed
2 parents df66c29 + f3a2ded commit 5478413

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpDocumentor\Reflection\Php\Enum_;
2121
use phpDocumentor\Reflection\Php\Interface_;
2222
use phpDocumentor\Reflection\Php\StrategyContainer;
23+
use phpDocumentor\Reflection\Php\Trait_;
2324
use phpDocumentor\Reflection\Php\Visibility;
2425
use PhpParser\Node\Stmt\ClassConst;
2526
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515

1616
use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor;
1717
use phpDocumentor\Reflection\DocBlockFactoryInterface;
18+
use phpDocumentor\Reflection\Element;
1819
use phpDocumentor\Reflection\Fqsen;
1920
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
2021
use phpDocumentor\Reflection\Php\Constant as ConstantDescriptor;
22+
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
23+
use phpDocumentor\Reflection\Php\Interface_ as InterfaceElement;
2124
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
25+
use phpDocumentor\Reflection\Php\Trait_ as TraitElement;
26+
use phpDocumentor\Reflection\Types\Null_;
2227
use PhpParser\Comment\Doc;
2328
use PhpParser\Node\Const_;
2429
use PhpParser\Node\Scalar\String_;
@@ -97,6 +102,42 @@ public function visibilityProvider(): array
97102
];
98103
}
99104

105+
public function testCreateForInterface(): void
106+
{
107+
$interface = new InterfaceElement(new Fqsen('\myInterface'));
108+
$const = new Const_('\Space\MyInterface::MY_CONST1', new String_('a'));
109+
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
110+
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);
111+
112+
$result = $this->performCreateWith($constantStub, $interface);
113+
114+
self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
115+
}
116+
117+
public function testCreateForTrait(): void
118+
{
119+
$trait = new TraitElement(new Fqsen('\myTrait'));
120+
$const = new Const_('\Space\MyTrait::MY_CONST1', new String_('a'));
121+
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
122+
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);
123+
124+
$result = $this->performCreateWith($constantStub, $trait);
125+
126+
self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
127+
}
128+
129+
public function testCreateForEnum(): void
130+
{
131+
$enum = new EnumElement(new Fqsen('\myEnum'), new Null_());
132+
$const = new Const_('\Space\MyEnum::MY_CONST1', new String_('a'));
133+
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
134+
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);
135+
136+
$result = $this->performCreateWith($constantStub, $enum);
137+
138+
self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
139+
}
140+
100141
public function testCreateWithDocBlock(): void
101142
{
102143
$doc = new Doc('text');
@@ -132,10 +173,17 @@ private function assertConstant(ConstantDescriptor $constant, string $visibility
132173

133174
private function performCreate(ClassConst $constantStub): ClassElement
134175
{
135-
$factory = new ProjectFactoryStrategies([]);
136176
$class = new ClassElement(new Fqsen('\myClass'));
137-
$this->fixture->create(self::createContext(null)->push($class), $constantStub, $factory);
177+
$this->performCreateWith($constantStub, $class);
138178

139179
return $class;
140180
}
181+
182+
private function performCreateWith(ClassConst $constantStub, Element $parent): Element
183+
{
184+
$factory = new ProjectFactoryStrategies([]);
185+
$this->fixture->create(self::createContext(null)->push($parent), $constantStub, $factory);
186+
187+
return $parent;
188+
}
141189
}

0 commit comments

Comments
 (0)