From d0307bd794a2d58d2ff7cfc4f8009a89f49e700f Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Sat, 10 Aug 2024 20:03:26 +0200 Subject: [PATCH] Normalize enum value to ClassConstFetch Fixes #930 --- lib/PhpParser/Builder/ClassConst.php | 4 ++-- lib/PhpParser/BuilderFactory.php | 2 +- lib/PhpParser/BuilderHelpers.php | 7 ++++++- test/PhpParser/BuilderHelpersTest.php | 12 ++++++++++++ test/fixtures/Suit.php | 9 +++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/Suit.php diff --git a/lib/PhpParser/Builder/ClassConst.php b/lib/PhpParser/Builder/ClassConst.php index a7fe129b0b..7c1512d079 100644 --- a/lib/PhpParser/Builder/ClassConst.php +++ b/lib/PhpParser/Builder/ClassConst.php @@ -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))]; @@ -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) */ diff --git a/lib/PhpParser/BuilderFactory.php b/lib/PhpParser/BuilderFactory.php index af010e028a..0aae8738ce 100644 --- a/lib/PhpParser/BuilderFactory.php +++ b/lib/PhpParser/BuilderFactory.php @@ -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 */ diff --git a/lib/PhpParser/BuilderHelpers.php b/lib/PhpParser/BuilderHelpers.php index af6ceb9968..05e7868f47 100644 --- a/lib/PhpParser/BuilderHelpers.php +++ b/lib/PhpParser/BuilderHelpers.php @@ -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; @@ -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 */ @@ -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'); } diff --git a/test/PhpParser/BuilderHelpersTest.php b/test/PhpParser/BuilderHelpersTest.php index 531da05c34..8dbb617cec 100644 --- a/test/PhpParser/BuilderHelpersTest.php +++ b/test/PhpParser/BuilderHelpersTest.php @@ -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; @@ -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)); + } } diff --git a/test/fixtures/Suit.php b/test/fixtures/Suit.php new file mode 100644 index 0000000000..2a72d350d6 --- /dev/null +++ b/test/fixtures/Suit.php @@ -0,0 +1,9 @@ +