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 - Ignore some cases of unused variables
Summary: Ignore some cases of unused variables
Status: NEW
Alias: None
Product: php
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.0
Hardware: All All
: P3 normal with 1 vote (vote)
Assignee: Tomas Mysik
Depends on:
Reported: 2014-09-04 18:17 UTC by dharkness
Modified: 2016-06-10 10:38 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
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.