Skip to content

Commit

Permalink
Merge pull request #305 from phpDocumentor/fix/parse-constants-in-traits
Browse files Browse the repository at this point in the history
Constants in traits cannot be parsed
  • Loading branch information
jaapio committed Dec 6, 2022
2 parents df66c29 + f3a2ded commit 5478413
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/phpDocumentor/Reflection/Php/Factory/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpDocumentor\Reflection\Php\Enum_;
use phpDocumentor\Reflection\Php\Interface_;
use phpDocumentor\Reflection\Php\StrategyContainer;
use phpDocumentor\Reflection\Php\Trait_;
use phpDocumentor\Reflection\Php\Visibility;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@

use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Element;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
use phpDocumentor\Reflection\Php\Constant as ConstantDescriptor;
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
use phpDocumentor\Reflection\Php\Interface_ as InterfaceElement;
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
use phpDocumentor\Reflection\Php\Trait_ as TraitElement;
use phpDocumentor\Reflection\Types\Null_;
use PhpParser\Comment\Doc;
use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -97,6 +102,42 @@ public function visibilityProvider(): array
];
}

public function testCreateForInterface(): void
{
$interface = new InterfaceElement(new Fqsen('\myInterface'));
$const = new Const_('\Space\MyInterface::MY_CONST1', new String_('a'));
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);

$result = $this->performCreateWith($constantStub, $interface);

self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
}

public function testCreateForTrait(): void
{
$trait = new TraitElement(new Fqsen('\myTrait'));
$const = new Const_('\Space\MyTrait::MY_CONST1', new String_('a'));
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);

$result = $this->performCreateWith($constantStub, $trait);

self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
}

public function testCreateForEnum(): void
{
$enum = new EnumElement(new Fqsen('\myEnum'), new Null_());
$const = new Const_('\Space\MyEnum::MY_CONST1', new String_('a'));
$const->setAttribute('fqsen', new Fqsen((string) $const->name));
$constantStub = new ClassConst([$const], ClassNode::MODIFIER_PUBLIC);

$result = $this->performCreateWith($constantStub, $enum);

self::assertInstanceOf(ConstantDescriptor::class, current($result->getConstants()));
}

public function testCreateWithDocBlock(): void
{
$doc = new Doc('text');
Expand Down Expand Up @@ -132,10 +173,17 @@ private function assertConstant(ConstantDescriptor $constant, string $visibility

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

return $class;
}

private function performCreateWith(ClassConst $constantStub, Element $parent): Element
{
$factory = new ProjectFactoryStrategies([]);
$this->fixture->create(self::createContext(null)->push($parent), $constantStub, $factory);

return $parent;
}
}

0 comments on commit 5478413

Please sign in to comment.