bug #2813 fix key exists check for non ArrayObject objects (xabbuh)

This PR was merged into the 1.x branch.

Discussion
----------

fix key exists check for non ArrayObject objects

fixes #2810

Commits
-------

6df989ba fix key exists check for non ArrayObject objects
This commit is contained in:
Fabien Potencier
2019-01-14 14:56:07 +01:00
2 changed files with 36 additions and 1 deletions
+3 -1
View File
@@ -508,7 +508,9 @@ abstract class Twig_Template implements Twig_TemplateInterface
if (self::METHOD_CALL !== $type) {
$arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
if ((is_array($object) || $object instanceof ArrayAccess) && (isset($object[$arrayItem]) || array_key_exists($arrayItem, $object))) {
if (((is_array($object) || $object instanceof ArrayObject) && (isset($object[$arrayItem]) || array_key_exists($arrayItem, $object)))
|| ($object instanceof ArrayAccess && isset($object[$arrayItem]))
) {
if ($isDefinedTest) {
return true;
}
+33
View File
@@ -418,6 +418,11 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase
[false, null, $methodAndPropObject, 'c', [], $arrayType],
]);
$arrayAccess = new Twig_TemplateArrayAccess();
$tests = array_merge($tests, [
[true, ['foo' => 'bar'], $arrayAccess, 'vars', [], $anyType],
]);
// tests when input is not an array or object
$tests = array_merge($tests, [
[false, null, 42, 'a', [], $anyType, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'],
@@ -720,6 +725,34 @@ class Twig_TemplateMethodAndPropObject
}
}
class Twig_TemplateArrayAccess implements ArrayAccess
{
public $vars = [
'foo' => 'bar',
];
private $children = [];
public function offsetExists($offset)
{
return array_key_exists($offset, $this->children);
}
public function offsetGet($offset)
{
return $this->children[$offset];
}
public function offsetSet($offset, $value)
{
$this->children[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->children[$offset]);
}
}
class Twig_TemplateMagicMethodObject
{
public function __call($method, $arguments)