Skip to content

Commit

Permalink
[1.x] Fixes namespace resolution on named arguments (#69)
Browse files Browse the repository at this point in the history
* Fixes namespace resolution on named arguments

* Apply fixes from StyleCI

* Re-add test

* Removes non-used class

* Fixes test suite

* tests functions

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
nunomaduro and StyleCIBot authored Jul 13, 2023
1 parent 5c1e047 commit c19f8f3
Show file tree
Hide file tree
Showing 8 changed files with 556 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Support/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,7 @@ public function getCode()
// named arguments...
case ':':
if ($lastState === 'closure' && $context === 'root') {
$state = 'ignore_next';
$lastState = 'closure';
$state = 'closure';
$code .= $id_start.$token;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/Fixtures/RegularClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Fixtures;

class RegularClass
{
public const C = 'CONST';

public function __construct(
public $a1 = null,
public $a2 = null,
public $a3 = null,
public $a4 = null,
public $a5 = null,
public $a6 = null,
public $a7 = null,
public $a8 = null,
public $a9 = null,
public $a10 = null,
public $a11 = null,
public $a12 = null,
public $a13 = null,
public $a14 = null,
public $a15 = null,
public $a16 = null,
public $a17 = null,
public $a18 = null,
public $a19 = null,
public $a20 = null,
) {
}

public static function staticMethod()
{
return 'staticMethod';
}

public function instanceMethod()
{
return 'instanceMethod';
}
}
15 changes: 15 additions & 0 deletions tests/ReflectionClosure1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Fake
use Foo\Bar;
use Foo\Baz as Qux;
use Tests\Fixtures\RegularClass;

test('new instance', function () {
$f = function () {
Expand Down Expand Up @@ -281,4 +282,18 @@
$e1 = 'function () {
return "{$foo}{$bar}{$foobar}";
}';

expect($f1)->toBeCode($e1);
});

test('consts', function () {
$f1 = function () {
return RegularClass::C;
};

$e1 = 'function () {
return \Tests\Fixtures\RegularClass::C;
}';

expect($f1)->toBeCode($e1);
});
Empty file.
282 changes: 282 additions & 0 deletions tests/ReflectionClosurePhp81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Foo\Baz\Qux\Forest;
use Some\ClassName as ClassAlias;
use Tests\Fixtures\Model;
use Tests\Fixtures\RegularClass;
use function Tests\Fixtures\{makeModel};

enum GlobalEnum {
Expand All @@ -11,6 +12,276 @@ enum GlobalEnum {
case Moderator;
}

test('named arguments', function () {
$variable = new RegularClass();

$f1 = function (string $a1) use ($variable) {
return new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(),
a11: RegularClass::C,
a12: RegularClass::staticMethod(),
a13: (new RegularClass())->instanceMethod(),
a14: [new RegularClass(), RegularClass::C, RegularClass::staticMethod(), (new RegularClass())->instanceMethod()],
),
a11: RegularClass::C,
a12: [new RegularClass(), RegularClass::C],
a13: RegularClass::staticMethod(),
a14: (new RegularClass())->instanceMethod(),
a15: fn () => new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(),
a11: RegularClass::C,
a12: RegularClass::staticMethod(),
a13: (new RegularClass())->instanceMethod(),
a14: [new RegularClass(), RegularClass::C, RegularClass::staticMethod(), (new RegularClass())->instanceMethod()],
),
a11: RegularClass::C,
a12: [new RegularClass(), RegularClass::C],
a13: RegularClass::staticMethod(),
a14: (new RegularClass())->instanceMethod(),
),
a16: fn () => fn () => new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(),
a11: RegularClass::C,
a12: RegularClass::staticMethod(),
a13: (new RegularClass())->instanceMethod(),
a14: [new RegularClass(), RegularClass::C, RegularClass::staticMethod(), (new RegularClass())->instanceMethod()],
),
a11: RegularClass::C,
a12: [new RegularClass(), RegularClass::C],
a13: RegularClass::staticMethod(),
a14: (new RegularClass())->instanceMethod(),
),
a17: function () use ($variable) {
return new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(
a1: $a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', $a1, 1, null, true],
a9: [[[[['string', $a1, 1, null, true]]]]],
a10: new RegularClass(),
a11: RegularClass::C,
a12: RegularClass::staticMethod(),
a13: (new RegularClass())->instanceMethod(),
a14: [new RegularClass(), RegularClass::C, RegularClass::staticMethod(), (new RegularClass())->instanceMethod()],
),
a11: RegularClass::C,
a12: [new RegularClass(), RegularClass::C],
a13: RegularClass::staticMethod(),
a14: (new RegularClass())->instanceMethod(),
);
},
a18: reflection_closure_my_function(),
a19: reflection_closure_my_function(ReflectionClosureGlobalEnum::Guest),
a20: reflection_closure_my_function(enum: ReflectionClosureGlobalEnum::Guest),
);
};

