Skip to content
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

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

Open
AnnaDamm opened this issue Jun 5, 2023 · 2 comments

Comments

@AnnaDamm
Copy link
Contributor

AnnaDamm commented Jun 5, 2023

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(...)

@AnnaDamm
Copy link
Contributor Author

AnnaDamm commented Jun 7, 2023

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.

@AnnaDamm AnnaDamm changed the title Exporting closure with new ... syntax throws ExportException Exporting closure with first class callable syntax (e.g. "foo(...)") throws ExportException Jun 7, 2023
@BenMorel
Copy link
Member

BenMorel commented Jun 7, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants