Skip to content

Bug: number_to_currency error on NumberFormatter::setAttribute() using default parameter values #5452

@thiekus

Description

@thiekus

PHP Version

8.1

CodeIgniter4 Version

4.1.5

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

No response

What happened?

When use number_to_currency() in PHP 8.1, it will throw NumberFormatter::setAttribute(): Passing null to parameter #2 ($value) of type int|float is deprecated exceptions. It doesn't occur on PHP 8.0.13 and older, much like problem discussed in #5371 because of PHP 8.1 disallow null type on non-nullable. It makes problem since $fraction is null by default and NumberFormatter::setAttribute() won't accept null variables.

Steps to Reproduce

Simply call to number_to_currency() with default value

<?php

namespace App\Modules\Home\Controllers;

use App\Controllers\BaseController;

class Home extends BaseController
{
    public function index()
    {
        echo number_to_currency(1000000.00, 'IDR');
    }
}

Expected Output

It should handle currency format as expected.

Anything else?

Here's partial log trace of problematic functions

{
    trace: [
        {
            "function": "errorHandler",
            "class": "CodeIgniter\\Debug\\Exceptions",
            "type": "->"
        },
        {
            "file": "/home/thiekus/web/redacted/system/Helpers/number_helper.php",
            "line": 146,
            "function": "setAttribute",
            "class": "NumberFormatter",
            "type": "->"
        },
        {
            "file": "/home/thiekus/web/redacted/system/Helpers/number_helper.php",
            "line": 124,
            "function": "format_number"
        }
    ]
}

Metadata

Metadata

Assignees

Labels

bugVerified issues on the current code behavior or pull requests that will fix them

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions