ObjectPriorityList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A delete() 0 4 1
A assertObject() 0 5 2
A add() 0 4 1
A objectID() 0 3 1
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