Issue Bug Closed

#90786NumberViewHelper: Avoid PHP Warning with (empty) string

Frank Frewer 03/19/20, 13:57 updated 04/01/20, 21:15

Description

From NumberViewHelper:

        $stringToFormat = $renderChildrenClosure();
        return number_format($stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator);

If $stringToFormat is an empty string, number_format will produce a PHP Warning, as it expects the first parameter to be a float value (and not a string).

In my opinion the naming of the variable inside the number_format call is already misleading: $floatToFormat should make it clearer.

I don’t know if it is intentional that an empty string or a string which is not a number produces a PHP Warning, to let the template developer ensure that there is a number.

If not, why not passing the number_format and gives back the unformatted string, e.g. in this – not tested – way:

        $stringToFormat = (string)$renderChildrenClosure();
        $floatToFormat = (float)$stringToFormat;
        if ($stringToFormat === '' || ($stringToFormat !== '0' && $floatToFormat === 0)) {
            return $stringToFormat;
        }
        return number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);

Reviews 2