Skip to content

Commit 6df7e6d

Browse files
committed
feature #4377 Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a Closure) (fabpot)
This PR was merged into the 3.x branch. Discussion ---------- Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a Closure) Instead of restricting callable arguments only in sandbox mode, let's deprecate using functions/arrays as callables. Commits ------- 5e94483 Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a Closure)
2 parents f363ab2 + 5e94483 commit 6df7e6d

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 3.15.0 (2024-XX-XX)
22

3+
* Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a `\Closure`)
34
* Add support for triggering deprecations for future operator precedence changes
45
* Deprecate using the `not` unary operator in an expression with ``*``, ``/``, ``//``, or ``%`` without using explicit parentheses to clarify precedence
56
* Deprecate using the `??` binary operator without explicit parentheses

doc/deprecated.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ Functions/Filters/Tests
290290
* For variadic arguments, use snake-case for the argument name to ease the
291291
transition to 4.0.
292292

293+
* Passing a ``string`` or an ``array`` to Twig callable arguments accepting
294+
arrow functions is deprecated as of Twig 3.15; these arguments will have a
295+
``\Closure`` type hint in 4.0.
296+
293297
Node
294298
----
295299

src/Extension/CoreExtension.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ public static function shuffle(string $charset, $item)
972972
* Sorts an array.
973973
*
974974
* @param array|\Traversable $array
975+
* @param ?\Closure $arrow
975976
*
976977
* @internal
977978
*/
@@ -984,7 +985,7 @@ public static function sort(Environment $env, $array, $arrow = null): array
984985
}
985986

986987
if (null !== $arrow) {
987-
self::checkArrowInSandbox($env, $arrow, 'sort', 'filter');
988+
self::checkArrow($env, $arrow, 'sort', 'filter');
988989

989990
uasort($array, $arrow);
990991
} else {
@@ -1838,6 +1839,8 @@ public static function column($array, $name, $index = null): array
18381839
}
18391840

18401841
/**
1842+
* @param \Closure $arrow
1843+
*
18411844
* @internal
18421845
*/
18431846
public static function filter(Environment $env, $array, $arrow)
@@ -1846,7 +1849,7 @@ public static function filter(Environment $env, $array, $arrow)
18461849
throw new RuntimeError(\sprintf('The "filter" filter expects a sequence/mapping or "Traversable", got "%s".', get_debug_type($array)));
18471850
}
18481851

1849-
self::checkArrowInSandbox($env, $arrow, 'filter', 'filter');
1852+
self::checkArrow($env, $arrow, 'filter', 'filter');
18501853

18511854
if (\is_array($array)) {
18521855
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
@@ -1857,6 +1860,8 @@ public static function filter(Environment $env, $array, $arrow)
18571860
}
18581861

18591862
/**
1863+
* @param \Closure $arrow
1864+
*
18601865
* @internal
18611866
*/
18621867
public static function find(Environment $env, $array, $arrow)
@@ -1865,7 +1870,7 @@ public static function find(Environment $env, $array, $arrow)
18651870
throw new RuntimeError(\sprintf('The "find" filter expects a sequence or a mapping, got "%s".', get_debug_type($array)));
18661871
}
18671872

1868-
self::checkArrowInSandbox($env, $arrow, 'find', 'filter');
1873+
self::checkArrow($env, $arrow, 'find', 'filter');
18691874

18701875
foreach ($array as $k => $v) {
18711876
if ($arrow($v, $k)) {
@@ -1877,6 +1882,8 @@ public static function find(Environment $env, $array, $arrow)
18771882
}
18781883

18791884
/**
1885+
* @param \Closure $arrow
1886+
*
18801887
* @internal
18811888
*/
18821889
public static function map(Environment $env, $array, $arrow)
@@ -1885,7 +1892,7 @@ public static function map(Environment $env, $array, $arrow)
18851892
throw new RuntimeError(\sprintf('The "map" filter expects a sequence or a mapping, got "%s".', get_debug_type($array)));
18861893
}
18871894

1888-
self::checkArrowInSandbox($env, $arrow, 'map', 'filter');
1895+
self::checkArrow($env, $arrow, 'map', 'filter');
18891896

18901897
$r = [];
18911898
foreach ($array as $k => $v) {
@@ -1896,6 +1903,8 @@ public static function map(Environment $env, $array, $arrow)
18961903
}
18971904

18981905
/**
1906+
* @param \Closure $arrow
1907+
*
18991908
* @internal
19001909
*/
19011910
public static function reduce(Environment $env, $array, $arrow, $initial = null)
@@ -1904,7 +1913,7 @@ public static function reduce(Environment $env, $array, $arrow, $initial = null)
19041913
throw new RuntimeError(\sprintf('The "reduce" filter expects a sequence or a mapping, got "%s".', get_debug_type($array)));
19051914
}
19061915

1907-
self::checkArrowInSandbox($env, $arrow, 'reduce', 'filter');
1916+
self::checkArrow($env, $arrow, 'reduce', 'filter');
19081917

19091918
$accumulator = $initial;
19101919
foreach ($array as $key => $value) {
@@ -1915,6 +1924,8 @@ public static function reduce(Environment $env, $array, $arrow, $initial = null)
19151924
}
19161925

19171926
/**
1927+
* @param \Closure $arrow
1928+
*
19181929
* @internal
19191930
*/
19201931
public static function arraySome(Environment $env, $array, $arrow)
@@ -1923,7 +1934,7 @@ public static function arraySome(Environment $env, $array, $arrow)
19231934
throw new RuntimeError(\sprintf('The "has some" test expects a sequence or a mapping, got "%s".', get_debug_type($array)));
19241935
}
19251936

1926-
self::checkArrowInSandbox($env, $arrow, 'has some', 'operator');
1937+
self::checkArrow($env, $arrow, 'has some', 'operator');
19271938

19281939
foreach ($array as $k => $v) {
19291940
if ($arrow($v, $k)) {
@@ -1935,6 +1946,8 @@ public static function arraySome(Environment $env, $array, $arrow)
19351946
}
19361947

19371948
/**
1949+
* @param \Closure $arrow
1950+
*
19381951
* @internal
19391952
*/
19401953
public static function arrayEvery(Environment $env, $array, $arrow)
@@ -1943,7 +1956,7 @@ public static function arrayEvery(Environment $env, $array, $arrow)
19431956
throw new RuntimeError(\sprintf('The "has every" test expects a sequence or a mapping, got "%s".', get_debug_type($array)));
19441957
}
19451958

1946-
self::checkArrowInSandbox($env, $arrow, 'has every', 'operator');
1959+
self::checkArrow($env, $arrow, 'has every', 'operator');
19471960

19481961
foreach ($array as $k => $v) {
19491962
if (!$arrow($v, $k)) {
@@ -1957,11 +1970,17 @@ public static function arrayEvery(Environment $env, $array, $arrow)
19571970
/**
19581971
* @internal
19591972
*/
1960-
public static function checkArrowInSandbox(Environment $env, $arrow, $thing, $type)
1973+
public static function checkArrow(Environment $env, $arrow, $thing, $type)
19611974
{
1962-
if (!$arrow instanceof \Closure && $env->hasExtension(SandboxExtension::class) && $env->getExtension(SandboxExtension::class)->isSandboxed()) {
1975+
if ($arrow instanceof \Closure) {
1976+
return;
1977+
}
1978+
1979+
if ($env->hasExtension(SandboxExtension::class) && $env->getExtension(SandboxExtension::class)->isSandboxed()) {
19631980
throw new RuntimeError(\sprintf('The callable passed to the "%s" %s must be a Closure in sandbox mode.', $thing, $type));
19641981
}
1982+
1983+
trigger_deprecation('twig/twig', '3.15', 'Passing a callable that is not a PHP \Closure as an argument to the "%s" %s is deprecated.', $thing, $type);
19651984
}
19661985

19671986
/**

src/Resources/core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,5 +537,5 @@ function twig_check_arrow_in_sandbox(Environment $env, $arrow, $thing, $type)
537537
{
538538
trigger_deprecation('twig/twig', '3.9', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
539539

540-
return CoreExtension::checkArrowInSandbox($env, $arrow, $thing, $type);
540+
CoreExtension::checkArrow($env, $arrow, $thing, $type);
541541
}

0 commit comments

Comments
 (0)