Passed
Pull Request — 2.x (#263)
by Akihito
02:50 queued 01:27
created

Map::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
c 3
b 0
f 0
dl 0
loc 7
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 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
     * @codeCoverageIgnore
64
     */
65
    #[ReturnTypeWillChange]
66
    public function offsetSet($offset, $value)
67
    {
68
        unset($offset, $value);
69
70
        throw new LogicException();
71
    }
72
73
    /**
74
     * @param string $offset
75
     *
76
     * @return never
77
     *
78
     * @codeCoverageIgnore
79
     */
80
    #[ReturnTypeWillChange]
81
    public function offsetUnset($offset)
82
    {
83
        unset($offset);
84
85
        throw new LogicException();
86
    }
87
88
    /** @return Generator<int, object> */
89
    public function getIterator(): Iterator
90
    {
91
        foreach ($this->lazies as $lazy) {
92
            $object = ($lazy)($this->injector);
93
            assert(is_object($object));
94
95
            yield $object;
96
        }
97
    }
98
99
    public function count(): int
100
    {
101
        return count($this->lazies);
102
    }
103
}
104