Skip to content

Commit 78e2441

Browse files
[VarExporter] Add support of PHP 8.1 enumerations
1 parent 2f20156 commit 78e2441

File tree

6 files changed

+24
-3
lines changed

6 files changed

+24
-3
lines changed

src/Symfony/Component/VarExporter/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Add support of PHP 8.1 enumerations
8+
49
5.1.0
510
-----
611

src/Symfony/Component/VarExporter/Internal/Exporter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
6060
$value = self::prepare($value, $objectsPool, $refsPool, $objectsCount, $valueIsStatic);
6161
}
6262
goto handle_value;
63-
} elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class) {
63+
} elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class || $value instanceof \UnitEnum) {
6464
goto handle_value;
6565
}
6666

@@ -188,7 +188,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
188188
public static function export($value, string $indent = '')
189189
{
190190
switch (true) {
191-
case \is_int($value) || \is_float($value): return var_export($value, true);
191+
case \is_int($value) || \is_float($value) || $value instanceof \UnitEnum : return var_export($value, true);
192192
case [] === $value: return '[]';
193193
case false === $value: return 'false';
194194
case true === $value: return 'true';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
4+
5+
enum FooUnitEnum
6+
{
7+
case Bar;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum::Bar;

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
1818
use Symfony\Component\VarExporter\Internal\Registry;
1919
use Symfony\Component\VarExporter\VarExporter;
20+
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
2021

2122
class VarExporterTest extends TestCase
2223
{
@@ -209,6 +210,10 @@ public function provideExport()
209210
yield ['private-constructor', PrivateConstructor::create('bar')];
210211

211212
yield ['php74-serializable', new Php74Serializable()];
213+
214+
if (\PHP_VERSION_ID >= 80100) {
215+
yield ['unit-enum', FooUnitEnum::Bar, true];
216+
}
212217
}
213218
}
214219

src/Symfony/Component/VarExporter/VarExporter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function export($value, bool &$isStaticValue = null, array &$found
4444
{
4545
$isStaticValue = true;
4646

47-
if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value)) {
47+
if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value) || $value instanceof \UnitEnum) {
4848
return Exporter::export($value);
4949
}
5050

0 commit comments

Comments
 (0)