Passed
Push — main ( 8e9bd1...830917 )
by Anatoly
01:38 queued 12s
created

ObjectCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 2
b 0
f 0
dl 0
loc 75
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getItemClassName() 0 10 2
A all() 0 3 1
A add() 0 13 2
A get() 0 3 1
A isEmpty() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
namespace Sunrise\Hydrator;
13
14
/**
15
 * Import classes
16
 */
17
use InvalidArgumentException;
18
use RuntimeException;
19
20
/**
21
 * Import functions
22
 */
23
use function sprintf;
24
25
/**
26
 * ObjectCollection
27
 */
28
abstract class ObjectCollection implements ObjectCollectionInterface
29
{
30
31
    /**
32
     * The type of objects in the collection
33
     *
34
     * @var class-string
35
     */
36
    public const T = null;
37
38
    /**
39
     * The collection objects
40
     *
41
     * @var array<array-key, object>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, object> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, object>.
Loading history...
42
     */
43
    private $objects = [];
44
45
    /**
46
     * {@inheritdoc}
47
     *
48
     * @throws RuntimeException
49
     *         If the called class doesn't contain the T constant.
50
     */
51 4
    final public function getItemClassName() : string
52
    {
53 4
        if (null === static::T) {
0 ignored issues
show
introduced by
The condition null === static::T is always true.
Loading history...
54 1
            throw new RuntimeException(sprintf(
55 1
                'The %s collection must contain the T constant.',
56 1
                static::class
57
            ));
58
        }
59
60 3
        return static::T;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    final public function add($key, object $object) : void
67
    {
68 4
        $type = $this->getItemClassName();
69
70 3
        if (!($object instanceof $type)) {
71 1
            throw new InvalidArgumentException(sprintf(
72 1
                'The %s collection can contain the %s objects only.',
73 1
                static::class,
74 1
                $type
75
            ));
76
        }
77
78 2
        $this->objects[$key] = $object;
79 2
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    final public function get($key) : ?object
85
    {
86 1
        return $this->objects[$key] ?? null;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    final public function all() : array
93
    {
94 1
        return $this->objects;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    final public function isEmpty() : bool
101
    {
102 1
        return [] === $this->objects;
103
    }
104
}
105