Test Failed
Pull Request — master (#870)
by Aleksei
08:56
created

Registry::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal\Common;
6
7
use Spiral\Core\Config;
8
9
/**
10
 * @internal
11
 */
12
final class Registry
13
{
14
    /**
15
     * @param array<string, object> $objects
16
     */
17
    public function __construct(
18
        private Config $config,
19
        private array $objects = [],
20
    ) {
21
    }
22
23
    public function set(string $name, object $value): void
24
    {
25
        $this->objects[$name] = $value;
26
    }
27
28
    /**
29
     * @template T
30
     *
31
     * @param class-string<T> $interface
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
32
     *
33
     * @return T
34
     */
35
    public function get(string $name, string $interface): object
36
    {
37
        $className = $this->config->$name;
38
        $result = $this->objects[$name] ?? new $className($this);
39
        \assert($result instanceof $interface);
40
        return $result;
41
    }
42
}
43