Skip to content

Commit

Permalink
Use Yield instead of regular foreach.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 11, 2018
1 parent a93cab3 commit cbc2219
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
16 changes: 8 additions & 8 deletions spec/drupol/phptree/Node/NodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ public function it_can_get_its_ancestors()
{
$this
->getAncestors()
->shouldReturn([]);
->shouldYield(new \ArrayIterator([]));

$node1 = new Node();
$node2 = new Node($node1);
$node3 = new Node($node2);
$root = new Node();
$level1 = new Node($root);
$level2 = new Node($level1);

$this->setParent($node3);
$this->setParent($level2);

$this
->getAncestors()
->shouldReturn([$node1, $node2, $node3]);
->shouldYield(new \ArrayIterator([$level2, $level1, $root]));
}

public function it_can_get_its_sibblings()
{
$this
->getSibblings()
->shouldReturn([]);
->shouldYield(new \ArrayIterator([]));

$node1 = new Node();
$node2 = new Node();
Expand All @@ -181,7 +181,7 @@ public function it_can_get_its_sibblings()

$this
->getSibblings()
->shouldReturn([$node2, $node3]);
->shouldYield(new \ArrayIterator([$node2, $node3]));
}

public function it_can_use_withChildren()
Expand Down
32 changes: 11 additions & 21 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,13 @@ public function children(): array
/**
* {@inheritdoc}
*/
public function getAncestors(): array
public function getAncestors(): \Traversable
{
$parents = [];
$node = $this;
while ($parent = $node->getParent()) {
\array_unshift($parents, $parent);
$node = $parent;
}

return $parents;
while ($node = $node->getParent()) {
yield $node;
}
}

/**
Expand All @@ -116,26 +113,19 @@ public function isRoot(): bool
/**
* {@inheritdoc}
*/
public function getSibblings(): array
public function getSibblings(): \Traversable
{
$parent = $this->getParent();

if (null === $parent) {
return [];
}

$sibblings = $parent->children();

$node = $this;

return \array_values(
\array_filter(
$sibblings,
function ($child) use ($node) {
return $child !== $node;
}
)
);
foreach ($parent->children() as $child) {
if ($child !== $this) {
yield $child;
}
}
}

/**
Expand Down Expand Up @@ -180,6 +170,6 @@ public function withChildren(NodeInterface ...$nodes): NodeInterface
*/
public function depth(): int
{
return \count($this->getAncestors());
return \count(\iterator_to_array($this->getAncestors()));
}
}
8 changes: 4 additions & 4 deletions src/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ public function isRoot(): bool;
/**
* Get the ancestors of a node.
*
* @return NodeInterface[]
* @return \Traversable
* The array of ancestors.
*/
public function getAncestors(): array;
public function getAncestors(): \Traversable;

/**
* Get the node's sibblings.
*
* @return NodeInterface[]
* @return \Traversable
* The array of sibblings.
*/
public function getSibblings(): array;
public function getSibblings(): \Traversable;

/**
* The number of children a node has.
Expand Down

0 comments on commit cbc2219

Please sign in to comment.