-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Twig 4] Introduce ForElseNode #4473
Closed
Closed
+57
−12
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stof
reviewed
Nov 26, 2024
fabpot
reviewed
Nov 29, 2024
Currently there is no line number information for `{% else %}` inside a `for` loop. Normally, this wouldn't be such a big deal. But TwigStan uses this line information to map errors in PHP back to the original Twig source. Let's say you have the following template: ```twig {% for product in products %} <h2>{{ product.name }}</h2> {% else %} <p>No products found</p> {% endfor %} ``` And `products` is of type `non-empty-array<Product>`, it means that the else will never happen. This is currently reported as: ``` Negated boolean expression is always false. 🔖 booleanNot.alwaysFalse 🐘 compiled_index.php:83 🌱 templates/product/index.html.twig:2 🎯 src/Controller/ProductController.php:15 ``` But as you can see, it points to line number 2, instead of line number 3. In the compiled code, it looks like this: ```php // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:1 foreach ($context['_seq'] as $context["_key"] => $context["product"]) { // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield " <h2>"; // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield \Twig\Extension\CoreExtension::getAttribute($this->env, $this->source, $context // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield "</h2>\n "; $context['_iterated'] = \true; } if (!$context['_iterated']) { // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:4 yield " <p>No products found</p>\n "; } ``` With this change, we will change the compiled code to become: ```php // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:1 foreach ($context['_seq'] as $context["_key"] => $context["product"]) { // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield " <h2>"; // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield \Twig\Extension\CoreExtension::getAttribute($this->env, $this->source, $context // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:2 yield "</h2>\n "; $context['_iterated'] = \true; } // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:3 if (!$context['_iterated']) { // line /Volumes/CS/opensource/twigstan-demo/templates/product/index.html.twig:4 yield " <p>No products found</p>\n "; } ```
Merged
fabpot
added a commit
that referenced
this pull request
Jan 24, 2025
Thank you! I will rebase as soon as V4 is synced. |
I guess this was closed accidentally? |
Ah, it's already merged. |
Yes, I made the needed changes when merging 3.x to 4.x (basically resolving the conflicts). |
Got it, thank you for getting this merged 💙 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently there is no line number information for
{% else %}
inside afor
loop.Normally, this wouldn't be such a big deal.
But TwigStan uses this line information to map errors in PHP back to the original Twig source.
Let's say you have the following template:
And
products
is of typenon-empty-array<Product>
, it means that the else will never happen.This is currently reported as:
But as you can see, it points to line number 2, instead of line number 3.
In the compiled code, it looks like this:
With this change, we will change the compiled code to become: