Issues (94)

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