yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Implicit conversion from float in Formatter

Open tomaszkane opened this issue 2 years ago • 3 comments

What steps will reproduce the problem?

echo Yii::$app->formatter->asDuration(0.3);

What is the expected result?

"0 seconds".

What do you get instead?

PHP Deprecated Warning – [yii\base\ErrorException] Implicit conversion from float 0.3 to int loses precision

Additional info

It's similar problem to: https://github.com/yiisoft/yii2-debug/issues/470

Q A
Yii version 2.0.49.3
PHP version 8.2
Operating system KDE Neon

tomaszkane avatar Dec 13 '23 13:12 tomaszkane

asDuration() does not accept float according to phpdoc.

rob006 avatar Dec 13 '23 13:12 rob006

And Yii 2.0.x don't support typed arguments in methods. So asDuration() should do something like this: if is_float $value then (int) $value ?

tomaszkane avatar Dec 13 '23 13:12 tomaszkane

I would rather treat this as user error - if you pass float to method that does not accept float, then it is your fault, not framework.

On the other hand formatters are a bit special and having some extra guards here could be beneficial. We don't need any check, we could add (int) cast before passing value to abs() in https://github.com/yiisoft/yii2/blob/d43341a73ab9ab711c42b74c117e076275d9b8c0/framework/i18n/Formatter.php#L1059

rob006 avatar Dec 13 '23 14:12 rob006