Passed
Push — multi_binding ( 69a64b...9592c0 )
by Akihito
01:17
created

Map::count()   A

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 assert;
18
use function count;
19
use function is_object;
20
21
final class Map implements IteratorAggregate, ArrayAccess, Countable
22
{
23
    /** @var array<string, Lazy> $lazies */
24
    private $lazies;
25
26
    /** @var InjectorInterface */
27
    private $injector;
28
29
    /** @param array<string, Lazy> $lazies */
30
    public function __construct(array $lazies, InjectorInterface $injector)
31
    {
32
        $this->lazies = $lazies;
33
        $this->injector = $injector;
34
    }
35
36
    /** @param string $offset */
37
    #[ReturnTypeWillChange]
38
    public function offsetExists($offset): bool
39
    {
40
        return array_key_exists($offset, $this->lazies);
41
    }
42
43
    /**
44
     * @param string $offset
45
     *
46
     * @return mixed
47
     */
48
    #[ReturnTypeWillChange]
49
    public function offsetGet($offset)
50
    {
51
        $lazy = $this->lazies[$offset];
52
53
        /** @var Lazy $lazy */
54
        return $lazy($this->injector);
55
    }
56
57
    /**
58
     * @param string $offset
59
     * @param mixed  $value
60
     *
61
     * @return never
0 ignored issues
show
Bug introduced by
The type Ray\Di\MultiBinding\never 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...
62
     */
63
    #[ReturnTypeWillChange]
64
    public function offsetSet($offset, $value)
65
    {
66
        throw new LogicException();
67
    }
68
69
    /**
70
     * @param string $offset
71
     *
72
     * @return never
73
     */
74
    #[ReturnTypeWillChange]
75
    public function offsetUnset($offset)
76
    {
77
        throw new LogicException();
78
    }
79
80
    /** @return Generator<int, object> */
81
    public function getIterator(): Iterator
82
    {
83
        foreach ($this->lazies as $lazy) {
84
            $object = ($lazy)($this->injector);
85
            assert(is_object($object));
86
87
            yield $object;
88
        }
89
    }
90
91
    public function count(): int
92
    {
93
        return count($this->lazies);
94
    }
95
}
96