Instance   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 14
c 0
b 0
f 0
dl 0
loc 60
rs 10

6 Methods

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