Implicit conversion from float in Formatter
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 |
asDuration() does not accept float according to phpdoc.
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 ?
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