Issues (101)

src/di/MultiBinding/Map.php (7 issues)

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
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
    /** @var 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...
27
    private $lazies;
28
29
    /** @var InjectorInterface */
30
    private $injector;
31
32
    /**
33
     * @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...
34
     */
35
    public function __construct(array $lazies, InjectorInterface $injector)
36
    {
37
        $this->lazies = $lazies;
38
        $this->injector = $injector;
39
    }
40
41
    /**
42
     * @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...
43
     *
44
     * @codeCoverageIgnore
45
     */
46
    #[ReturnTypeWillChange]
47
    public function offsetExists($offset): bool
48
    {
49
        return array_key_exists($offset, $this->lazies);
50
    }
51
52
    /**
53
     * @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...
54
     *
55
     * @return T
56
     *
57
     * @codeCoverageIgnore
58
     */
59
    #[ReturnTypeWillChange]
60
    public function offsetGet($offset)
61
    {
62
        /** @var T $instance */
63
        $instance = ($this->lazies[$offset])($this->injector);
64
65
        return $instance;
66
    }
67
68
    /**
69
     * @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...
70
     * @param mixed     $value
71
     *
72
     * @return never
73
     *
74
     * @codeCoverageIgnore
75
     */
76
    #[ReturnTypeWillChange]
77
    public function offsetSet($offset, $value)
78
    {
79
        unset($offset, $value);
80
81
        throw new LogicException();
82
    }
83
84
    /**
85
     * @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...
86
     *
87
     * @return never
88
     *
89
     * @codeCoverageIgnore
90
     */
91
    #[ReturnTypeWillChange]
92
    public function offsetUnset($offset)
93
    {
94
        unset($offset);
95
96
        throw new LogicException();
97
    }
98
99
    /** @return Generator<array-key, T, void, void> */
100
    public function getIterator(): Iterator
101
    {
102
        foreach ($this->lazies as $key => $lazy) {
103
            /** @var T $object */
104
            $object = ($lazy)($this->injector);
105
106
            yield $key => $object;
107
        }
108
    }
109
110
    public function count(): int
111
    {
112
        return count($this->lazies);
113
    }
114
}
115