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 193459 - autocompetion foreach on a class implementing Iterator
Summary: autocompetion foreach on a class implementing Iterator
Status: REOPENED
Alias: None
Product: php
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: All All
: P3 normal (vote)
Assignee: Ondrej Brejla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-15 19:04 UTC by Pimmetje
Modified: 2012-04-25 08:21 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pimmetje 2010-12-15 19:04:41 UTC
When a class implements iterator and i do a foreach Netbeans does not detect the type of the variable i am getting.

example:

<?php
/**
 * Description of GroupList
 */
class Auth_GroupList implements Iterator {
    public function __construct($array) {}

    /**
     * Rewind the iterator
     */
    public function rewind(){}

    /**
     * Return the current User
     *
     * @return Auth_Group
     */
    public function current() {}

    /**
     * @return int Index key
     */
    public function key() {}

    /**
     * Get the next in the list
     */
    public function next() {}

    /**
     * If the current position is valid
     *
     * @return bool
     */
    public function valid() {}
}
?>

When iterate it like 
<?

$groups = Auth_Group::getAllGroups(); //Will return a Auth_GroupList
foreach($groups as $group) { //Group will be of type Auth_Group as returned by current() but Netbeans won't detect this in the autocompetion
    /* @var $group Auth_Group */ //Will fix it partly the autocompletion still gives the methodes on the Auth_GroupList
    $group->someMethodeOnAuth_Group();
}
Comment 1 Ondrej Brejla 2012-04-24 14:35:17 UTC
I'm not able to reproduce that form your snippet. Some classes are missing, etc. Provide whole piece of a code, where I can simply invoke CC to see that it doesn't work, please.
Comment 2 Pimmetje 2012-04-25 08:15:33 UTC
I hope this example is more clear:

<?php

class Auth_Group {
     public function __construct()
     {
         
     }

     
    public function someMethode() {
        //dummy methode should show in the autocompetion
    }
}


/**
 * Description of GroupList
 */
class Auth_GroupList implements Iterator {
    private $var = array();
    private $index = 0;

    public function __construct($array)
    {
        if (is_array($array)) {
            $this->var = $array;
            $this->size = count($array);
        }
    }

    /**
     * Rewind the iterator
     */
    public function rewind(){
        $this->index = 0;
    }

    /**
     * Return the current User
     *
     * @return Auth_Group
     */
    public function current() {
        return $this->var[$this->index];
    }

    /**
     * @return int Index key
     */
    public function key() {
        return $this->index;
    }

    /**
     * Get the next in the list
     */
    public function next() {
        $this->index++;
    }

    /**
     * If the current position is valid
     *
     * @return bool
     */
    public function valid() {
        return ($this->index < $this->size);
    }
}

$array = array(new Auth_Group(), new Auth_Group());

$auth_group = new Auth_GroupList($array);
foreach ($auth_group as $group) {
    //$group-> //Autocompletion here gives the interface of Auth_GroupList
    //$group->current(); //This is not valid in this context

    //This should be the interface of Auth_Group as this is the type returned by current
    //It is possible to force it with 
    /* @var $group Auth_Group */
    $group->someMethode();
    //But than still the interface of the iterator is there
}
Comment 3 Ondrej Brejla 2012-04-25 08:21:19 UTC
Yep, thanks. If you use varDoc, interface methods are still sthere, because the type of "varDocked" variable is merged from all possible types. And one of them is that Auth_GroupList which it gives from the foreach.

It could be handled more clearly, but not sure how it will be difficult to handle this Iterator interface correctly. Will try look at it for next release.