This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 246880

Summary: Ignore some cases of unused variables
Product: php Reporter: dharkness <dharkness>
Component: EditorAssignee: Tomas Mysik <tmysik>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.0   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:

Description dharkness 2014-09-04 18:17:53 UTC
In addition to issue #246230, there are two more cases where I am forced to declare a variable to receive a value that I don't intend to use. I would like NetBeans not to flag these as a warning.

While they do have workarounds to avoid the hint, it's not acceptable to complicate the code to satisfy the IDE.

1. Iterating over the keys of an array.

    // don't need $value

    foreach ($array as $key => $value) {
        echo $key . PHP_EOL;
    }

Workaround: Use array_keys() to copy the keys to a new array of values.

    foreach (array_keys($array) as $key) {
        echo $key . PHP_EOL;
    }

2. Ignoring some values extracted using list().

    // don't need $id

    list($id, $author, $title) = explode(',',
        '123,Charles Lutwidge Dodgson,Alice in Wonderland');
    echo "$author is the author of $title\n";

Workaround: Assign the result of explode() to a temporary variable and extract only the values you need.

    $info = explode(...);
    $author = $info[1];
    $title = $info[2];
    echo "$author is the author of $title\n";
Comment 1 Ondrej Brejla 2014-09-04 18:46:06 UTC
Makes sense, thanks.
Comment 2 peteford 2014-09-05 08:19:36 UTC
The first case is especially tricky when iterating over object properties, since

    foreach (array_keys($object) as $key) {
        echo $key . PHP_EOL;
    }

doesn't work (the workaround here is even more bloated), while 

    foreach ($object as $key => $value) {
        echo $key . PHP_EOL;
    }

is fine.
Comment 3 dharkness 2014-09-05 20:57:33 UTC
Indeed, but I don't think it matters for the fix. Not using the $value in a foreach should never produce a warning IMHO.