Completed
Push — master ( 7e0f99...47534f )
by Marco
10s
created

Changes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeWith() 0 7 1
A __construct() 0 2 1
A new() 0 3 1
A getIterator() 0 3 1
A withAddedChange() 0 5 1
A fromArray() 0 6 1
A count() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare;
5
6
use ArrayIterator;
7
use Assert\Assert;
8
use Countable;
9
use IteratorAggregate;
10
use function count;
11
12
final class Changes implements IteratorAggregate, Countable
13
{
14
    /** @var Change[] */
15
    private $changes = [];
16
17
    private function __construct()
18
    {
19
    }
20
21
    public static function new(): self
22
    {
23
        return new self();
24
    }
25
26
    public static function fromArray(array $changes): self
27
    {
28
        Assert::that($changes)->all()->isInstanceOf(Change::class);
29
        $instance = self::new();
30
        $instance->changes = $changes;
31
        return $instance;
32
    }
33
34
    public function mergeWith(self $other) : self
35
    {
36
        $instance = new self();
37
38
        $instance->changes = array_merge($this->changes, $other->changes);
39
40
        return $instance;
41
    }
42
43
    public function withAddedChange(Change $change): self
44
    {
45
        $new = clone $this;
46
        $new->changes[] = $change;
47
        return $new;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     *
53
     * @return Change[]
54
     */
55
    public function getIterator() : ArrayIterator
56
    {
57
        return new ArrayIterator($this->changes);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ArrayIterator($this->changes) returns the type ArrayIterator which is incompatible with the documented return type Roave\ApiCompare\Change[].
Loading history...
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function count() : int
64
    {
65
        return count($this->changes);
66
    }
67
}
68