Skip to content

Commit

Permalink
ReflectionSourceStubber uses PhpParser builder factory for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 30, 2021
1 parent d929b98 commit 645ed92
Showing 1 changed file with 24 additions and 39 deletions.
63 changes: 24 additions & 39 deletions src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LogicException;
use PhpParser\Builder\Class_;
use PhpParser\Builder\ClassConst;
use PhpParser\Builder\Enum_;
use PhpParser\Builder\Function_;
use PhpParser\Builder\FunctionLike;
use PhpParser\Builder\Interface_;
Expand Down Expand Up @@ -97,29 +98,29 @@ class_exists($className, false)
$this->addClassModifiers($classNode, $classReflection);
}

if ($classNode instanceof Class_ || $classNode instanceof Interface_ || $classNode instanceof Node\Stmt\Enum_) {
if ($classNode instanceof Class_ || $classNode instanceof Interface_ || $classNode instanceof Enum_) {
$this->addExtendsAndImplements($classNode, $classReflection);
}

if ($classNode instanceof Class_ || $classNode instanceof Trait_) {
$this->addProperties($classNode, $classReflection);
}

if ($classNode instanceof Class_ || $classNode instanceof Trait_ || $classNode instanceof Node\Stmt\Enum_) {
if ($classNode instanceof Class_ || $classNode instanceof Trait_ || $classNode instanceof Enum_) {
$this->addTraitUse($classNode, $classReflection);
}

$this->addDocComment($classNode, $classReflection);

if ($classNode instanceof Node\Stmt\Enum_ && $classReflection instanceof CoreReflectionEnum) {
if ($classNode instanceof Enum_ && $classReflection instanceof CoreReflectionEnum) {
$this->addEnumBackingType($classNode, $classReflection);
$this->addEnumCases($classNode, $classReflection);
}

$this->addClassConstants($classNode, $classReflection);
$this->addMethods($classNode, $classReflection);

$node = $classNode instanceof Node\Stmt\Enum_ ? $classNode : $classNode->getNode();
$node = $classNode->getNode();
$stub = $classReflection->inNamespace()
? $this->generateStubInNamespace($node, $classReflection->getNamespaceName())
: $this->generateStub($node);
Expand Down Expand Up @@ -201,11 +202,10 @@ private function findConstantData(string $constantName): ?array
return null;
}

private function createClass(CoreReflectionClass $classReflection): Class_|Interface_|Trait_|Node\Stmt\Enum_
private function createClass(CoreReflectionClass $classReflection): Class_|Interface_|Trait_|Enum_
{
if ($classReflection instanceof CoreReflectionEnum) {
// https://github.com/nikic/PHP-Parser/pull/816
return new Node\Stmt\Enum_($classReflection->getShortName());
return $this->builderFactory->enum($classReflection->getShortName());
}

if ($classReflection->isTrait()) {
Expand All @@ -220,7 +220,7 @@ private function createClass(CoreReflectionClass $classReflection): Class_|Inter
}

private function addDocComment(
Class_|Interface_|Trait_|Node\Stmt\Enum_|Method|Property|Function_ $node,
Class_|Interface_|Trait_|Enum_|Method|Property|Function_ $node,
CoreReflectionClass|CoreReflectionMethod|CoreReflectionProperty|CoreReflectionFunction $reflection,
): void {
$docComment = $reflection->getDocComment() ?: '';
Expand Down Expand Up @@ -252,7 +252,7 @@ private function addDocComment(
$node->setDocComment(new Doc($docComment));
}

private function addEnumBackingType(Node\Stmt\Enum_ $enumNode, CoreReflectionEnum $enumReflection): void
private function addEnumBackingType(Enum_ $enumNode, CoreReflectionEnum $enumReflection): void
{
if (! $enumReflection->isBacked()) {
return;
Expand All @@ -261,7 +261,7 @@ private function addEnumBackingType(Node\Stmt\Enum_ $enumNode, CoreReflectionEnu
$backingType = $enumReflection->getBackingType();
assert($backingType instanceof CoreReflectionNamedType);

$enumNode->scalarType = new Node\Identifier($backingType->getName());
$enumNode->setScalarType($backingType->getName());
}

private function addClassModifiers(Class_ $classNode, CoreReflectionClass $classReflection): void
Expand All @@ -278,7 +278,7 @@ private function addClassModifiers(Class_ $classNode, CoreReflectionClass $class
$classNode->makeFinal();
}

private function addExtendsAndImplements(Class_|Interface_|Node\Stmt\Enum_ $classNode, CoreReflectionClass $classReflection): void
private function addExtendsAndImplements(Class_|Interface_|Enum_ $classNode, CoreReflectionClass $classReflection): void
{
$interfaces = $classReflection->getInterfaceNames();

Expand All @@ -300,15 +300,13 @@ private function addExtendsAndImplements(Class_|Interface_|Node\Stmt\Enum_ $clas

if ($classNode instanceof Interface_) {
$classNode->extend($interfaceNode);
} elseif ($classNode instanceof Node\Stmt\Enum_) {
$classNode->implements[] = $interfaceNode;
} else {
$classNode->implement($interfaceNode);
}
}
}

private function addTraitUse(Class_|Trait_|Node\Stmt\Enum_ $classNode, CoreReflectionClass $classReflection): void
private function addTraitUse(Class_|Trait_|Enum_ $classNode, CoreReflectionClass $classReflection): void
{
$traitAliases = $classReflection->getTraitAliases();
assert(is_array($traitAliases));
Expand All @@ -331,13 +329,7 @@ private function addTraitUse(Class_|Trait_|Node\Stmt\Enum_ $classNode, CoreRefle
$traitUse->with($traitUseAdaptations[$traitName]);
}

if ($classNode instanceof Node\Stmt\Enum_) {
$traitUseNode = $traitUse->getNode();
assert($traitUseNode instanceof Node\Stmt\TraitUse);
$classNode->stmts[] = $traitUseNode;
} else {
$classNode->addStmt($traitUse);
}
$classNode->addStmt($traitUse);
}
}

Expand Down Expand Up @@ -407,19 +399,20 @@ private function addPropertyModifiers(Property $propertyNode, CoreReflectionProp
}
}

private function addEnumCases(Node\Stmt\Enum_ $enumNode, CoreReflectionEnum $enumReflection): void
private function addEnumCases(Enum_ $enumNode, CoreReflectionEnum $enumReflection): void
{
foreach ($enumReflection->getCases() as $enumCaseReflection) {
$value = $enumReflection->isBacked() ? $this->builderFactory->val($enumCaseReflection->getBackingValue()) : null;
$enumCaseNode = $this->builderFactory->enumCase($enumCaseReflection->getName());

if ($enumReflection->isBacked()) {
$enumCaseNode->setValue($enumCaseReflection->getBackingValue());
}

$enumNode->stmts[] = new Node\Stmt\EnumCase(
$enumCaseReflection->getName(),
$value,
);
$enumNode->addStmt($enumCaseNode);
}
}

private function addClassConstants(Class_|Interface_|Trait_|Node\Stmt\Enum_ $classNode, CoreReflectionClass $classReflection): void
private function addClassConstants(Class_|Interface_|Trait_|Enum_ $classNode, CoreReflectionClass $classReflection): void
{
foreach ($classReflection->getReflectionConstants() as $constantReflection) {
if (method_exists($constantReflection, 'isEnumCase') && $constantReflection->isEnumCase()) {
Expand All @@ -438,11 +431,7 @@ private function addClassConstants(Class_|Interface_|Trait_|Node\Stmt\Enum_ $cla

$this->addClassConstantModifiers($classConstantNode, $constantReflection);

if ($classNode instanceof Node\Stmt\Enum_) {
$classNode->stmts[] = $classConstantNode->getNode();
} else {
$classNode->addStmt($classConstantNode);
}
$classNode->addStmt($classConstantNode);
}
}

Expand All @@ -461,7 +450,7 @@ private function addClassConstantModifiers(ClassConst $classConstantNode, CoreRe
}
}

private function addMethods(Class_|Interface_|Trait_|Node\Stmt\Enum_ $classNode, CoreReflectionClass $classReflection): void
private function addMethods(Class_|Interface_|Trait_|Enum_ $classNode, CoreReflectionClass $classReflection): void
{
foreach ($classReflection->getMethods() as $methodReflection) {
if (! $this->isMethodDeclaredInClass($methodReflection, $classReflection)) {
Expand All @@ -485,11 +474,7 @@ private function addMethods(Class_|Interface_|Trait_|Node\Stmt\Enum_ $classNode,
$methodNode->setReturnType($this->formatType($returnType));
}

if ($classNode instanceof Node\Stmt\Enum_) {
$classNode->stmts[] = $methodNode->getNode();
} else {
$classNode->addStmt($methodNode);
}
$classNode->addStmt($methodNode);
}
}

Expand Down

0 comments on commit 645ed92

Please sign in to comment.