Completed
Push — 2.x ( 5c5bb8...ae2cb5 )
by Akihito
20s queued 13s
created

Map::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 3
c 5
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di\MultiBinding;
6
7
use ArrayAccess;
8
use Countable;
9
use Generator;
10
use Iterator;
11
use IteratorAggregate;
12
use LogicException;
13
use Ray\Di\InjectorInterface;
14
use ReturnTypeWillChange;
0 ignored issues
show
Bug introduced by
The type ReturnTypeWillChange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
use function array_key_exists;
17
use function count;
18
19
/**
20
 * @template T
21
 * @implements ArrayAccess<array-key, T>
22
 */
23
final class Map implements IteratorAggregate, ArrayAccess, Countable
24
{
25
    /** @var array<array-key, Lazy> $lazies */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, Lazy> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, Lazy>.
Loading history...
26
    private $lazies;
27
28
    /** @var InjectorInterface */
29
    private $injector;
30
31
    /**
32
     * @param array<array-key, Lazy> $lazies
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, Lazy> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, Lazy>.
Loading history...
33
     */
34
    public function __construct(array $lazies, InjectorInterface $injector)
35
    {
36
        $this->lazies = $lazies;
37
        $this->injector = $injector;
38
    }
39
40
    /**
41
     * @param array-key $offset
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
42
     *
43
     * @codeCoverageIgnore
44
     */
45
    #[ReturnTypeWillChange]
46
    public function offsetExists($offset): bool
47
    {
48
        return array_key_exists($offset, $this->lazies);
49
    }
50
51
    /**
52
     * @param array-key $offset
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
53
     *
54
     * @return T
55
     *
56
     * @codeCoverageIgnore
57
     */
58
    #[ReturnTypeWillChange]
59
    public function offsetGet($offset)
60
    {
61
        /** @var T $instance */
62
        $instance = ($this->lazies[$offset])($this->injector);
63
64
        return $instance;
65
    }
66
67
    /**
68
     * @param array-key $offset
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
69
     * @param mixed     $value
70
     *
71
     * @return never
72
     *
73
     * @codeCoverageIgnore
74
     */
75
    #[ReturnTypeWillChange]
76
    public function offsetSet($offset, $value)
77
    {
78
        unset($offset, $value);
79
80
        throw new LogicException();
81
    }
82
83
    /**
84
     * @param array-key $offset
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
85
     *
86
     * @return never
87
     *
88
     * @codeCoverageIgnore
89
     */
90
    #[ReturnTypeWillChange]
91
    public function offsetUnset($offset)
92
    {
93
        unset($offset);
94
95
        throw new LogicException();
96
    }
97
98
    /** @return Generator<array-key, T, mixed, void> */
99
    public function getIterator(): Iterator
100
    {
101
        foreach ($this->lazies as $key => $lazy) {
102
            /** @var T $object */
103
            $object = ($lazy)($this->injector);
104
105
            yield $key => $object;
106
        }
107
    }
108
109
    public function count(): int
110
    {
111
        return count($this->lazies);
112
    }
113
}
114