Prepare for php 7.2

* allow `|length` of `null`
* allow `|length` of objects not implementing `\Countable`

* To get no errors when running with PHP 7.2 You need to use phpunit
^6.1, otherwise the `each` function, used by phpunit, triggers errors.
You can do so by running phpunit like:
`SYMFONY_PHPUNIT_VERSION=6.1 phpnightly vendor/bin/simple-phpunit` *
This commit is contained in:
Hidde Boomsma
2017-03-31 12:05:13 +02:00
parent 324ab87d2f
commit d2b7a01a18
3 changed files with 25 additions and 4 deletions
+9 -1
View File
@@ -1264,6 +1264,10 @@ if (function_exists('mb_get_info')) {
*/
function twig_length_filter(Twig_Environment $env, $thing)
{
if (null === $thing) {
return 0;
}
if (is_scalar($thing)) {
return mb_strlen($thing, $env->getCharset());
}
@@ -1272,7 +1276,11 @@ if (function_exists('mb_get_info')) {
return mb_strlen((string) $thing, $env->getCharset());
}
return count($thing);
if ($thing instanceof \Countable || is_array($thing)) {
return count($thing);
}
return 1;
}
/**
@@ -0,0 +1,16 @@
--TEST--
"length" filter
--TEMPLATE--
{{ null|length }}
{{ magic|length }}
{{ non_countable|length }}
--DATA--
return array(
'null' => null, /* triggers deprecation error */
'magic' => new MagicCallStub(), /* used to assert we do *not* call __call, also triggers deprecation */
'non_countable' => new \StdClass(), /* triggers deprecation error */
);
--EXPECT--
0
1
1
@@ -6,7 +6,6 @@
{{ number|length }}
{{ to_string_able|length }}
{{ countable|length }}
{{ magic|length }}
--DATA--
return array(
'array' => array(1, 4),
@@ -14,7 +13,6 @@ return array(
'number' => 1000,
'to_string_able' => new ToStringStub('foobar'),
'countable' => new CountableStub(42), /* also asserts we do *not* call __toString() */
'magic' => new MagicCallStub(), /* used to assert we do *not* call __call */
);
--EXPECT--
2
@@ -22,4 +20,3 @@ return array(
4
6
42
1