8000 [VarExporter] Add support of PHP 8.1 enumerations · symfony/symfony@3a4c27c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a4c27c

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

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
lines changed

src/Symfony/Component/VarExporter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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';
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum::Bar,
5+
];

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
1717
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
1818
use Symfony\Component\VarExporter\Internal\Registry;
19+
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
1920
use Symfony\Component\VarExporter\VarExporter;
2021

2122
class VarExporterTest extends TestCase
@@ -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

Lines changed: 1 addition & 1 deletion
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)
0