$e1 = "function (string \$a1) use (\$variable) {
return new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(),
a11: \Tests\Fixtures\RegularClass::C,
a12: \Tests\Fixtures\RegularClass::staticMethod(),
a13: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
a14: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C, \Tests\Fixtures\RegularClass::staticMethod(), (new \Tests\Fixtures\RegularClass())->instanceMethod()],
),
a11: \Tests\Fixtures\RegularClass::C,
a12: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C],
a13: \Tests\Fixtures\RegularClass::staticMethod(),
a14: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
a15: fn () => new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(),
a11: \Tests\Fixtures\RegularClass::C,
a12: \Tests\Fixtures\RegularClass::staticMethod(),
a13: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
a14: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C, \Tests\Fixtures\RegularClass::staticMethod(), (new \Tests\Fixtures\RegularClass())->instanceMethod()],
),
a11: \Tests\Fixtures\RegularClass::C,
a12: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C],
a13: \Tests\Fixtures\RegularClass::staticMethod(),
a14: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
),
a16: fn () => fn () => new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(),
a11: \Tests\Fixtures\RegularClass::C,
a12: \Tests\Fixtures\RegularClass::staticMethod(),
a13: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
a14: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C, \Tests\Fixtures\RegularClass::staticMethod(), (new \Tests\Fixtures\RegularClass())->instanceMethod()],
),
a11: \Tests\Fixtures\RegularClass::C,
a12: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C],
a13: \Tests\Fixtures\RegularClass::staticMethod(),
a14: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
),
a17: function () use (\$variable) {
return new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(
a1: \$a1,
a2: 'string',
a3: 1,
a4: 1.1,
a5: true,
a6: null,
a7: ['string'],
a8: ['string', \$a1, 1, null, true],
a9: [[[[['string', \$a1, 1, null, true]]]]],
a10: new \Tests\Fixtures\RegularClass(),
a11: \Tests\Fixtures\RegularClass::C,
a12: \Tests\Fixtures\RegularClass::staticMethod(),
a13: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
a14: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C, \Tests\Fixtures\RegularClass::staticMethod(), (new \Tests\Fixtures\RegularClass())->instanceMethod()],
),
a11: \Tests\Fixtures\RegularClass::C,
a12: [new \Tests\Fixtures\RegularClass(), \Tests\Fixtures\RegularClass::C],
a13: \Tests\Fixtures\RegularClass::staticMethod(),
a14: (new \Tests\Fixtures\RegularClass())->instanceMethod(),
);
},
a18: \\reflection_closure_my_function(),
a19: \\reflection_closure_my_function(\ReflectionClosureGlobalEnum::Guest),
a20: \\reflection_closure_my_function(enum: \ReflectionClosureGlobalEnum::Guest),
);
}";

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

test('enums', function () {
$f = function (GlobalEnum $role) {
return $role;
Expand Down Expand Up @@ -419,3 +690,14 @@ public function getSelf(self $instance): self
return $instance;
}
}

enum ReflectionClosureGlobalEnum {
case Admin;
case Guest;
case Moderator;
}

function reflection_closure_my_function(SerializerGlobalEnum $enum = SerializerGlobalEnum::Admin)
{
return $enum;
}
Loading

0 comments on commit c19f8f3

Please sign in to comment.