Skip to content

Commit

Permalink
Merge pull request #74 from adoy/bugfix/anonymous-extends-implements
Browse files Browse the repository at this point in the history
Fix `getCode` when returning anonymous class extending/implementing non FQCN.
  • Loading branch information
nunomaduro authored Oct 16, 2023
2 parents bfc58d9 + bcbde06 commit 22718bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Support/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ public function getCode()
break;
case 'anonymous':
switch ($token[0]) {
case T_NAME_QUALIFIED:
[$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]);
$state = 'id_name';
$lastState = 'anonymous';
break 2;
case T_NS_SEPARATOR:
case T_STRING:
$id_start = $token[1];
Expand Down
12 changes: 12 additions & 0 deletions tests/ReflectionClosure5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@
$f4 = fn () => new Qux();
$e4 = 'fn () => new \Foo\Baz\Qux()';

$f5 = fn () => new class extends Baz\Qux {};
$e5 = 'fn () => new class extends \Foo\Bar\Qux {}';

$f6 = fn () => new class extends Baz\Qux implements Baz\Qux {};
$e6 = 'fn () => new class extends \Foo\Bar\Qux implements \Foo\Bar\Qux {}';

$f7 = fn () => new class implements Baz\Qux, Baz\Qux {};
$e7 = 'fn () => new class implements \Foo\Bar\Qux, \Foo\Bar\Qux {}';

expect($f1)->toBeCode($e1);
expect($f2)->toBeCode($e2);
expect($f3)->toBeCode($e3);
expect($f4)->toBeCode($e4);
expect($f5)->toBeCode($e5);
expect($f6)->toBeCode($e6);
expect($f7)->toBeCode($e7);
});

test('class keywords instantiation', function () {
Expand Down

0 comments on commit 22718bc

Please sign in to comment.