Skip to content

Commit

Permalink
bug #2462 Prepare for php 7.2 (hboomsma)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

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`*

Commits
-------

d2b7a01 Prepare for php 7.2
  • Loading branch information
fabpot committed May 11, 2017
2 parents cf93147 + d2b7a01 commit 537db19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/Twig/Extension/Core.php
Expand Up @@ -1264,6 +1264,10 @@ function _twig_escape_html_attr_callback($matches)
*/
function twig_length_filter(Twig_Environment $env, $thing)
{
if (null === $thing) {
return 0;
}

if (is_scalar($thing)) {
return mb_strlen($thing, $env->getCharset());
}
Expand All @@ -1272,7 +1276,11 @@ function twig_length_filter(Twig_Environment $env, $thing)
return mb_strlen((string) $thing, $env->getCharset());
}

return count($thing);
if ($thing instanceof \Countable || is_array($thing)) {
return count($thing);
}

return 1;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/Twig/Tests/Fixtures/filters/length.legacy.test
@@ -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
3 changes: 0 additions & 3 deletions test/Twig/Tests/Fixtures/filters/length.test
Expand Up @@ -6,20 +6,17 @@
{{ number|length }}
{{ to_string_able|length }}
{{ countable|length }}
{{ magic|length }}
--DATA--
return array(
'array' => array(1, 4),
'string' => 'foo',
'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
3
4
6
42
1

0 comments on commit 537db19

Please sign in to comment.