Skip to content

Commit

Permalink
Add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Apr 15, 2023
1 parent b5c6aea commit fb1b6a5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Standard implements IteratorAggregate, Countable
*
* @var ?iterable
*/
private $pipeline;
private ?iterable $pipeline;

/**
* Contructor with an optional source of data.
Expand Down Expand Up @@ -289,14 +289,15 @@ public function map(?callable $func = null): self
}

// Let's check what we got for a start.
$this->pipeline = $func();
$value = $func();

// Generator is a generator, moving along
if ($this->pipeline instanceof Generator) {
if ($value instanceof Generator) {
// It is possible to detect if callback is a generator like so:
// (new \ReflectionFunction($func))->isGenerator();
// Yet this will restrict users from replacing the pipeline and has unknown performance impact.
// But, again, we could add a direct internal method to replace the pipeline, e.g. as done by unpack()
$this->pipeline = $value;

return $this;
}
Expand All @@ -305,7 +306,7 @@ public function map(?callable $func = null): self
// We do not cast to an array here because casting a null to an array results in
// an empty array; that's surprising and not how it works for other values.
$this->pipeline = [
$this->pipeline,
$value,
];

return $this;
Expand Down

0 comments on commit fb1b6a5

Please sign in to comment.