forked from composer-unused/composer-unused
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyCollection.php
79 lines (67 loc) · 1.59 KB
/
DependencyCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Icanhazstring\Composer\Unused\Dependency;
use ArrayIterator;
use Closure;
use Countable;
use IteratorAggregate;
/**
* @implements IteratorAggregate<DependencyInterface>
*/
final class DependencyCollection implements IteratorAggregate, Countable
{
/** @var array<DependencyInterface> */
private $items;
/**
* @param array<DependencyInterface> $items
*/
public function __construct(array $items = [])
{
$this->items = $items;
}
/**
* @return ArrayIterator<int, DependencyInterface>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}
public function count(): int
{
return count($this->items);
}
public function add(DependencyInterface $dependency): void
{
$this->items[] = $dependency;
}
/**
* @param Closure $partition
* @return array<DependencyCollection>
*/
public function partition(Closure $partition): array
{
$matches = [];
$noMatches = [];
foreach ($this->items as $item) {
if ($partition($item)) {
$matches[] = $item;
} else {
$noMatches[] = $item;
}
}
return [
new self($matches),
new self($noMatches)
];
}
/**
* @param Closure $fn
* @return DependencyCollection
*/
public function filter(Closure $fn): DependencyCollection
{
return new self(
array_filter($this->items, $fn)
);
}
}