brick / varexporter

A powerful alternative to var_export(), which can export closures and objects without __set_state()

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exporting closure with first class callable syntax (e.g. "foo(...)") throws ExportException

AnnaDamm opened this issue · comments

Description

The new syntax for closures foo(...) throws an ExportException when used in an export

Test Case

<?php

use Brick\VarExporter\VarExporter;

require __DIR__ . '/vendor/autoload.php';

function foo(): int {
    return 1;
}

var_dump(VarExporter::export(['foo' => static fn () => foo()]));
var_dump(VarExporter::export(['foo' => foo(...)]));

Result

string(59) "[
    'foo' => function () {
        return \foo();
    }
]"
PHP Fatal error:  Uncaught Brick\VarExporter\ExportException: At [foo]: Expected exactly 1 closure in /Users/anna.damm/Projects/test/test.php on line 7, found 0. in /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/ObjectExporter/ClosureExporter.php:166
Stack trace:
#0 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/ObjectExporter/ClosureExporter.php(54): Brick\VarExporter\Internal\ObjectExporter\ClosureExporter->getClosure(Object(ReflectionFunction), Array, '/Users/anna.dam...', 7, Array)
#1 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/GenericExporter.php(276): Brick\VarExporter\Internal\ObjectExporter\ClosureExporter->export(Object(Closure), Object(ReflectionObject), Array, Array)
#2 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/GenericExporter.php(148): Brick\VarExporter\Internal\GenericExporter->exportObject(Object(Closure), Array, Array)
#3 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/GenericExporter.php(188): Brick\VarExporter\Internal\GenericExporter->export(Object(Closure), Array, Array)
#4 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/GenericExporter.php(144): Brick\VarExporter\Internal\GenericExporter->exportArray(Array, Array, Array)
#5 /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/VarExporter.php(95): Brick\VarExporter\Internal\GenericExporter->export(Array, Array, Array)
#6 /Users/anna.damm/Projects/test/test.php(12): Brick\VarExporter\VarExporter::export(Array)
#7 {main}
  thrown in /Users/anna.damm/Projects/test/vendor/brick/varexporter/src/Internal/ObjectExporter/ClosureExporter.php on line 166

Expected Behavior

The new closure syntax should either return the same code as the one with static fn, or should return foo(...)

I just found out that this only happens with named functions, not with closures. But sadly, using Factory functions on a class, such as ServerRequestFactory::create(...) or similiar don't work because of this.

Thanks for the report, indeed this is not supported yet. PR welcome!