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

[Twig 4] Introduce ForElseNode #4473

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Nov 24, 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:

{% for product in products %}
    <h2>FOR</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:

// line 1
foreach ($context['_seq'] as $context["_key"] => $context["product"]) {
    // line 2
    yield "        <h2>FOR</h2>\n    ";
    $context['_iterated'] = \true;
}
if (!$context['_iterated']) {
    // line 4
    yield "        <p>No products found</p>\n    ";
}

With this change, we will change the compiled code to become:

// line 1
foreach ($context['_seq'] as $context["_key"] => $context["product"]) {
    // line 2
    yield "        <h2>FOR</h2>\n    ";
    $context['_iterated'] = \true;
}
// line 3
if (!$context['_iterated']) {
    // line 4
    yield "        <p>No products found</p>\n    ";
}

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    ";
}
```
@@ -25,7 +25,7 @@
#[YieldReady]
class ForNode extends Node
{
public function __construct(AssignContextVariable $keyTarget, AssignContextVariable $valueTarget, AbstractExpression $seq, ?AbstractExpression $ifexpr, Node $body, ?Node $else, int $lineno)
public function __construct(AssignContextVariable $keyTarget, AssignContextVariable $valueTarget, AbstractExpression $seq, ?AbstractExpression $ifexpr, Node $body, ?ForElseNode $else, int $lineno)
Copy link
Member

Choose a reason for hiding this comment

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

Restricting the type is a BC break. You should introduce the new class in 3.x with a deprecation instead (where passing something else than a ForElseNode or null would trigger a deprecation and wrap the node in a ForElseNode, using the line of that node as fallback line number)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea.

Do you think the direction of this PR is OK / makes sense? I can propose the V3 change once that is clear.

->outdent()
->write("}\n")
;
$compiler->subcompile($this->getNode('else'));
Copy link
Contributor

@fabpot fabpot Nov 29, 2024

Choose a reason for hiding this comment

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

IIUC, this is to get the // line xxx comment above the if instead of inside it, right?

What about adding ->addDebugInfo($this->getNode('else')) after current line 75 instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The goal of this PR is to get the line number of {% else %} above the PHP if condition.

Your suggestion would print the line number of the line after the {% else %}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can see it in the example of the PR description.

Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing something. Isn't it what you want?

$_v0 = new \Twig\Runtime\LoopIterator([1, 2, 3, 4]);
yield from ($_v1 = function ($iterator, &$context, $blocks, $recurseFunc, $depth) {
    $macros = $this->macros;
    $parent = $context;
    foreach ($iterator as $context["_key"] => $context["key"]) {
        // line 3
        yield "    IF";
    }
    // line 5
    if (0 === $iterator->getIndex0()) {
        yield "    ELSE";
    }
    unset($context['_key'], $context['key']);
    $context = array_intersect_key($context, $parent) + $parent;
    yield from [];
})($_v0, $context, $blocks, $_v1, 0);

Copy link
Contributor Author

@ruudk ruudk Dec 2, 2024

Choose a reason for hiding this comment

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

I tried doing ->addDebugInfo($this->getNode('else')) but that will print the wrong line (4 instead of 3).

1 {% for product in products %}
2    <h2>FOR</h2>
3 {% else %}
4    <p>No products found</p>
5 {% endfor %}

Before this PR it compiles to:

// line 1
foreach ($context['_seq'] as $context["_key"] => $context["product"]) {
    // line 2
    yield "        <h2>FOR</h2>\n    ";
    $context['_iterated'] = \true;
}
if (!$context['_iterated']) {
    // line 4
    yield "        <p>No products found</p>\n    ";
}

With this PR it compiles to:

// line 1
foreach ($context['_seq'] as $context["_key"] => $context["product"]) {
    // line 2
    yield "        <h2>FOR</h2>\n    ";
    $context['_iterated'] = \true;
}
// line 3
if (!$context['_iterated']) {
    // line 4
    yield "        <p>No products found</p>\n    ";
}

Because if (!$context['_iterated']) { points to {% else %} on line 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants