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 258418

Summary: Iterator current() method return-type hint not set correctly for autocompletion inside foreach
Product: php Reporter: BlameFate
Component: CodeAssignee: Tomas Mysik <tmysik>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.2   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

Description BlameFate 2016-03-17 15:00:57 UTC
<?php
class IterableSetOfThings implements Iterator{
	protected $things=[],$i=0;
	public function push(Thing $thing):bool{$this->things[]=$thing;return true;}
//	public function expose(){return $this->things;}//For test purposes
//http://stackoverflow.com/questions/20577438/treat-a-php-class-that-implements-iterator-as-an-array
	public function current():Thing{return $this->things[$this->i];}
	public function next(){$this->i++;}
	public function key():int{return $this->i;}
	public function valid():bool{return ($this->i>=0 && $this->i<count($this->things));}
	public function rewind(){$this->i=0;}
}
class Thing{
	protected $string;
	public function __construct(string $thing_string){$this->string=$thing_string;}
	public function testMethod():bool{return true;}
	public function __toString():string{return $this->string;}
}
$things=new IterableSetOfThings();
$things->push(new Thing('test1'));
$things->push(new Thing('test2'));

//WITH THE ABOVE CODE: TRY THIS...

foreach($things AS $thing){
	$thing->
}

//Instead of dropping down a list of auto-completion options including the method, "testMethod()"; we see a list of options for the container/set class, "IterableSetOfThings"!
//NOW TRY THIS: An explicit type-hint...

foreach($things AS $thing){/*@var $thing Thing*/
	$thing->
}

//This time, instead of dropping down a list of options for the Thing class as expected, we see a COMBINED list of options, for the union of all methods of both Thing and IterableSetOfThings!
//Correcting return-type-hinting features for Iterators etc., would make things a lot easier for many people, when using PHP7!
Comment 1 Tomas Mysik 2016-03-22 16:34:30 UTC
Currently unsupported, sorry. Please, use [1] as you do to get (at least) some code-completion.

Thanks for reporting.
[1] /*@var $thing Thing*/
Comment 2 BlameFate 2016-03-22 16:52:52 UTC
I'm not familiar with the process for P3 ENHANCEMENTs to get incorporated into NetBeans.
What would generally be the typical timescale for a feature like this one? A month? Six months? A year? Three to five years? Not at all?
Comment 3 Tomas Mysik 2016-03-22 16:58:58 UTC
No estimate, sorry. However, anyone can attach a patch with a possible implementation, of course.

Thanks.