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

[1.x] Fixes serialisation of closures with named arguments code #29

Merged
merged 4 commits into from
Nov 26, 2021
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</testsuite>
<testsuite name="php80">
<file phpVersion="8.0.0-dev" phpVersionOperator=">=">./tests/ReflectionClosurePhp80Test.php</file>
<file phpVersion="8.0.0-dev" phpVersionOperator=">=">./tests/SerializerPhp80Test.php</file>
</testsuite>
<testsuite name="php81">
<file phpVersion="8.1.0-dev" phpVersionOperator=">=">./tests/ReflectionClosurePhp81Test.php</file>
Expand Down
9 changes: 9 additions & 0 deletions src/Support/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ public function getCode()
break;
case 'id_name':
switch ($token[0]) {
// named arguments...
case ':':
if ($lastState === 'closure' && $context === 'root') {
$state = 'ignore_next';
$lastState = 'closure';
$code .= $id_start.$token;
}

break;
case T_NAME_QUALIFIED:
case T_NS_SEPARATOR:
case T_STRING:
Expand Down
63 changes: 52 additions & 11 deletions tests/ReflectionClosurePhp80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,65 @@
return $firstName.' '.$lastName;
};

expect('Marco Deleu')->toBe(s($f1)(
lastName: 'Deleu',
firstName: 'Marco'
));
$e1 = "function (string \$firstName, string \$lastName) {
return \$firstName.' '.\$lastName;
}";

expect($f1)->toBeCode($e1);
})->with('serializers');

test('constructor property promotion', function () {
$class = new PropertyPromotion('public', 'protected', 'private');
test('single named argument within closures', function () {
$f1 = function () {
return (new ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string');
};

$f1 = fn () => $class;
$e1 = "function () {
return (new \ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string');
}";

$object = s($f1)();
expect($f1)->toBeCode($e1);
})->with('serializers');

expect($object->public)->toBe('public');
expect($object->getProtected())->toBe('protected');
expect($object->getPrivate())->toBe('private');
test('multiple named arguments within closures', function () {
$f1 = function () {
return (new ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string', namedArgumentB: 1);
};

$e1 = "function () {
return (new \ReflectionClosurePhp80NamedArguments)->publicMethod(namedArgument: 'string', namedArgumentB: 1);
}";

expect($f1)->toBeCode($e1);
})->with('serializers');

test('multiple named arguments within nested closures', function () {
$f1 = function () {
$fn = fn ($namedArgument, $namedArgumentB) => (
new ReflectionClosurePhp80NamedArguments
)->publicMethod(namedArgument: $namedArgument, namedArgumentB: $namedArgumentB);

return $fn(namedArgument: 'string', namedArgumentB: 1);
};

$e1 = "function () {
\$fn = fn (\$namedArgument, \$namedArgumentB) => (
new \ReflectionClosurePhp80NamedArguments
)->publicMethod(namedArgument: \$namedArgument, namedArgumentB: \$namedArgumentB);

return \$fn(namedArgument: 'string', namedArgumentB: 1);
}";

expect($f1)->toBeCode($e1);
})->with('serializers');

class ReflectionClosurePhp80NamedArguments
{
public function publicMethod(string $namedArgument, $namedArgumentB = null)
{
return $namedArgument.(string) $namedArgumentB;
}
}

class PropertyPromotion
{
public function __construct(
Expand Down
52 changes: 52 additions & 0 deletions tests/SerializerPhp80Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

test('named arguments', function () {
$f1 = function (string $firstName, string $lastName) {
return $firstName.' '.$lastName;
};

expect('Marco Deleu')->toBe(s($f1)(
lastName: 'Deleu',
firstName: 'Marco'
));
})->with('serializers');

test('single named argument within closures', function () {
$f1 = function () {
return (new SerializerPhp80NamedArguments)->publicMethod(
namedArgument: 'string'
);
};

expect('string')->toBe(s($f1)());
})->with('serializers');

test('multiple named arguments within closures', function () {
$f1 = function () {
return (new SerializerPhp80NamedArguments)->publicMethod(
namedArgument: 'string', namedArgumentB: 1
);
};

expect('string1')->toBe(s($f1)());
})->with('serializers');

test('multiple named arguments within nested closures', function () {
$f1 = function () {
$fn = fn ($namedArgument, $namedArgumentB) => (
new SerializerPhp80NamedArguments
)->publicMethod(namedArgument: $namedArgument, namedArgumentB: $namedArgumentB);

return $fn(namedArgument: 'string', namedArgumentB: 1);
};

expect('string1')->toBe(s($f1)());
})->with('serializers');

class SerializerPhp80NamedArguments
{
public function publicMethod(string $namedArgument, $namedArgumentB = null)
{
return $namedArgument.(string) $namedArgumentB;
}
}
12 changes: 12 additions & 0 deletions tests/SerializerPhp81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ enum SerializerScopedBackedEnum: string {
expect($f())->toBe('foo');
})->with('serializers');

test('constructor property promotion', function () {
$class = new PropertyPromotion('public', 'protected', 'private');

$f1 = fn () => $class;

$object = s($f1)();

expect($object->public)->toBe('public');
expect($object->getProtected())->toBe('protected');
expect($object->getPrivate())->toBe('private');
})->with('serializers');

interface SerializerPhp81HasId {}
interface SerializerPhp81HasName {}

Expand Down