ObjectPriorityList::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Utility\Lists;
5
6
use Countable;
7
use Habemus\Utility\PHPVersion;
8
use InvalidArgumentException;
9
use IteratorAggregate;
10
11
class ObjectPriorityList implements IteratorAggregate, Countable
12
{
13
    use KeyValuePriorityList {
14
        has as private _has;
15
        get as private _get;
16
        set as private _set;
17
        delete as private _delete;
18
    }
19
20
    public function add($object, int $priority): void
21
    {
22
        $this->assertObject($object);
23
        $this->_set($this->objectID($object), $object, $priority);
24
    }
25
26
    public function delete($object): void
27
    {
28
        $this->assertObject($object);
29
        $this->_delete($this->objectID($object));
30
    }
31
32
    public function has($object): bool
33
    {
34
        $this->assertObject($object);
35
        return $this->_has($this->objectID($object));
36
    }
37
38
    private function objectID($object)
39
    {
40
        return spl_object_hash($object);
41
    }
42
43
    private function assertObject($object): void
44
    {
45
        if (!is_object($object)) {
46
            throw new InvalidArgumentException(
47
                sprintf("Expected object. Got: %s", gettype($object))
48
            );
49
        }
50
    }
51
}
52