Skip to content

Commit 7f613ee

Browse files
committed
Container::getComponents() returns array when $deep is false (BC break)
1 parent f204540 commit 7f613ee

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

src/ComponentModel/Container.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* ComponentContainer is default implementation of IContainer.
1717
*
18-
* @property-read \Iterator $components
18+
* @property-read IComponent[] $components
1919
*/
2020
class Container extends Component implements IContainer
2121
{
@@ -179,20 +179,22 @@ protected function createComponent(string $name): ?IComponent
179179

180180
/**
181181
* Iterates over descendants components.
182-
* @return \Iterator<int|string,IComponent>
182+
* @return iterable<int|string,IComponent>
183183
*/
184-
final public function getComponents(bool $deep = false, ?string $filterType = null): \Iterator
184+
final public function getComponents(bool $deep = false, ?string $filterType = null): iterable
185185
{
186-
$iterator = new RecursiveComponentIterator($this->components);
187186
if ($deep) {
187+
$iterator = new RecursiveComponentIterator($this->components);
188188
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
189+
if ($filterType) {
190+
$iterator = new \CallbackFilterIterator($iterator, fn($item) => $item instanceof $filterType);
191+
}
192+
return $iterator;
189193
}
190194

191-
if ($filterType) {
192-
$iterator = new \CallbackFilterIterator($iterator, fn($item) => $item instanceof $filterType);
193-
}
194-
195-
return $iterator;
195+
return $filterType
196+
? array_filter($this->components, fn($item) => $item instanceof $filterType)
197+
: $this->components;
196198
}
197199

198200

src/ComponentModel/IContainer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function getComponent(string $name): ?IComponent;
3636

3737
/**
3838
* Iterates over descendants components.
39-
* @return \Iterator<int|string,IComponent>
39+
* @return iterable<int|string,IComponent>
4040
*/
41-
function getComponents(): \Iterator;
41+
function getComponents(): iterable;
4242
}

src/ComponentModel/RecursiveComponentIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function hasChildren(): bool
3030
*/
3131
public function getChildren(): self
3232
{
33-
return $this->current()->getComponents();
33+
return new self($this->current()->getComponents());
3434
}
3535

3636

tests/ComponentModel/Container.getComponents.phpt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,13 @@ Assert::same([
3838
'one',
3939
'two',
4040
'button1',
41-
], array_keys(iterator_to_array($list)));
42-
41+
], array_keys($list));
4342

4443
// Filter
4544
$list = $c->getComponents(false, Button::class);
4645
Assert::same([
4746
'button1',
48-
], array_keys(iterator_to_array($list)));
49-
50-
51-
// RecursiveIteratorIterator
52-
$list = new RecursiveIteratorIterator($c->getComponents(), 1);
53-
Assert::same([
54-
'one',
55-
'inner',
56-
'inner2',
57-
'button2',
58-
'two',
59-
'button1',
60-
], array_keys(iterator_to_array($list)));
47+
], array_keys($list));
6148

6249

6350
// Recursive

tests/ComponentModel/Container.zeroname.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ Assert::same('0', $container->getComponent('0')->getName());
2121
$container->addComponent($c1 = new Container, '1', '0');
2222
Assert::same(
2323
[1 => $c1, 0 => $c0],
24-
(array) $container->getComponents(),
24+
$container->getComponents(),
2525
);

0 commit comments

Comments
 (0)