2amigos / yii2-usuario

Highly customizable and extensible user management, authentication, and authorization Yii2 extension
https://github.com/2amigos/yii2-usuario
Other
295 stars 142 forks source link

Implicit conversion #461

Closed srakl closed 1 year ago

srakl commented 2 years ago

What steps will reproduce the problem?

I'm using PHP 8.1

When i go to the profile setting page, i get this error

Implicit conversion from float 12.75 to int loses precision

Looks like the error is from $timezoneHelper->getAll() located here

 <?= $form
                        ->field($model, 'timezone')
                        ->dropDownList(ArrayHelper::map(
                                                        $timezoneHelper->getAll(), 'offset', 'name'
                                                    ), 
                                                    [
                                                        'prompt' => 'Select Timezone', 
                                                        'options' => [ '' => ['Selected'=>'selected']], 
                                                        'class' => 'custom-select custom-select-lg'
                                                    ]
                                        );
                    ?>

Any ideas on how to fix this? Thanks

maxxer commented 2 years ago

I only see a division here:

https://github.com/2amigos/yii2-usuario/blob/43b2d76ec6a5719c69f0713ffc6a60dcb23467da/src/User/Helper/TimezoneHelper.php#L35

Could it be there's a TZ causing it?

srakl commented 2 years ago

@maxxer I added a print_r($timeZones); die; to line 42 and got this

...
...
...
389 => [
        'timezone' => 'Pacific/Chatham'
        'name' => 'Pacific/Chatham (UTC +12.75)'
        'offset' => 12.75
    ]
...
...
 406 => [
        'timezone' => 'Pacific/Marquesas'
        'name' => 'Pacific/Marquesas (UTC -9.5)'
        'offset' => -9.5
    ]

division of float maybe?

maxxer commented 2 years ago

What is the exact exception you get? I cannot see where it attempts to cast this value to int.

maxxer commented 2 years ago

Maybe? Just a guess, SORT_NUMERIC should do with floats

https://github.com/2amigos/yii2-usuario/blob/43b2d76ec6a5719c69f0713ffc6a60dcb23467da/src/User/Helper/TimezoneHelper.php#L43

srakl commented 2 years ago

@maxxer

It fails in this view

 <?= $form
                        ->field($model, 'timezone')
                        ->dropDownList(ArrayHelper::map(
                                                        $timezoneHelper->getAll(), 'offset', 'name'
                                                    ), 
                                                    [
                                                        'prompt' => 'Select Timezone', 
                                                        'options' => [ '' => ['Selected'=>'selected']], 
                                                        'class' => 'custom-select custom-select-lg'
                                                    ]
                                        );
                    ?>

in ArrayHelper::map() maybe ?

maxxer commented 2 years ago

Unlikely. Out of curiosity, try changing line 35 of TimezoneHelper.php to something like this:

$offset = $date->getOffset() / 60 / 60 * 100; 
srakl commented 2 years ago
$date->getOffset() / 60 / 60 * 100;

Yeah now it works. But the drop-down timezone looks like this. +8 becomes +800 and so on

<select id="profile-timezone" class="custom-select custom-select-lg" name="Profile[timezone]"><option value="">Select Timezone</option><option value="1400">Pacific/Kiritimati (UTC +1400)</option><option value="1300">Pacific/Tongatapu (UTC +1300)</option><option value="1275">Pacific/Chatham (UTC +1275)</option><option value="1200">Pacific/Wallis (UTC +1200)</option><option value="1100">Pacific/Pohnpei (UTC +1100)</option><option value="1050">Australia/Lord_Howe (UTC +1050)</option><option value="1000">Pacific/Saipan (UTC +1000)</option><option value="950">Australia/Darwin (UTC +950)</option><option value="900">Pacific/Palau (UTC +900)</option><option value="875">Australia/Eucla (UTC +875)</option><option value="800">Australia/Perth (UTC +800)</option><option value="700">Indian/Christmas (UTC +700)</option><option value="650">Indian/Cocos (UTC +650)</option><option value="600">Indian/Chagos (UTC +600)</option><option value="575">Asia/Kathmandu (UTC +575)</option><option value="550">Asia/Kolkata (UTC +550)</option><option value="500">Indian/Maldives (UTC +500)</option><option value="450">Asia/Tehran (UTC +450)</option><option value="400">Indian/Reunion (UTC +400)</option><option value="300">Indian/Mayotte (UTC +300)</option><option value="200">Europe/Zurich (UTC +200)</option><option value="100">Europe/London (UTC +100)</option><option value="0">UTC (UTC 0)</option><option value="-100">Atlantic/Cape_Verde (UTC -100)</option><option value="-200">Atlantic/South_Georgia (UTC -200)</option><option value="-250">America/St_Johns (UTC -250)</option><option value="-300">Atlantic/Stanley (UTC -300)</option><option value="-400">America/Tortola (UTC -400)</option><option value="-500">America/Winnipeg (UTC -500)</option><option value="-600">Pacific/Galapagos (UTC -600)</option><option value="-700">America/Whitehorse (UTC -700)</option><option value="-800">Pacific/Pitcairn (UTC -800)</option><option value="-900">Pacific/Gambier (UTC -900)</option><option value="-950">Pacific/Marquesas (UTC -950)</option><option value="-1000">Pacific/Tahiti (UTC -1000)</option><option value="-1100">Pacific/Pago_Pago (UTC -1100)</option></select>
maxxer commented 2 years ago

Look here.

We should manage offset as string instead of float, otherwise it cannot be used as array key. So you were right, the problem happens at ArrayHelper::map. Can you create a pull request?

maxxer commented 2 years ago

Note: when converting the float to string, remember to str_pad the value or sorting will be messed up

maxxer commented 1 year ago

should be closed, if you can have a test run