Skip to content

Commit

Permalink
Normalize enum value to ClassConstFetch
Browse files Browse the repository at this point in the history
Fixes #930
  • Loading branch information
ruudk authored and nikic committed Aug 11, 2024
1 parent 4d36e9c commit 8a21ec3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/PhpParser/Builder/ClassConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ClassConst implements PhpParser\Builder
* Creates a class constant builder
*
* @param string|Identifier $name Name
* @param Node\Expr|bool|null|int|float|string|array $value Value
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value Value
*/
public function __construct($name, $value) {
$this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
Expand All @@ -36,7 +36,7 @@ public function __construct($name, $value) {
* Add another constant to const group
*
* @param string|Identifier $name Name
* @param Node\Expr|bool|null|int|float|string|array $value Value
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value Value
*
* @return $this The builder instance (for fluid interface)
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/BuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function enumCase($name) : Builder\EnumCase {
/**
* Creates node a for a literal value.
*
* @param Expr|bool|null|int|float|string|array $value $value
* @param Expr|bool|null|int|float|string|array|\UnitEnum $value $value
*
* @return Expr
*/
Expand Down
7 changes: 6 additions & 1 deletion lib/PhpParser/BuilderHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -215,7 +216,7 @@ public static function normalizeType($type) {
* Normalizes a value: Converts nulls, booleans, integers,
* floats, strings and arrays into their respective nodes
*
* @param Node\Expr|bool|null|int|float|string|array $value The value to normalize
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value The value to normalize
*
* @return Expr The normalized value
*/
Expand Down Expand Up @@ -269,6 +270,10 @@ public static function normalizeValue($value) : Expr {
return new Expr\Array_($items);
}

if ($value instanceof \UnitEnum) {
return new Expr\ClassConstFetch(new FullyQualified(\get_class($value)), new Identifier($value->name));
}

throw new \LogicException('Invalid value');
}

Expand Down
12 changes: 12 additions & 0 deletions test/PhpParser/BuilderHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpParser;

use PhpParser\Builder\Class_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -222,4 +224,14 @@ public function testNormalizeAttribute() {
$this->expectExceptionMessage('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
BuilderHelpers::normalizeAttribute('test');
}

public function testNormalizeValueEnum() {
if (\PHP_VERSION_ID <= 80100) {
$this->markTestSkipped('Enums are supported since PHP 8.1');
}

include __DIR__ . '/../fixtures/Suit.php';

$this->assertEquals(new Expr\ClassConstFetch(new FullyQualified(\Suit::class), new Identifier('Hearts')), BuilderHelpers::normalizeValue(\Suit::Hearts));
}
}
9 changes: 9 additions & 0 deletions test/fixtures/Suit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

0 comments on commit 8a21ec3

Please sign in to comment.