Passed
Push — visitor ( 2d5566 )
by Akihito
11:02
created

DependencyProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 28
c 0
b 0
f 0
dl 0
loc 97
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isSingleton() 0 3 1
A __toString() 0 5 1
A inject() 0 15 4
A setScope() 0 7 2
A __construct() 0 4 1
A accept() 0 7 1
A __sleep() 0 3 1
A register() 0 3 1
A setContext() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Compiler\CompileVisitor;
0 ignored issues
show
Bug introduced by
The type Ray\Compiler\CompileVisitor 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...
8
9
use function assert;
10
use function sprintf;
11
12
final class DependencyProvider implements DependencyInterface
13
{
14
    /** @var string */
15
    public $context;
16
17
    /**
18
     * Provider dependency
19
     *
20
     * @var Dependency
21
     */
22
    private $dependency;
23
24
    /** @var bool */
25
    private $isSingleton = false;
26
27
    /** @var ?mixed */
28
    private $instance;
29
30
    public function __construct(Dependency $dependency, string $context)
31
    {
32
        $this->dependency = $dependency;
33
        $this->context = $context;
34
    }
35
36
    /**
37
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Ray\Di\list 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...
38
     */
39
    public function __sleep()
40
    {
41
        return ['context', 'dependency', 'isSingleton'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('context', ...ndency', 'isSingleton') returns the type array<integer,string> which is incompatible with the documented return type Ray\Di\list.
Loading history...
42
    }
43
44
    public function __toString(): string
45
    {
46
        return sprintf(
47
            '(provider) %s',
48
            (string) $this->dependency
49
        );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function register(array &$container, Bind $bind): void
56
    {
57
        $container[(string) $bind] = $bind->getBound();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function inject(Container $container)
64
    {
65
        if ($this->isSingleton && $this->instance) {
66
            return $this->instance;
67
        }
68
69
        $provider = $this->dependency->inject($container);
70
        assert($provider instanceof ProviderInterface);
71
        if ($provider instanceof SetContextInterface) {
72
            $this->setContext($provider);
73
        }
74
75
        $this->instance = $provider->get();
76
77
        return $this->instance;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function setScope($scope): void
84
    {
85
        if ($scope !== Scope::SINGLETON) {
86
            return;
87
        }
88
89
        $this->isSingleton = true;
90
    }
91
92
    public function setContext(SetContextInterface $provider): void
93
    {
94
        $provider->setContext($this->context);
95
    }
96
97
    public function isSingleton(): bool
98
    {
99
        return $this->isSingleton;
100
    }
101
102
    public function accept(VisitorInterface $visitor): string
103
    {
104
        assert($visitor instanceof CompileVisitor);
105
106
        return $visitor->visitProvider(
0 ignored issues
show
Bug introduced by
The method visitProvider() does not exist on Ray\Di\VisitorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        return $visitor->/** @scrutinizer ignore-call */ visitProvider(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $this->dependency,
108
            $this->context,
109
        );
110
    }
111
}
112