File tree 3 files changed +23
-33
lines changed
3 files changed +23
-33
lines changed Original file line number Diff line number Diff line change @@ -154,24 +154,24 @@ public function it_can_get_its_ancestors()
154
154
{
155
155
$ this
156
156
->getAncestors ()
157
- ->shouldReturn ([] );
157
+ ->shouldYield ( new \ ArrayIterator ([]) );
158
158
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 );
162
162
163
- $ this ->setParent ($ node3 );
163
+ $ this ->setParent ($ level2 );
164
164
165
165
$ this
166
166
->getAncestors ()
167
- ->shouldReturn ([ $ node1 , $ node2 , $ node3 ] );
167
+ ->shouldYield ( new \ ArrayIterator ([ $ level2 , $ level1 , $ root ]) );
168
168
}
169
169
170
170
public function it_can_get_its_sibblings ()
171
171
{
172
172
$ this
173
173
->getSibblings ()
174
- ->shouldReturn ([] );
174
+ ->shouldYield ( new \ ArrayIterator ([]) );
175
175
176
176
$ node1 = new Node ();
177
177
$ node2 = new Node ();
@@ -181,7 +181,7 @@ public function it_can_get_its_sibblings()
181
181
182
182
$ this
183
183
->getSibblings ()
184
- ->shouldReturn ( [$ node2 , $ node3 ]);
184
+ ->shouldYield ( new \ ArrayIterator ( [$ node2 , $ node3 ]) );
185
185
}
186
186
187
187
public function it_can_use_withChildren ()
Original file line number Diff line number Diff line change @@ -85,16 +85,13 @@ public function children(): array
85
85
/**
86
86
* {@inheritdoc}
87
87
*/
88
- public function getAncestors (): array
88
+ public function getAncestors (): \ Traversable
89
89
{
90
- $ parents = [];
91
90
$ node = $ this ;
92
- while ($ parent = $ node ->getParent ()) {
93
- \array_unshift ($ parents , $ parent );
94
- $ node = $ parent ;
95
- }
96
91
97
- return $ parents ;
92
+ while ($ node = $ node ->getParent ()) {
93
+ yield $ node ;
94
+ }
98
95
}
99
96
100
97
/**
@@ -116,26 +113,19 @@ public function isRoot(): bool
116
113
/**
117
114
* {@inheritdoc}
118
115
*/
119
- public function getSibblings (): array
116
+ public function getSibblings (): \ Traversable
120
117
{
121
118
$ parent = $ this ->getParent ();
122
119
123
120
if (null === $ parent ) {
124
121
return [];
125
122
}
126
123
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
+ }
139
129
}
140
130
141
131
/**
@@ -180,6 +170,6 @@ public function withChildren(NodeInterface ...$nodes): NodeInterface
180
170
*/
181
171
public function depth (): int
182
172
{
183
- return \count ($ this ->getAncestors ());
173
+ return \count (\iterator_to_array ( $ this ->getAncestors () ));
184
174
}
185
175
}
Original file line number Diff line number Diff line change @@ -77,18 +77,18 @@ public function isRoot(): bool;
77
77
/**
78
78
* Get the ancestors of a node.
79
79
*
80
- * @return NodeInterface[]
80
+ * @return \Traversable
81
81
* The array of ancestors.
82
82
*/
83
- public function getAncestors (): array ;
83
+ public function getAncestors (): \ Traversable ;
84
84
85
85
/**
86
86
* Get the node's sibblings.
87
87
*
88
- * @return NodeInterface[]
88
+ * @return \Traversable
89
89
* The array of sibblings.
90
90
*/
91
- public function getSibblings (): array ;
91
+ public function getSibblings (): \ Traversable ;
92
92
93
93
/**
94
94
* The number of children a node has.
You can’t perform that action at this time.
0 commit comments