Passed
Push — master ( 414f0a...c0888e )
by Pierre
03:03
created

Invokable::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary;
6
7
use ArrayIterator;
8
use InvalidArgumentException;
9
use Knp\DictionaryBundle\Dictionary;
10
11
/**
12
 * @template E
13
 * @implements Dictionary<E>
14
 */
15
final class Invokable implements Dictionary
16
{
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var bool
24
     */
25
    private $invoked = false;
26
27
    /**
28
     * @var array<mixed, mixed>
29
     */
30
    private $values = [];
31
32
    /**
33
     * @var callable
34
     */
35
    private $callable;
36
37
    /**
38
     * @var mixed[]
39
     */
40
    private $callableArgs = [];
41
42
    /**
43
     * @param mixed[] $callableArgs
44
     */
45 32
    public function __construct(string $name, callable $callable, array $callableArgs = [])
46
    {
47 32
        $this->name         = $name;
48 32
        $this->callable     = $callable;
49 32
        $this->callableArgs = $callableArgs;
50 32
    }
51
52 6
    public function getName(): string
53
    {
54 6
        return $this->name;
55
    }
56
57 11
    public function getValues(): array
58
    {
59 11
        $this->invoke();
60
61 10
        return $this->values;
62
    }
63
64 3
    public function getKeys(): array
65
    {
66 3
        $this->invoke();
67
68 3
        return array_keys($this->values);
69
    }
70
71 2
    public function offsetExists($offset): bool
72
    {
73 2
        $this->invoke();
74
75 2
        return \array_key_exists($offset, $this->values);
76
    }
77
78 5
    public function offsetGet($offset)
79
    {
80 5
        $this->invoke();
81
82 5
        return $this->values[$offset];
83
    }
84
85 2
    public function offsetSet($offset, $value): void
86
    {
87 2
        $this->invoke();
88
89 2
        $this->values[$offset] = $value;
90 2
    }
91
92 2
    public function offsetUnset($offset): void
93
    {
94 2
        $this->invoke();
95
96 2
        unset($this->values[$offset]);
97 2
    }
98
99
    /**
100
     * @return ArrayIterator<mixed, mixed>
101
     */
102 3
    public function getIterator(): ArrayIterator
103
    {
104 3
        $this->invoke();
105
106 3
        return new ArrayIterator($this->values);
107
    }
108
109 2
    public function count(): int
110
    {
111 2
        $this->invoke();
112
113 2
        return \count($this->values);
114
    }
115
116 23
    private function invoke(): void
117
    {
118 23
        if ($this->invoked) {
119 8
            return;
120
        }
121
122 23
        $values = ($this->callable)(...$this->callableArgs);
123
124 23
        if (!\is_array($values)) {
125 1
            throw new InvalidArgumentException(
126 1
                'Dictionary callable must return an array or an instance of ArrayAccess.'
127
            );
128
        }
129
130 22
        $this->values  = $values;
131 22
        $this->invoked = true;
132 22
    }
133
}
134