mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-17 21:07:18 +00:00
minor #1475 Improved exception message while trying to access an attribute from an empty array (phansys, tucksaun)
This PR was merged into the 1.16-dev branch. Discussion ---------- Improved exception message while trying to access an attribute from an empty array Rebased #1439 in master and updated twig extension Commits -------759a77d[Ext] updating Twig extension for previous commit15ce450Improved exception message while trying to access an attribute from an empty array.
This commit is contained in:
+10
-2
@@ -830,7 +830,11 @@ PHP_FUNCTION(twig_template_get_attributes)
|
||||
} elseif (is_object($object)) {
|
||||
$message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface', $item, get_class($object));
|
||||
} elseif (is_array($object)) {
|
||||
$message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
|
||||
if (empty($object)) {
|
||||
$message = sprintf('Key "%s" does not exist as the array is empty', $arrayItem);
|
||||
} else {
|
||||
$message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
|
||||
}
|
||||
} elseif (Twig_Template::ARRAY_CALL === $type) {
|
||||
$message = sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
|
||||
} else {
|
||||
@@ -845,7 +849,11 @@ PHP_FUNCTION(twig_template_get_attributes)
|
||||
} else if (Z_TYPE_P(object) == IS_OBJECT) {
|
||||
TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to access a key \"%s\" on an object of class \"%s\" that does not implement ArrayAccess interface", item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
|
||||
} else if (Z_TYPE_P(object) == IS_ARRAY) {
|
||||
TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC));
|
||||
if (0 == zend_hash_num_elements(Z_ARRVAL_P(object))) {
|
||||
TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" does not exist as the array is empty", item);
|
||||
} else {
|
||||
TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC));
|
||||
}
|
||||
} else {
|
||||
char *type_name = zend_zval_type_name(object);
|
||||
Z_ADDREF_P(object);
|
||||
|
||||
@@ -379,7 +379,11 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
} elseif (is_object($object)) {
|
||||
$message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface', $item, get_class($object));
|
||||
} elseif (is_array($object)) {
|
||||
$message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
|
||||
if (empty($object)) {
|
||||
$message = sprintf('Key "%s" does not exist as the array is empty', $arrayItem);
|
||||
} else {
|
||||
$message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
|
||||
}
|
||||
} elseif (Twig_Template::ARRAY_CALL === $type) {
|
||||
$message = sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
|
||||
} else {
|
||||
|
||||
@@ -28,6 +28,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$context = array(
|
||||
'string' => 'foo',
|
||||
'empty_array' => array(),
|
||||
'array' => array('foo' => 'foo'),
|
||||
'array_access' => new Twig_TemplateArrayAccessObject(),
|
||||
'magic_exception' => new Twig_TemplateMagicPropertyObjectWithException(),
|
||||
@@ -46,10 +47,12 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
$tests = array(
|
||||
array('{{ string["a"] }}', 'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1', false),
|
||||
array('{{ empty_array["a"] }}', 'Key "a" does not exist as the array is empty in "%s" at line 1', false),
|
||||
array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
|
||||
array('{{ array_access["a"] }}', 'Key "a" in object with ArrayAccess of class "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
|
||||
array('{{ string.a }}', 'Impossible to access an attribute ("a") on a string variable ("foo") in "%s" at line 1', false),
|
||||
array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1', false),
|
||||
array('{{ empty_array.a }}', 'Key "a" does not exist as the array is empty in "%s" at line 1', false),
|
||||
array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
|
||||
array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1', false),
|
||||
array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
|
||||
@@ -368,7 +371,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
$tests = array_merge($tests, array(
|
||||
array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42")'),
|
||||
array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string")'),
|
||||
array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'),
|
||||
array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty'),
|
||||
));
|
||||
|
||||
// add twig_template_get_attributes tests
|
||||
|
||||
Reference in New Issue
Block a user