Skip to content

Commit 6a97061

Browse files
ruudknikic
authored andcommitted
Normalize enum value to ClassConstFetch
Fixes #930 (cherry picked from commit 8a21ec3)
1 parent e3f223f commit 6a97061

5 files changed

Lines changed: 28 additions & 4 deletions

File tree

lib/PhpParser/Builder/ClassConst.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ClassConst implements PhpParser\Builder {
2828
* Creates a class constant builder
2929
*
3030
* @param string|Identifier $name Name
31-
* @param Node\Expr|bool|null|int|float|string|array $value Value
31+
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value Value
3232
*/
3333
public function __construct($name, $value) {
3434
$this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
@@ -38,7 +38,7 @@ public function __construct($name, $value) {
3838
* Add another constant to const group
3939
*
4040
* @param string|Identifier $name Name
41-
* @param Node\Expr|bool|null|int|float|string|array $value Value
41+
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value Value
4242
*
4343
* @return $this The builder instance (for fluid interface)
4444
*/

lib/PhpParser/BuilderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function enumCase($name): Builder\EnumCase {
210210
/**
211211
* Creates node a for a literal value.
212212
*
213-
* @param Expr|bool|null|int|float|string|array $value $value
213+
* @param Expr|bool|null|int|float|string|array|\UnitEnum $value $value
214214
*/
215215
public function val($value): Expr {
216216
return BuilderHelpers::normalizeValue($value);

lib/PhpParser/BuilderHelpers.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Identifier;
88
use PhpParser\Node\Name;
9+
use PhpParser\Node\Name\FullyQualified;
910
use PhpParser\Node\NullableType;
1011
use PhpParser\Node\Scalar;
1112
use PhpParser\Node\Stmt;
@@ -214,7 +215,7 @@ public static function normalizeType($type) {
214215
* Normalizes a value: Converts nulls, booleans, integers,
215216
* floats, strings and arrays into their respective nodes
216217
*
217-
* @param Node\Expr|bool|null|int|float|string|array $value The value to normalize
218+
* @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value The value to normalize
218219
*
219220
* @return Expr The normalized value
220221
*/
@@ -268,6 +269,10 @@ public static function normalizeValue($value): Expr {
268269
return new Expr\Array_($items);
269270
}
270271

272+
if ($value instanceof \UnitEnum) {
273+
return new Expr\ClassConstFetch(new FullyQualified(\get_class($value)), new Identifier($value->name));
274+
}
275+
271276
throw new \LogicException('Invalid value');
272277
}
273278

test/PhpParser/BuilderHelpersTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Builder\Class_;
66
use PhpParser\Node\Identifier;
7+
use PhpParser\Node\Name\FullyQualified;
78
use PhpParser\Node\Scalar;
89
use PhpParser\Node\Stmt;
910
use PhpParser\Node\Expr;
@@ -222,4 +223,14 @@ public function testNormalizeAttribute(): void {
222223
$this->expectExceptionMessage('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
223224
BuilderHelpers::normalizeAttribute('test');
224225
}
226+
227+
public function testNormalizeValueEnum() {
228+
if (\PHP_VERSION_ID <= 80100) {
229+
$this->markTestSkipped('Enums are supported since PHP 8.1');
230+
}
231+
232+
include __DIR__ . '/../fixtures/Suit.php';
233+
234+
$this->assertEquals(new Expr\ClassConstFetch(new FullyQualified(\Suit::class), new Identifier('Hearts')), BuilderHelpers::normalizeValue(\Suit::Hearts));
235+
}
225236
}

test/fixtures/Suit.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
enum Suit {
4+
case Hearts;
5+
case Diamonds;
6+
case Clubs;
7+
case Spades;
8+
}

0 commit comments

Comments
 (0)