Skip to content

Commit

Permalink
Fix missing properties for UnitEnum and BackedEnum interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 18, 2022
1 parent 23ee59d commit d097ba3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/Reflection/ReflectionClass.php
Expand Up @@ -804,11 +804,7 @@ public function getImmediateProperties(?int $filter = null): array
*/
private function addEnumProperties(array $properties): array
{
if (! $this->node instanceof EnumNode) {
return $properties;
}

$createProperty = function (string $name, string|Node\Identifier $type): ReflectionProperty {
$createProperty = function (string $name, string|Node\Identifier|Node\UnionType $type): ReflectionProperty {
$propertyNode = new Node\Stmt\Property(
ClassNode::MODIFIER_PUBLIC | ClassNode::MODIFIER_READONLY,
[new Node\Stmt\PropertyProperty($name)],
Expand All @@ -826,6 +822,24 @@ private function addEnumProperties(array $properties): array
);
};

if (! $this->node instanceof EnumNode) {
if ($this->node instanceof Node\Stmt\Interface_) {
$interfaceName = $this->getName();
if ($interfaceName === 'UnitEnum') {
$properties['name'] = $createProperty('name', 'string');
}

if ($interfaceName === 'BackedEnum') {
$properties['value'] = $createProperty('value', new Node\UnionType([
new Node\Identifier('int'),
new Node\Identifier('string'),
]));
}
}

return $properties;
}

$properties['name'] = $createProperty('name', 'string');

if ($this->node->scalarType !== null) {
Expand Down Expand Up @@ -863,7 +877,7 @@ static function (ReflectionClass $ancestor) use ($filter): array {
static fn (ReflectionProperty $property): bool => ! $property->isPrivate(),
);
},
array_filter([$this->getParentClass()]),
array_merge(array_filter([$this->getParentClass()]), array_values($this->getInterfaces())),
),
...array_map(
function (ReflectionClass $trait) use ($filter) {
Expand Down
10 changes: 9 additions & 1 deletion test/unit/Reflection/ReflectionClassTest.php
Expand Up @@ -580,6 +580,14 @@ public function dataGetPropertiesForBackedEnum(): array
IntEnum::class,
['name' => 'string', 'value' => 'int'],
],
[
UnitEnum::class,
['name' => 'string'],
],
[
BackedEnum::class,
['name' => 'string', 'value' => 'int|string'],
],
];
}

Expand All @@ -596,7 +604,7 @@ public function testGetPropertiesForBackedEnum(string $className, array $propert
]));

$classInfo = $reflector->reflectClass($className);
$properties = $classInfo->getImmediateProperties();
$properties = $classInfo->getProperties();

foreach ($propertiesData as $propertyName => $propertyType) {
$fullPropertyName = sprintf('%s::$%s', $className, $propertyName);
Expand Down

0 comments on commit d097ba3

Please sign in to comment.