apache / apache/netbeans

[PHP] Wrong auto completion in foreach loop on iterators

Open
#5,663 3 comments 0 reactions 0 assignees View on GitHub
enhancement PHP
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 17

### What happened

If I am iterating with `foreach` over an iterator object Netbeans offers auto completion for the members of the object that provides the iterator. But this is wrong.

Here's a screenshot:
![grafik](https://user-images.githubusercontent.com/1118790/225010645-a6e941b4-75fe-4110-bb3b-87d616f50364.png)

It would be great if Netbeans would offer auto completion for the `foreach` items (as far as I remember this already worked some years ago). But at least the wrong suggestions should be removed.

You will find an example code to reproduce the issue below.

### How to reproduce

```php
items);
}

public function customCollectionMethod() {}
}

class Example
{
private Collection $collection;

public function __construct(array $items = []) {
$this->collection = new Collection($items);
}

/**
* @return CollectionItem[]
*/
public function getCollection(): Collection {
return $this->collection;
}
}

$example = new Example([
new CollectionItem(),
new CollectionItem(),
]);

foreach ($example->getCollection() as $item) {
$item-> // put your cursor here and press Ctrl + Space
}
```

Netbeans offers `getCollection()` and `customCollectionMethod()` for auto completion. This would be correct if I press Ctrl + Space after `$example->getCollection()->`. But when iterating over `$example->getCollection()` the items usually have other properties.

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Debian Linux

### JDK

OpenJDK Runtime Environment 11.0.18+10-post-Debian-1deb11u1

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Start with the supplied PHP reproducer and trigger completion with Ctrl + Space at the cursor after $item->. Compare those suggestions with completion after $example->getCollection()->; done means the foreach item context no longer offers Collection members and instead offers the item members described by the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.