Passed
Pull Request — master (#50)
by Marco
02:36
created

Changes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility;
6
7
use ArrayIterator;
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 empty() : self
22
    {
23
        static $empty;
24
25
        if ($empty) {
26
            return $empty;
27
        }
28
29
        $empty = new self();
30
31
        $empty->changes = [];
32
33
        return $empty;
34
    }
35
36
    public static function fromList(Change ...$changes) : self
37
    {
38
        $instance = new self();
39
40
        $instance->changes = $changes;
41
42
        return $instance;
43
    }
44
45
    public function mergeWith(self $other) : self
46
    {
47
        if (! $other->changes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $other->changes of type Roave\BackwardCompatibility\Change[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
            return $this;
49
        }
50
51
        return self::fromList(...$this->changes, ...$other->changes);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     *
57
     * @return ArrayIterator|Change[]
58
     */
59
    public function getIterator() : ArrayIterator
60
    {
61
        return new ArrayIterator($this->changes);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function count() : int
68
    {
69
        return count($this->changes);
70
    }
71
}
72