interface Stream extends IteratorAggregate
{
/**
* @return Item[]
*/
public function getIterator();
}
interface Stream extends Iterator
{
/**
* @return Item
*/
public function current();
}
/* @var $stream Stream */
foreach ($stream as $item) {
$item->... // works, Item class methods are suggested
}
interface Stream extends Traversable
{}
class InMemoryStream implements Stream, IteratorAggregate
{
public function __construct(Item ...$items)
{
$this->items = $items;
}
// rest of the code goes here
/**
* @return Item[]
*/
public function getIterator()
{
return new ArrayIterator($this->items);
}
}
class DatabaseStream implements Stream, Iterator
{
// rest of the code goes here
public function rewind()
{
$this->statement = $this->connection->prepare('SELECT * FROM items');
$this->statement->execute();
$this->current = $this->statement->fetch(\PDO::FETCH_ASSOC);
$this->key = 0;
}
public function next()
{
$this->current = $this->statement->fetch(\PDO::FETCH_ASSOC);
$this->key++;
}
public function key()
{
return $this->key;
}
public function valid()
{
return false !== $this->current;
}
/**
* @return Item
*/
public function current()
{
return new Item($this->current);
}
}
/* @var $stream Stream */
foreach ($stream as $item) {
$item->... // not enough information for IDEs to suggest type that $stream iterates over.
}
/* @var $stream Stream|A[] */
foreach ($stream as $item) {
$item->... // works
}
// or worse
/* @var $stream DatabaseStream */
foreach ($stream as $item) {
$item->... // works but annotation specifies actual implementation which violates Design by Contract principle
}
/* @var $stream Stream|Iterator|IteratorAggregate */
foreach ($stream as $item) {
$item->...
}
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/77014da1-8ab9-4042-af9d-eb26deaeaf97%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/32811690.k7hq4Amyri%40vulcan.
/**
* @var Item[]
*/
interface Stream extends Traversable
{}
/**
* @iterable Item
*/
interface Stream extends Traversable
{}
public final static Map<String, String> ARRAY_VALUE_PROVIDERS;
static{
ARRAY_VALUE_PROVIDERS = new LinkedHashMap<>();
ARRAY_VALUE_PROVIDERS.put("\\Iterator", "#M#C*.current");
ARRAY_VALUE_PROVIDERS.put("\\Traversable", "#M#C*.__iterator");
ARRAY_VALUE_PROVIDERS.put("\\IteratorAggregate", "#E#M#C*.getIterator");
ARRAY_VALUE_PROVIDERS.put("\\ArrayAccess", "#M#C*.offsetGet");
}
public final static Map<String, String> ARRAY_KEY_PROVIDERS;
static{
ARRAY_KEY_PROVIDERS = new LinkedHashMap<>();
ARRAY_KEY_PROVIDERS.put("\\Iterator", "#M#C*.key");
ARRAY_KEY_PROVIDERS.put("\\IteratorAggregate", "#Y#M#C*.getIterator");
ARRAY_KEY_PROVIDERS.put("\\ArrayAccess", "#M#C*.offsetGet");
}
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/1574651.KF4Rh9aDTK%40vulcan.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/943d4f69-bb55-4b19-90f1-a1a4c889d344%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/39547303.NTl641kpUQ%40vulcan.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CAGOJM6%2BV8Da%3Dc_%3Di2Rq%3D5Vq8uQ6%2BvX7sdnoVeq%2BKZde5of7DEg%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/39547303.NTl641kpUQ%40vulcan.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.
Yes, it needs generics syntax, making it currently intentionally out-of-scope.If you don't mind ugly hacks with unclear semantics that may stop working in the future, you can also use `iterable|User[]`.