Merge branch '3.x' into 4.x

* 3.x:
  Remove `Template::*_CALL` const optimisations
  Deprecate node names that are not strings or integers
This commit is contained in:
Fabien Potencier
2024-08-24 10:47:05 +02:00
2 changed files with 27 additions and 9 deletions
+5 -5
View File
@@ -1550,10 +1550,10 @@ final class CoreExtension extends AbstractExtension
*
* @internal
*/
public static function getAttribute(Environment $env, Source $source, $object, $item, array $arguments = [], $type = /* Template::ANY_CALL */ 'any', $isDefinedTest = false, $ignoreStrictCheck = false, $sandboxed = false, int $lineno = -1)
public static function getAttribute(Environment $env, Source $source, $object, $item, array $arguments = [], $type = Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false, $sandboxed = false, int $lineno = -1)
{
// array
if (/* Template::METHOD_CALL */ 'method' !== $type) {
if (Template::METHOD_CALL !== $type) {
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item;
if (((\is_array($object) || $object instanceof \ArrayObject) && (isset($object[$arrayItem]) || \array_key_exists($arrayItem, (array) $object)))
@@ -1566,7 +1566,7 @@ final class CoreExtension extends AbstractExtension
return $object[$arrayItem];
}
if (/* Template::ARRAY_CALL */ 'array' === $type || !\is_object($object)) {
if (Template::ARRAY_CALL === $type || !\is_object($object)) {
if ($isDefinedTest) {
return false;
}
@@ -1585,7 +1585,7 @@ final class CoreExtension extends AbstractExtension
} else {
$message = \sprintf('Key "%s" for sequence/mapping with keys "%s" does not exist.', $arrayItem, implode(', ', array_keys($object)));
}
} elseif (/* Template::ARRAY_CALL */ 'array' === $type) {
} elseif (Template::ARRAY_CALL === $type) {
if (null === $object) {
$message = \sprintf('Impossible to access a key ("%s") on a null variable.', $item);
} else {
@@ -1626,7 +1626,7 @@ final class CoreExtension extends AbstractExtension
}
// object property
if (/* Template::METHOD_CALL */ 'method' !== $type) {
if (Template::METHOD_CALL !== $type) {
if (isset($object->$item) || \array_key_exists((string) $item, (array) $object)) {
if ($isDefinedTest) {
return true;
+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;
@@ -37,10 +40,10 @@ class Node implements \Countable, \IteratorAggregate
private array $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)
{
@@ -156,11 +159,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])) {
@@ -180,6 +189,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;
@@ -198,6 +210,9 @@ class Node implements \Countable, \IteratorAggregate
$this->nodes[$name] = $node;
}
/**
* @param string|int $name
*/
public function removeNode(string $name): void
{
unset($this->nodes[$name]);
@@ -208,6 +223,9 @@ class Node implements \Countable, \IteratorAggregate
return \count($this->nodes);
}
/**
* @param string|int $name
*/
public function deprecateNode(string $name, NameDeprecation $dep): void
{
$this->nodeNameDeprecations[$name] = $dep;