Completed
Pull Request — 2.x (#237)
by Akihito
01:33
created

NullObjectDependency   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 60
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A inject() 0 9 1
A register() 0 4 1
A setScope() 0 3 1
A toNull() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Koriym\NullObject\NullObject;
8
use Ray\Di\Annotation\ScriptDir;
9
use ReflectionClass;
10
11
use function assert;
12
use function class_exists;
13
use function interface_exists;
14
use function is_string;
15
16
/**
17
 * @codeCoverageIgnore
18
 */
19
final class NullObjectDependency implements DependencyInterface
20
{
21
    /** @var string */
22
    private $interface;
23
24
    /**
25
     * @param class-string $interface
0 ignored issues
show
Documentation introduced by
The doc-type class-string could not be parsed: Unknown type name "class-string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
     */
27
    public function __construct(string $interface)
28
    {
29
        $this->interface = $interface;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __toString(): string
36
    {
37
        return '';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function inject(Container $container)
44
    {
45
        $scriptDir = $container->getInstance('', ScriptDir::class);
46
        assert(is_string($scriptDir));
47
        assert(interface_exists($this->interface));
48
        $class = (new NullObject($scriptDir))($this->interface);
49
50
        return (new ReflectionClass($class))->newInstanceWithoutConstructor();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     *
56
     * @return void
57
     */
58
    public function register(array &$container, Bind $bind)
59
    {
60
        $container[(string) $bind] = $bind->getBound();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function setScope($scope)
67
    {
68
    }
69
70
    public function toNull(): Dependency
71
    {
72
        $nullObjectClass = $this->interface . 'Null';
73
        assert(class_exists($nullObjectClass));
74
        $class = new ReflectionClass($nullObjectClass);
75
76
        return new Dependency(new NewInstance($class, new SetterMethods([])));
77
    }
78
}
79