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

Conversation

panzer-punk
Copy link
Contributor

Hello there!
I have encountered broken code generation with the new operator inside ternary operator.
ReflectionClosure->getCode() skips the ':' symbol if constructor parentheses are omitted.
Sample code:

function () {
    $flag = true;
    return $flag ? new RegularClass : new RegularClass;
}

will result a broken code:

function () {
     $flag = true;
     return $flag ? new \Tests\Fixtures\RegularClass  new \Tests\Fixtures\RegularClass;
}

This can be reproduced in 1.x branch as well.

@@ -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']):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3rd param of true should be passed to in_array, to use a strict comparison.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@panzer-punk
Copy link
Contributor Author

@GrahamCampbell Should I do the same PR for 1.x?

@taylorotwell taylorotwell merged commit 613b2d4 into laravel:2.x Dec 16, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants