@@ -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 /**
0 commit comments