Issues (61)

src/di/DependencyProvider.php (2 issues)

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