Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-3812

PHP: Code completion detects wrong type for typed Iterator in foreach

    XMLWordPrintableJSON

Details

    Description

      When using types on the elements of an Iterator, then the IDE detects the wrong types while iterating via foreach.

      The variable "$item" in the source code is of type "Model" and the IDE should suggest the properties of "Model". But it thinks "$item" is of type \TestIterator and suggests methods and properties of the \TestIterator type.

      Run code completion on $item within the foreach to reproduce this issue.

      class Model
      {
          public $name = "test";
      }
      
      class TestIterator extends \ArrayIterator
      {
          public function __construct(\Model ...$items)
          {
              parent::__construct($items);
          }
      
          public function current(): \Model
          {
              return parent::current();
          }
      
          public function offsetGet($offset): \Model
          {
              return parent::offsetGet($offset);
          }
      
      }
      
      $iterator = new \TestIterator(new Model(), new Model());
      foreach ($iterator as $item)
      {
         # run code completion on $item to reproduce issue
          echo $item;
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            ChrisKM Chris
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: