Passed
Pull Request — 2.x (#263)
by Akihito
04:00 queued 02:02
created

Map::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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