From 79b4efe6c9bf1fd16964ae787f5cd1ba314410ad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 30 Jul 2024 13:50:34 +0200 Subject: [PATCH] Format the array as a PHP array --- packages/testing/src/FluentTestingHelpers.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/testing/src/FluentTestingHelpers.php b/packages/testing/src/FluentTestingHelpers.php index b0255a5c8aa..10ebb8e916f 100644 --- a/packages/testing/src/FluentTestingHelpers.php +++ b/packages/testing/src/FluentTestingHelpers.php @@ -69,11 +69,28 @@ protected function dd($var): void echo $var.($var[-1] === "\n" ? '' : "\n"); echo "```\n"; } elseif(is_array($var)) { - var_export($var); + echo "```php\n"; + echo $this->formatArray($var); + echo "```\n"; } else { dd($var); } die; } + + /** + * @experimental Helper function to format an array as a plain PHP array with [] syntax. + */ + private function formatArray(array $array): string + { + $json = json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + + // Transform JSON to PHP array syntax + $php = preg_replace('/^(\s*)\"(\w+)\":/m', '$1$2:', $json); // Remove quotes from keys + $php = str_replace('{', '[', $php); // Replace { with [ + $php = str_replace('}', ']', $php); // Replace } with ] + + return $php."\n"; + } }