php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75627 Warning: Narrowing occurred during type inference with reproduction code
Submitted: 2017-12-05 02:32 UTC Modified: 2017-12-05 20:27 UTC
From: sam dot hemelryk at totaralearning dot com Assigned:
Status: Closed Package: opcache
PHP Version: 7.1.12 OS: OSX + Ubuntu
Private report: No CVE-ID: None
 [2017-12-05 02:32 UTC] sam dot hemelryk at totaralearning dot com
Description:
------------
Several developers upgraded to the latest PHP 7.1.12, and PHP 7.2.0 releases in the last two days and all have encountered the following warning while trying to use our product:

Warning: Narrowing occurred during type inference. Please file a bug report on bugs.php.net

Our code base is very large so rather than try to share the file that generates the warning I produced a simplification of the code that causes the issue, and which can be used to reliably reproduce the issue.
Please see the provided test script code.

To reproduce this: 
* Place the test script into your web root
* Start watching the server logs or config php to display warnings etc
* Visit the script in your browser

We've tested on several versions of PHP and the following is our findings:

* OSX + PHP 7.2.0 === affected
* OSX + PHP 7.1.12 === affected
* OSX + PHP 7.1.12RC5 === fine
* OSX + PHP 7.1.11 === fine
* Ubuntu + PHP 7.2.0 === affected
* Ubuntu + PHP 7.1.12 === affected

Test script:
---------------
<?php

opcache_reset();

function test($capability) {
    $needed = [];
    $caps = (array)$capability;
    foreach ($caps as $cap) {
        if ($cap == 1) {
            unset($needed[$cap][1]);
        } else if (true) {
            $needed[$cap][1] = true;
        }
    }
}

Expected result:
----------------
Importantly no warnings.

Actual result:
--------------
Warning: Narrowing occurred during type inference. Please file a bug report on bugs.php.net in Unknown on line 0

The effect this has in our code base is that during execution the $needed array will magically become null *most* of the time.
Once in a while it will correctly

To see this effect change the test script as follows:
<code>
<?php
opcache_reset();

function test($capability) {
    $needed = [];
    $caps = (array)$capability;
    foreach ($caps as $cap) {
        if ($cap == 1) {
            unset($needed[$cap][1]);
        } else if (true) {
            $needed[$cap][1] = true;
        }
    }
    var_dump($needed);
}

$test = new stdClass();
$test->a = 1;
$test->b = 2;
test($test);
</code>

Now when you call it most of the time it will output the warning and null.
*Occasionally* you will not get the warning, and you will get:

"array(1) { [2]=> array(1) { [1]=> bool(true) } }"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-05 03:52 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2017-12-05 03:52 UTC] laruence@php.net
Please try using this snapshot:

  http://snaps.php.net/php-trunk-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

I believe this has been fixed yesterday....
 [2017-12-05 20:27 UTC] sam dot hemelryk at totaralearning dot com
-Status: Feedback +Status: Closed
 [2017-12-05 20:27 UTC] sam dot hemelryk at totaralearning dot com
Hi Laruence,

Thank you for sharing that.
The link you provided didn't work, but I have compiled both the head of PHP-7.2 (7.2.1), and PHP-7.1 (7.1.14) and can confirm that the issue has been fixed in both.

I've closed this ticket now.

For anyone arriving there this was fixed by https://bugs.php.net/bug.php?id=75608.

Kind regards
Sam Hemelryk
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Mar 09 08:01:24 2021 UTC