Deprecate node names that are not strings or integers

This commit is contained in:
Fabien Potencier
2024-08-23 19:44:16 +02:00
parent 86330fb9bc
commit 1ee4210ba9
3 changed files with 28 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.12.0 (2024-XX-XX)
* Add support for integers in methods of `Twig\Node\Node` that take a Node name
* 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)
+5
View File
@@ -35,6 +35,11 @@ Extensions
Nodes
-----
* The following ``Twig\Node\Node`` methods will take a string or an integer
(instead of just a string) in Twig 4.0 for their "name" argument:
``getNode()``, ``hasNode()``, ``setNode()``, ``removeNode()``, and
``deprecateNode()``.
* The second argument of the
``Twig\Node\Expression\CallExpression::compileArguments()`` method is
deprecated.
+22 -4
View File
@@ -24,6 +24,9 @@ use Twig\Source;
#[YieldReady]
class Node implements \Countable, \IteratorAggregate
{
/**
* @var array<string|int, Node>
*/
protected $nodes;
protected $attributes;
protected $lineno;
@@ -36,10 +39,10 @@ class Node implements \Countable, \IteratorAggregate
private $attributeNameDeprecations = [];
/**
* @param array $nodes An array of named nodes
* @param array $attributes An array of attributes (should not be nodes)
* @param int $lineno The line number
* @param string $tag The tag name associated with the Node
* @param array<string|int, Node> $nodes An array of named nodes
* @param array $attributes An array of attributes (should not be nodes)
* @param int $lineno The line number
* @param string $tag The tag name associated with the Node
*/
public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0, ?string $tag = null)
{
@@ -158,11 +161,17 @@ class Node implements \Countable, \IteratorAggregate
unset($this->attributes[$name]);
}
/**
* @param string|int $name
*/
public function hasNode(string $name): bool
{
return isset($this->nodes[$name]);
}
/**
* @param string|int $name
*/
public function getNode(string $name): self
{
if (!isset($this->nodes[$name])) {
@@ -182,6 +191,9 @@ class Node implements \Countable, \IteratorAggregate
return $this->nodes[$name];
}
/**
* @param string|int $name
*/
public function setNode(string $name, self $node): void
{
$triggerDeprecation = \func_num_args() > 2 ? func_get_arg(2) : true;
@@ -200,11 +212,17 @@ class Node implements \Countable, \IteratorAggregate
$this->nodes[$name] = $node;
}
/**
* @param string|int $name
*/
public function removeNode(string $name): void
{
unset($this->nodes[$name]);
}
/**
* @param string|int $name
*/
public function deprecateNode(string $name, NameDeprecation $dep): void
{
$this->nodeNameDeprecations[$name] = $dep;