Narrowing occurred during type inference of ZEND_FETCH_DIM_W
Description
We're developing an ecommerce solution called HikaShop for the Joomla CMS. We've been at it for more than 10 years. Several of our users report a warning happening when upgrading to PHP8 which says: Warning: Narrowing occurred during type inference of ZEND_FETCH_DIM_W. Please file a bug report on bugs.php.net in /xxx/xxx/xxx/administrator/components/com_hikashop/helpers/helper.php on line 1381
You can read the reports here: https://www.hikashop.com/forum/install-update/903846-update-hikashop-php.html
Unfortunately, we're not able to reproduce the warning on our end as it apparently only happens in some rare case. Also, it happens on a line which only has this: include_once($include_file);
It's basically the main line of our main helper file to load all the other files (controllers, models, views, other helpers, etc). So the warning message doesn't help much. We could just add a @ to hide the warning, but since the message says to file a bug report, I'm doing that hoping someone has a better idea.
PHP Version
PHP 8
Operating System
No response
Hi @dstogov
I am trying to reproduce the issue on a new function. I will update you once done.
However, I noticed that with PHP 8.2.12, our app doesn't face this issue. It only happens with PHP 8.2.13 (and only on the first page load).
However, I noticed that with PHP 8.2.12, our app doesn't face this issue. It only happens with PHP 8.2.13 (and only on the first page load).
Same for us, only on the first page load after the file has been edited.
@dstogov I will try to make the bug trigger in a stand alone file
I have some additional pieces of information, which might be useful.
I made this little script to manually compile the file causing an error.
<?php
if (
function_exists('opcache_get_status') &&
($opcache_status = opcache_get_status()) &&
$opcache_status['opcache_enabled']
) {
$result = opcache_compile_file('/var/www/html/app/Http/Controllers/BankController.php');
if ($result) {
echo "File compiled successfully" . PHP_EOL;
} else {
echo "Error while compiling the file" . PHP_EOL;
}
}
?>
I enabled Opcache for CLI, and executed this test script.
➜ business-platform git:(test) ✗ sail php ./public/compile.php
PHP Warning: Narrowing occurred during type inference of ZEND_FETCH_DIM_W. Please file a bug report on https://github.com/php/php-src/issues in /var/www/html/app/Http/Controllers/BankController.php on line 2142
PHP Warning: Narrowing occurred during type inference of ZEND_FETCH_DIM_W. Please file a bug report on https://github.com/php/php-src/issues in /var/www/html/app/Http/Controllers/BankController.php on line 5064
File compiled successfully
You can see that the exceptions are actually warnings, not fatal errors. However these warnings are transformed to ErrorException in our Laravel app. These warnings do not appear when using 8.2.12.
You can also see that the file actually successfully compiled, which is why we don't have any exception on the subsequent requests (unless the file changed).
Ironically, this problem occurs on the messiest file of this project. I've been trying to create a simplified version of this function so that I can share it, but I was not able to so far. I also wasn't able to reproduce the issue with a function written from scratch. I think that in my case, the easiest solution would be to just refactor this messy file.
However, I will not stop there. I will try to compile every file in our project, and see if there is a simpler file that yields the same warning.
EDIT: unfortunately, this file is the only one giving this warning. While it is good news for the project, it is not good news for debugging 😅
@dstogov I managed to come up with a simplified version of the relevant class. I sent it to you via email.
@dstogov I will try to make the bug trigger in a stand alone file
I cannot get it reproduced in a single file in a reasonable amount of time, I'm sorry.
@wblondel your case is fixed in PHP-8.2 and above (the fix should be released in 8.2.14 or 8.2.15).
@casperbakker in case you get the test just put it here. I'll verify if it's fixed as well.
Thank you very much @dstogov !
I'm also getting "Warning: Narrowing occurred during type inference of ZEND_FETCH_DIM_W" and I can consistently reproduce it. I'm currently on PHP 8.2.13. How can I be sure that PHP 8.2.14 or 8.2.15 will fix also my case?
How can I be sure that PHP 8.2.14 or 8.2.15 will fix also my case?
You can't be sure. This warning occurs during load of some function and the name and location of this function are reported in the error message. Just extract the function in a separate PHP file (you don't need to call it), check that the new file causes warning and send it to me.
We got someone else reporting the issue today: https://www.hikashop.com/forum/product-category-display/907547-warning-narrowing-occurred-during-type-inference-of-zend-fetch-dim-w.html With the patch added in 8.1.6, he got the corrected error message, since he was on 8.2.13. I advised him to update to 8.2.14 and he confirmed that the patch made by @dstogov fixes the problem. So it seems the patch covers all the cases reported so far.
Thanks for your work !