Skip to content

Commit

Permalink
Deprecate OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 23, 2024
1 parent b9b8d52 commit 7121673
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.12.0 (2024-XX-XX)

* Deprecate `OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES`
* Fix performance regression when `use_yield` is `false` (which is the default)
* Improve compatibility when `use_yield` is `false` (as extensions still using `echo` will work as is)
* Accept colons (`:`) in addition to equals (`=`) to separate argument names and values in named arguments
Expand All @@ -18,6 +19,7 @@

# 3.11.0 (2024-08-08)

* Deprecate `OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER`
* Add `Twig\Cache\ChainCache` and `Twig\Cache\ReadOnlyFilesystemCache`
* Add the possibility to deprecate attributes and nodes on `Node`
* Add the possibility to add a package and a version to the `deprecated` tag
Expand Down
5 changes: 5 additions & 0 deletions doc/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ Node Visitors
* The ``Twig\NodeVisitor\AbstractNodeVisitor`` class is deprecated, implement the
``Twig\NodeVisitor\NodeVisitorInterface`` interface instead.

* The ``Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER`` and the
``Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES`` options are
deprecated as of Twig 3.12 and will be removed in Twig 4.0; they don't do
anything anymore.

Parser
------

Expand Down
40 changes: 4 additions & 36 deletions src/NodeVisitor/OptimizerNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function __construct(int $optimizers = -1)
trigger_deprecation('twig/twig', '3.11', 'The "Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER" option is deprecated and does nothing.');
}

if (-1 !== $optimizers && self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $optimizers)) {
trigger_deprecation('twig/twig', '3.12', 'The "Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES" option is deprecated and does nothing.');
}

$this->optimizers = $optimizers;
}

Expand All @@ -82,42 +86,6 @@ public function leaveNode(Node $node, Environment $env): ?Node

$node = $this->optimizePrintNode($node);

if (self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $this->optimizers)) {
$node = $this->mergeTextNodeCalls($node);
}

return $node;
}

private function mergeTextNodeCalls(Node $node): Node
{
$text = '';
$names = [];
foreach ($node as $k => $n) {
if (!$n instanceof TextNode) {
return $node;
}

$text .= $n->getAttribute('data');
$names[] = $k;
}

if (!$text) {
return $node;
}

if (Node::class === \get_class($node)) {
return new TextNode($text, $node->getTemplateLine());
}

foreach ($names as $i => $name) {
if (0 === $i) {
$node->setNode($name, new TextNode($text, $node->getTemplateLine()));
} else {
$node->removeNode($name);
}
}

return $node;
}

Expand Down
6 changes: 2 additions & 4 deletions tests/NodeVisitor/OptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class OptimizerTest extends TestCase
public function testConstructor()
{
$this->expectNotToPerformAssertions();
new OptimizerNodeVisitor(
OptimizerNodeVisitor::OPTIMIZE_FOR
| OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES
);

new OptimizerNodeVisitor(OptimizerNodeVisitor::OPTIMIZE_FOR);
}

public function testRenderBlockOptimizer()
Expand Down

0 comments on commit 7121673

Please sign in to comment.