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

[8.x] Consistently negate conditions in @includeUnless() Blade directive #40077

Conversation

markjaquith
Copy link
Contributor

https://twitter.com/gonedark/status/1471514212363079681
CleanShot 2021-12-16 at 14 05 28@2x

Before this change, @includeUnless($undefined ?? true, 'template') would result in a PHP Warning and an unexpected condition result, because $undefined ?? true would be compiled as ! $undefined ?? true, which due to ! having precedence over ?? is different than ! ($undefined ?? true) (which is how @unless would compile it).

>>> ! $undefined ?? true
<warning>PHP Warning:  Undefined variable $undefined in eval()'d code on line 1</warning>
=> true
>>> ! ($undefined ?? true)
=> false

This change necessitated the creation of renderUnless(), because @includeUnless() accepts multiple arguments, and parsing out the first argument to a Blade directive is fraught. So renderUnless() takes the parameters as-is, and negates the condition inside the method.

@includeUnless() also lacked unit tests, which I added.

This change makes @includeUnless() work more like @unless(), which already correctly wraps the condition in parentheses (or rather, does not remove the parentheses that are already there).

Before this, `@includeUnless($undefined ?? true, 'template')` would result
in a PHP Warning and an unexpected condition result, because
`$undefined ?? true` would be compiled as `! $undefined ?? true`, which due to
`!` having precedence over `??` is different than `! ($undefined ?? true)`
(which is how `@unless` would compile it).

This necessitated the creation of `renderUnless()`, because `@includeUnless()`
accepts multiple arguments, and parsing out the first argument to a Blade
directive is fraught. So `renderUnless()` takes the parameters as-is,
and negates the condition inside the method.
@taylorotwell taylorotwell merged commit 1759e5f into laravel:8.x Dec 16, 2021
@taylorotwell
Copy link
Member

Thanks for contributing to Laravel! ❤️

@jasonmccreary
Copy link
Contributor

@markjaquith, nice work!

@GrahamCampbell GrahamCampbell changed the title Consistently negate conditions in @includeUnless() Blade directive [8.x] Consistently negate conditions in @includeUnless() Blade directive Dec 17, 2021
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.

None yet

3 participants