Skip to content

Fix dumping enums on PHP 8.2 #46170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ abstract protected function describeCallable($callable, array $options = []);
*/
protected function formatValue($value): string
{
if ($value instanceof \UnitEnum) {
return ltrim(var_export($value, true), '\\');
}

if (\is_object($value)) {
return sprintf('object(%s)', \get_class($value));
}
Expand All @@ -158,7 +162,7 @@ protected function formatValue($value): string
return $value;
}

return preg_replace("/\n\s*/s", '', var_export($value, true));
return preg_replace("/\n\s*/s", '', ltrim(var_export($value, true)), '\\');
}

/**
Expand All @@ -169,15 +173,15 @@ protected function formatValue($value): string
protected function formatParameter($value): string
{
if ($value instanceof \UnitEnum) {
return var_export($value, true);
return ltrim(var_export($value, true), '\\');
}

// Recursively search for enum values, so we can replace it
// before json_encode (which will not display anything for \UnitEnum otherwise)
if (\is_array($value)) {
array_walk_recursive($value, static function (&$value) {
if ($value instanceof \UnitEnum) {
$value = var_export($value, true);
$value = ltrim(var_export($value, true), '\\');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function writeData(array $data, array $options)
// before json_encode (which will not display anything for \UnitEnum otherwise)
array_walk_recursive($data, static function (&$value) {
if ($value instanceof \UnitEnum) {
$value = var_export($value, true);
$value = ltrim(var_export($value, true), '\\');
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
} elseif ($argument instanceof Definition) {
$argumentsInformation[] = 'Inlined Service';
} elseif ($argument instanceof \UnitEnum) {
$argumentsInformation[] = var_export($argument, true);
$argumentsInformation[] = ltrim(var_export($argument, true), '\\');
} else {
$argumentsInformation[] = \is_array($argument) ? sprintf('Array (%d element(s))', \count($argument)) : $argument;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private function getArgumentNodes(array $arguments, \DOMDocument $dom): array
}
} elseif ($argument instanceof \UnitEnum) {
$argumentXML->setAttribute('type', 'constant');
$argumentXML->appendChild(new \DOMText(var_export($argument, true)));
$argumentXML->appendChild(new \DOMText(ltrim(var_export($argument, true), '\\')));
} else {
$argumentXML->appendChild(new \DOMText($argument));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
public static function export($value, $indent = '')
{
switch (true) {
case \is_int($value) || \is_float($value) || $value instanceof \UnitEnum: return var_export($value, true);
case \is_int($value) || \is_float($value): return var_export($value, true);
case [] === $value: return '[]';
case false === $value: return 'false';
case true === $value: return 'true';
case null === $value: return 'null';
case '' === $value: return "''";
case $value instanceof \UnitEnum: return ltrim(var_export($value, true), '\\');
}

if ($value instanceof Reference) {
Expand Down