Skip to content

Commit cbc2219

Browse files
committed
Use Yield instead of regular foreach.
1 parent a93cab3 commit cbc2219

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

spec/drupol/phptree/Node/NodeSpec.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,24 @@ public function it_can_get_its_ancestors()
154154
{
155155
$this
156156
->getAncestors()
157-
->shouldReturn([]);
157+
->shouldYield(new \ArrayIterator([]));
158158

159-
$node1 = new Node();
160-
$node2 = new Node($node1);
161-
$node3 = new Node($node2);
159+
$root = new Node();
160+
$level1 = new Node($root);
161+
$level2 = new Node($level1);
162162

163-
$this->setParent($node3);
163+
$this->setParent($level2);
164164

165165
$this
166166
->getAncestors()
167-
->shouldReturn([$node1, $node2, $node3]);
167+
->shouldYield(new \ArrayIterator([$level2, $level1, $root]));
168168
}
169169

170170
public function it_can_get_its_sibblings()
171171
{
172172
$this
173173
->getSibblings()
174-
->shouldReturn([]);
174+
->shouldYield(new \ArrayIterator([]));
175175

176176
$node1 = new Node();
177177
$node2 = new Node();
@@ -181,7 +181,7 @@ public function it_can_get_its_sibblings()
181181

182182
$this
183183
->getSibblings()
184-
->shouldReturn([$node2, $node3]);
184+
->shouldYield(new \ArrayIterator([$node2, $node3]));
185185
}
186186

187187
public function it_can_use_withChildren()

src/Node/Node.php

+11-21
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,13 @@ public function children(): array
8585
/**
8686
* {@inheritdoc}
8787
*/
88-
public function getAncestors(): array
88+
public function getAncestors(): \Traversable
8989
{
90-
$parents = [];
9190
$node = $this;
92-
while ($parent = $node->getParent()) {
93-
\array_unshift($parents, $parent);
94-
$node = $parent;
95-
}
9691

97-
return $parents;
92+
while ($node = $node->getParent()) {
93+
yield $node;
94+
}
9895
}
9996

10097
/**
@@ -116,26 +113,19 @@ public function isRoot(): bool
116113
/**
117114
* {@inheritdoc}
118115
*/
119-
public function getSibblings(): array
116+
public function getSibblings(): \Traversable
120117
{
121118
$parent = $this->getParent();
122119

123120
if (null === $parent) {
124121
return [];
125122
}
126123

127-
$sibblings = $parent->children();
128-
129-
$node = $this;
130-
131-
return \array_values(
132-
\array_filter(
133-
$sibblings,
134-
function ($child) use ($node) {
135-
return $child !== $node;
136-
}
137-
)
138-
);
124+
foreach ($parent->children() as $child) {
125+
if ($child !== $this) {
126+
yield $child;
127+
}
128+
}
139129
}
140130

141131
/**
@@ -180,6 +170,6 @@ public function withChildren(NodeInterface ...$nodes): NodeInterface
180170
*/
181171
public function depth(): int
182172
{
183-
return \count($this->getAncestors());
173+
return \count(\iterator_to_array($this->getAncestors()));
184174
}
185175
}

src/Node/NodeInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ public function isRoot(): bool;
7777
/**
7878
* Get the ancestors of a node.
7979
*
80-
* @return NodeInterface[]
80+
* @return \Traversable
8181
* The array of ancestors.
8282
*/
83-
public function getAncestors(): array;
83+
public function getAncestors(): \Traversable;
8484

8585
/**
8686
* Get the node's sibblings.
8787
*
88-
* @return NodeInterface[]
88+
* @return \Traversable
8989
* The array of sibblings.
9090
*/
91-
public function getSibblings(): array;
91+
public function getSibblings(): \Traversable;
9292

9393
/**
9494
* The number of children a node has.

0 commit comments

Comments
 (0)