diff --git a/lib/PhpParser/BuilderHelpers.php b/lib/PhpParser/BuilderHelpers.php index af6ceb9968..16e7abada7 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)), $value->name); + } + throw new \LogicException('Invalid value'); } diff --git a/test/PhpParser/BuilderHelpersPHP81Test.php b/test/PhpParser/BuilderHelpersPHP81Test.php new file mode 100644 index 0000000000..d9704a9715 --- /dev/null +++ b/test/PhpParser/BuilderHelpersPHP81Test.php @@ -0,0 +1,19 @@ +markTestSkipped('Enums are supported since PHP 8.1'); + } + + include __DIR__ . '/../code/Suit.php'; + + $this->assertEquals(new Expr\ClassConstFetch(new FullyQualified(\Suit::class), 'Hearts'), BuilderHelpers::normalizeValue(\Suit::Hearts)); + } +} diff --git a/test/code/Suit.php b/test/code/Suit.php new file mode 100644 index 0000000000..2a72d350d6 --- /dev/null +++ b/test/code/Suit.php @@ -0,0 +1,9 @@ +