I am trying to refactor a use of create_function in an old legacy site that uses propel 1.6 but needs to run on php 7.2.
The original function is
protected static function dumpArray($value)
{
// array
$keys = array_keys($value);
if (
(1 == count($keys) && '0' == $keys[0])
||
(count($keys) > 1 && array_reduce($keys, create_function('$v,$w', 'return (integer) $v + $w;'), 0) == count($keys) * (count($keys) - 1) / 2))
{
$output = array();
foreach ($value as $val) {
$output[] = self::dump($val);
}
return sprintf('[%s]', implode(', ', $output));
}
// mapping
$output = array();
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key), self::dump($val));
}
return sprintf('{ %s }', implode(', ', $output));
}
I’ve refactored
(count($keys) > 1 && array_reduce($keys, create_function('$v,$w', 'return (integer) $v + $w;'), 0) == count($keys) * (count($keys) - 1) / 2))
to
(count($keys) > 1 && array_reduce($keys, function($v,$w){return (integer) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2))
It appears to work BUT i now get warnings because $v+$w doesn’t always return an integer.
Im unsure if I have refactored correctly and if I have how I should deal with the warning.
Thanks.