Map::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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