Passed
Pull Request — 2.x (#263)
by Akihito
05:24 queued 03:52
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
    /** @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
        unset($offset, $value);
67
        throw new LogicException();
68
    }
69
70
    /**
71
     * @param string $offset
72
     *
73
     * @return never
74
     */
75
    #[ReturnTypeWillChange]
76
    public function offsetUnset($offset)
77
    {
78
        unset($offset);
79
        throw new LogicException();
80
    }
81
82
    /** @return Generator<int, object> */
83
    public function getIterator(): Iterator
84
    {
85
        foreach ($this->lazies as $lazy) {
86
            $object = ($lazy)($this->injector);
87
            assert(is_object($object));
88
89
            yield $object;
90
        }
91
    }
92
93
    public function count(): int
94
    {
95
        return count($this->lazies);
96
    }
97
}
98