Instance   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 12
c 0
b 0
f 0
dl 0
loc 56
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A register() 0 4 1
A inject() 0 3 1
A accept() 0 3 1
A __toString() 0 15 3
A setScope() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use function gettype;
8
use function is_object;
9
use function is_scalar;
10
use function sprintf;
11
12
final class Instance implements DependencyInterface, AcceptInterface
13
{
14
    /**
15
     * @param mixed $value
16
     */
17
    public function __construct(public $value)
18
    {
19
    }
20
21
    public function __toString(): string
22
    {
23
        if (is_scalar($this->value)) {
24
            return sprintf(
25
                '(%s) %s',
26
                gettype($this->value),
27
                (string) $this->value
28
            );
29
        }
30
31
        if (is_object($this->value)) {
32
            return '(object) ' . $this->value::class;
33
        }
34
35
        return '(' . gettype($this->value) . ')';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function register(array &$container, Bind $bind): void
42
    {
43
        $index = (string) $bind;
44
        $container[$index] = $bind->getBound();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function inject(Container $container)
51
    {
52
        return $this->value;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @codeCoverageIgnore
59
     */
60
    public function setScope($scope): void
61
    {
62
    }
63
64
    /** @inheritDoc */
65
    public function accept(VisitorInterface $visitor)
66
    {
67
        return $visitor->visitInstance($this->value);
68
    }
69
}
70