Passed
Push — main ( d95c3a...aa9968 )
by Michael
02:47
created

instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
use MichaelRubel\EnhancedContainer\Call;
6
use MichaelRubel\EnhancedContainer\Core\BindingBuilder;
7
8
if (! function_exists('call')) {
9
    /**
10
     * @param string|object $class
11
     * @param array         $parameters
12
     *
13
     * @return mixed
14
     */
15
    function call(string|object $class, array $parameters = []): mixed
16
    {
17 56
        return app(Call::class, [$class, $parameters]);
18
    }
19
}
20
21
if (! function_exists('bind')) {
22
    /**
23
     * @param string|object $abstract
24
     *
25
     * @return BindingBuilder
26
     */
27
    function bind(string|object $abstract): BindingBuilder
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29 29
        return new BindingBuilder($abstract);
30
    }
31
}
32
33
if (! function_exists('singleton')) {
34
    /**
35
     * @param string              $abstract
36
     * @param Closure|string|null $concrete
37
     *
38
     * @return void
39
     */
40
    function singleton(string $abstract, \Closure|string $concrete = null): void
41
    {
42 1
        app()->singleton($abstract, $concrete);
43 1
    }
44
}
45
46
if (! function_exists('scoped')) {
47
    /**
48
     * @param string              $abstract
49
     * @param Closure|string|null $concrete
50
     *
51
     * @return void
52
     */
53
    function scoped(string $abstract, \Closure|string $concrete = null): void
54
    {
55 1
        app()->scoped($abstract, $concrete);
56 1
    }
57
}
58
59
if (! function_exists('extend')) {
60
    /**
61
     * @param string  $abstract
62
     * @param Closure $closure
63
     *
64
     * @return void
65
     */
66
    function extend(string $abstract, \Closure $closure): void
67
    {
68 1
        app()->extend($abstract, $closure);
69 1
    }
70
}
71
72
if (! function_exists('instance')) {
73
    /**
74
     * @param string $abstract
75
     * @param mixed  $instance
76
     *
77
     * @return void
78
     */
79
    function instance(string $abstract, mixed $instance): void
80
    {
81 1
        app()->instance($abstract, $instance);
82 1
    }
83
}
84
85
if (! function_exists('isForwardingEnabled')) {
86
    /**
87
     * @return bool
88
     */
89
    function isForwardingEnabled(): bool
90
    {
91 17
        return (bool) config('enhanced-container.forwarding_enabled');
92
    }
93
}
94
95
if (! function_exists('enableMethodForwarding')) {
96
    /**
97
     * @return void
98
     */
99
    function enableMethodForwarding(): void
100
    {
101 6
        config(['enhanced-container.forwarding_enabled' => true]);
102 6
    }
103
}
104
105
if (! function_exists('disableMethodForwarding')) {
106
    /**
107
     * @return void
108
     */
109
    function disableMethodForwarding(): void
110
    {
111 6
        config(['enhanced-container.forwarding_enabled' => false]);
112 6
    }
113
}
114
115
if (! function_exists('runWithoutForwarding')) {
116
    /**
117
     * @param Closure $closure
118
     *
119
     * @return mixed
120
     */
121
    function runWithoutForwarding(\Closure $closure): mixed
122
    {
123 4
        disableMethodForwarding();
124
125 4
        $callback = $closure();
126
127 4
        enableMethodForwarding();
128
129 4
        return $callback;
130
    }
131
}
132
133
if (! function_exists('runWithForwarding')) {
134
    /**
135
     * @param Closure $closure
136
     *
137
     * @return mixed
138
     */
139
    function runWithForwarding(\Closure $closure): mixed
140
    {
141 1
        enableMethodForwarding();
142
143 1
        $callback = $closure();
144
145 1
        disableMethodForwarding();
146
147 1
        return $callback;
148
    }
149
}
150