Be careful with type hints in callbacks when using array-traverse functions. In some cases, this may silently cause the data type of elements to change.
<?php
declare(strict_types=1);
$unexpected = array_filter(['123', (string) PHP_INT_MAX], fn (int $item) => true);
var_dump($unexpected);
$unexpectedTypecasting = array_map(fn (int $item) => $item, ['123', (string) PHP_INT_MAX]);
var_dump($unexpectedTypecasting);
$unexpectedTypecasting = array_map(fn (string $item) => $item, [123, PHP_INT_MAX]);
var_dump($unexpectedTypecasting);
$unexpectedTypecasting = array_reduce(['123', (string) PHP_INT_MAX], fn (?int $carry, int $item) => $item);
var_dump($unexpectedTypecasting);
$bigIntValue = bcadd((string) PHP_INT_MAX, '1');
$expectedTypeError = array_map(fn (int $item) => $item, [$bigIntValue]);
var_dump($expectedTypeError);
?>
The above example will output (PHP version 8.3.6, error_reporting E_ALL):
<?php
array(2) {
[0]=>
string(3) "123"
[1]=>
string(19) "9223372036854775807"
}
array(2) {
[0]=>
int(123)
[1]=>
int(9223372036854775807)
}
array(2) {
[0]=>
string(3) "123"
[1]=>
string(19) "9223372036854775807"
}
int(9223372036854775807)
Fatal error: Uncaught TypeError: {closure}(): Argument ?>