#84485misuse as closure of callable-typed $callback in ArrayUtility::filterRecursive
Description
The method \TYPO3\CMS\Core\Utility\ArrayUtility::filterRecursive declares its $callback argument as of type callable, but uses it as a \Closure via $callback(...).
Current State
public static function filterRecursive(array $array, callable $callback = null): array
// ...
foreach ($array as $key => $value) {
// ...
if (!$callback($value)) { // <--- used as a \Closure, but declared as a callable
unset($array[$key]);
}
}
// ...
}
Possible Strategies
We could …- either change the
filterRecursive-methods signature to..., \Closure $callback..., or … - we fix the use of the
$callbackargument in the method-body.
I decided to go for the latter (2.) and fix the use of the $callback argument in the method-body, as it seems to be quite common in the core to use callable type, instead of \Closure. The fixed line will then contain a call_user_func($callback, $value)-function call.
Proposed Solution
public static function filterRecursive(array $array, callable $callback = null): array
// ...
foreach ($array as $key => $value) {
// ...
if (!call_user_func($callback, $value)) { // <--- now used and declared as a callable
unset($array[$key]);
}
}
// ...
}
Conclusion
Fixing the use of $callback in the static \TYPO3\CMS\Core\Utility\ArrayUtility::filterRecursive method, won’t harm existing installations. Adding an appropriate unit-test ensures the correct behaviour.
Therefore I’ll create a PR to fix this issue with the proposed solution and I’ll add an appropriate test to \TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest too.
A quick search over the core-sources did not reveal any other misuses of callable-types as closure. But do not bet on me, as I just took 5 minutes to identify potential candidates.