Passed
Push — main ( 63e9c5...5856da )
by Michael
12:39
created

BindingBuilder::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedContainer\Core;
6
7
use MichaelRubel\EnhancedContainer\Bind;
8
use MichaelRubel\EnhancedContainer\Traits\HelpsProxies;
9
10
class BindingBuilder implements Bind
11
{
12
    use HelpsProxies;
13
14
    /**
15
     * @var \Closure|string|array
16
     */
17
    private \Closure|string|array $contextualImplementation;
18
19
    /**
20
     * BindingBuilder constructor.
21
     *
22
     * @param object|string $abstract
23
     */
24
    public function __construct(
25
        private object | string $abstract
26
    ) {
27
        $this->abstract = $this->convertToNamespace($this->abstract);
28
    }
29
30
    /**
31
     * Method binding.
32
     *
33
     * @param string|null  $method
34
     * @param \Closure|null $override
35
     *
36
     * @return $this|null
37
     */
38
    public function method(string $method = null, \Closure $override = null): self|null
0 ignored issues
show
Bug introduced by
The type MichaelRubel\EnhancedContainer\Core\null 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...
39
    {
40
        // try to resolve implementation
41
        // for actual abstract type
42
        $this->resolve();
43
44
        if (is_null($method) || is_null($override)) {
45
            return $this;
46
        }
47
48
        return $this->{$method}($override);
49
    }
50
51
    /**
52
     * Basic "bind".
53
     *
54
     * @param \Closure|string|null $concrete
55
     * @param bool                 $shared
56
     *
57
     * @return $this
58
     */
59
    public function to(\Closure|string $concrete = null, bool $shared = false): self
60
    {
61
        app()->bind($this->abstract, $concrete, $shared);
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Container::bind() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        app()->bind(/** @scrutinizer ignore-type */ $this->abstract, $concrete, $shared);
Loading history...
62
63
        return $this;
64
    }
65
66
    /**
67
     * Basic "bind", binds itself.
68
     *
69
     * @return void
70
     */
71
    public function itself(): void
72
    {
73
        app()->bind($this->abstract);
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Container::bind() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
        app()->bind(/** @scrutinizer ignore-type */ $this->abstract);
Loading history...
74
    }
75
76
    /**
77
     * Singleton.
78
     *
79
     * @param \Closure|string|null $concrete
80
     *
81
     * @return void
82
     */
83
    public function singleton(\Closure|string $concrete = null): void
84
    {
85
        app()->singleton($this->abstract, $concrete);
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Container::singleton() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        app()->singleton(/** @scrutinizer ignore-type */ $this->abstract, $concrete);
Loading history...
86
    }
87
88
    /**
89
     * Scoped instance.
90
     *
91
     * @param \Closure|string|null $concrete
92
     *
93
     * @return void
94
     */
95
    public function scoped(\Closure|string $concrete = null): void
96
    {
97
        app()->scoped($this->abstract, $concrete);
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Container::scoped() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        app()->scoped(/** @scrutinizer ignore-type */ $this->abstract, $concrete);
Loading history...
98
    }
99
100
    /**
101
     * Enables contextual binding.
102
     *
103
     * @return $this
104
     */
105
    public function contextual(\Closure|string|array $implementation): self
106
    {
107
        $this->contextualImplementation = $implementation;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Contextual binding.
114
     *
115
     * @param array|string $concrete
116
     *
117
     * @return void
118
     */
119
    public function for(array|string $concrete): void
120
    {
121
        app()->when($concrete)
122
             ->needs($this->abstract)
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Con...BindingBuilder::needs() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

122
             ->needs(/** @scrutinizer ignore-type */ $this->abstract)
Loading history...
123
             ->give($this->contextualImplementation);
124
    }
125
126
    /**
127
     * Extend the abstract type.
128
     *
129
     * @param \Closure $closure
130
     *
131
     * @return BindingBuilder
132
     */
133
    public function extend(\Closure $closure): self
134
    {
135
        app()->extend($this->abstract, $closure);
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $abstract of Illuminate\Container\Container::extend() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

135
        app()->extend(/** @scrutinizer ignore-type */ $this->abstract, $closure);
Loading history...
136
137
        return $this;
138
    }
139
140
    /**
141
     * Try to resolve an abstract.
142
     *
143
     * @return mixed
144
     */
145
    public function resolve(): mixed
146
    {
147
        return rescue(
148
            fn () => $this->abstract = $this->convertToNamespace(
149
                resolve($this->abstract)
0 ignored issues
show
Bug introduced by
It seems like $this->abstract can also be of type object; however, parameter $name of resolve() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

149
                resolve(/** @scrutinizer ignore-type */ $this->abstract)
Loading history...
150
            ),
151
            null,
152
            false
153
        );
154
    }
155
156
    /**
157
     * Bind the method to the container.
158
     *
159
     * @param string $method
160
     * @param array  $parameters
161
     *
162
     * @return void
163
     */
164
    public function __call(string $method, array $parameters): void
165
    {
166
        app()->bindMethod([
167
            $this->abstract,
168
            $method,
169
        ], current($parameters));
170
    }
171
}
172