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

Fix broken code for ternary operator with class instantiation with omitted parentheses #102

Merged
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
2 changes: 1 addition & 1 deletion src/Support/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], true):
if ($lastState === 'closure' && $context === 'root') {
$state = 'closure';
$code .= $id_start.$token;
Expand Down
16 changes: 16 additions & 0 deletions tests/ReflectionClosure5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Loading