From 36a4b56b29429d6c62f6c35935486114e16d22ca Mon Sep 17 00:00:00 2001 From: Daniil Date: Mon, 16 Dec 2024 01:44:52 +0300 Subject: [PATCH 1/3] Fix ReflectionClosure wrong code for new without constructor in ternary --- src/Support/ReflectionClosure.php | 2 +- tests/ReflectionClosure5Test.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Support/ReflectionClosure.php b/src/Support/ReflectionClosure.php index 9c5f3b0d..e77f2b61 100644 --- a/src/Support/ReflectionClosure.php +++ b/src/Support/ReflectionClosure.php @@ -508,7 +508,7 @@ public function getCode() break; case 'id_name': switch ($token[0]) { - case $token[0] === ':' && $context !== 'instanceof': + case $token[0] === ':' && !in_array($context, ['instanceof', 'new']): if ($lastState === 'closure' && $context === 'root') { $state = 'closure'; $code .= $id_start.$token; diff --git a/tests/ReflectionClosure5Test.php b/tests/ReflectionClosure5Test.php index 2919d45b..cbf411ac 100644 --- a/tests/ReflectionClosure5Test.php +++ b/tests/ReflectionClosure5Test.php @@ -5,6 +5,7 @@ use Foo\Baz\Qux\Forest; use Laravel\SerializableClosure\Support\ReflectionClosure; use Tests\Fixtures\Model; +use Tests\Fixtures\RegularClass; test('is short closure', function () { $f1 = fn () => 1; @@ -243,6 +244,21 @@ public function qux(\Foo\Bar\Qux $qux): \Foo\Bar\Qux expect($f)->toBeCode($e); }); +test('ternanry operator new without constructor', function () { + $f = function () { + $flag = true; + + return $flag ? new RegularClass : new RegularClass; + }; + $e = 'function () { + $flag = true; + + return $flag ? new \Tests\Fixtures\RegularClass : new \Tests\Fixtures\RegularClass; + }'; + + expect($f)->toBeCode($e); +}); + // Helpers function c(Closure $closure) { From 611a64cd1f83b4c10cf6959de792323bb0f06891 Mon Sep 17 00:00:00 2001 From: Daniil Date: Mon, 16 Dec 2024 09:30:24 +0300 Subject: [PATCH 2/3] Fix strict comparsion for in_array --- src/Support/ReflectionClosure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/ReflectionClosure.php b/src/Support/ReflectionClosure.php index e77f2b61..481f2b9f 100644 --- a/src/Support/ReflectionClosure.php +++ b/src/Support/ReflectionClosure.php @@ -508,7 +508,7 @@ public function getCode() break; case 'id_name': switch ($token[0]) { - case $token[0] === ':' && !in_array($context, ['instanceof', 'new']): + case $token[0] === ':' && !in_array($context, ['instanceof', 'new'], true): if ($lastState === 'closure' && $context === 'root') { $state = 'closure'; $code .= $id_start.$token; From bdbf6683ac3cfdcb67e8cd86535780dc7cbb93ee Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 16 Dec 2024 09:21:17 -0600 Subject: [PATCH 3/3] Update ReflectionClosure.php --- src/Support/ReflectionClosure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/ReflectionClosure.php b/src/Support/ReflectionClosure.php index 481f2b9f..8bf13ac0 100644 --- a/src/Support/ReflectionClosure.php +++ b/src/Support/ReflectionClosure.php @@ -508,7 +508,7 @@ public function getCode() break; case 'id_name': switch ($token[0]) { - case $token[0] === ':' && !in_array($context, ['instanceof', 'new'], true): + case $token[0] === ':' && ! in_array($context, ['instanceof', 'new'], true): if ($lastState === 'closure' && $context === 'root') { $state = 'closure'; $code .= $id_start.$token;