Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.28.1 (2026-XX-XX)

* Fix array access with a `Stringable` key coercing the key to string for `ArrayAccess` objects that use object keys (such as `SplObjectStorage`)
* Fix duplicated macro argument names triggering a PHP fatal error instead of a `SyntaxError`

# 3.28.0 (2026-07-03)
Expand Down
12 changes: 7 additions & 5 deletions src/Node/Expression/GetAttrExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function compile(Compiler $compiler): void
->raw($var)
->raw('[')
;
$this->compileArrayKey($compiler);
$this->compileArrayKey($compiler, $var);
$compiler->raw('] ?? null) : null)');

return;
Expand All @@ -91,7 +91,7 @@ public function compile(Compiler $compiler): void
->raw($var)
->raw('[')
;
$this->compileArrayKey($compiler);
$this->compileArrayKey($compiler, $var);
$compiler->raw('] ?? null) : ');
}

Expand Down Expand Up @@ -177,9 +177,11 @@ public function getStringCoercedChildNames(): array

/**
* Coerces a Stringable array key to string so the optimized path matches
* CoreExtension::getAttribute(); scalars are left to PHP's native offset coercion.
* CoreExtension::getAttribute(): only arrays coerce the key, while ArrayAccess
* objects (e.g. SplObjectStorage) receive it untouched. Scalars are left to
* PHP's native offset coercion.
*/
private function compileArrayKey(Compiler $compiler): void
private function compileArrayKey(Compiler $compiler, string $var): void
{
$attribute = $this->getNode('attribute');

Expand All @@ -193,7 +195,7 @@ private function compileArrayKey(Compiler $compiler): void
$compiler
->raw('(('.$key.' = ')
->subcompile($attribute)
->raw(') instanceof \Stringable ? (string) '.$key.' : '.$key.')')
->raw(') instanceof \Stringable && is_array('.$var.') ? (string) '.$key.' : '.$key.')')
;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ public static function getStrictVariablesModes(): iterable
yield 'strict' => [true];
}

/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testArrayAccessWithObjectKeyKeepsTheObjectKey(bool $strict)
{
$twig = new Environment(new ArrayLoader(['index' => '{{ data[object] }}']), [
'strict_variables' => $strict,
'autoescape' => false,
]);

$object = new class implements \Stringable {
public function __toString(): string
{
return 'string';
}
};
$data = new \SplObjectStorage();
$data[$object] = 'value';

$this->assertSame('value', $twig->render('index', ['data' => $data, 'object' => $object]));
}

public function testArrayAccessWithStringableKeyIsCheckedBySandbox()
{
$object = new class implements \Stringable {
Expand Down
Loading