Passed
Push — main ( 32b436...212718 )
by Michael
11:20
created

single()   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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
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
     * @param string|null   $context
13
     *
14
     * @return mixed
15
     */
16
    function call(string|object $class, array $parameters = [], ?string $context = null): mixed
17
    {
18 61
        return app(Call::class, [$class, $parameters, $context]);
19
    }
20
}
21
22
if (! function_exists('bind')) {
23
    /**
24
     * @param string|object $abstract
25
     *
26
     * @return BindingBuilder
27
     */
28
    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...
29
    {
30 31
        return new BindingBuilder($abstract);
31
    }
32
}
33
34
if (! function_exists('singleton')) {
35
    /**
36
     * @param string              $abstract
37
     * @param Closure|string|null $concrete
38
     *
39
     * @return void
40
     */
41
    function singleton(string $abstract, \Closure|string $concrete = null): void
42
    {
43 1
        app()->singleton($abstract, $concrete);
44 1
    }
45
}
46
47
if (! function_exists('scoped')) {
48
    /**
49
     * @param string              $abstract
50
     * @param Closure|string|null $concrete
51
     *
52
     * @return void
53
     */
54
    function scoped(string $abstract, \Closure|string $concrete = null): void
55
    {
56 1
        app()->scoped($abstract, $concrete);
57 1
    }
58
}
59
60
if (! function_exists('extend')) {
61
    /**
62
     * @param string  $abstract
63
     * @param Closure $closure
64
     *
65
     * @return void
66
     */
67
    function extend(string $abstract, \Closure $closure): void
68
    {
69 2
        app()->extend($abstract, $closure);
70 2
    }
71
}
72
73
if (! function_exists('instance')) {
74
    /**
75
     * @param string $abstract
76
     * @param mixed  $instance
77
     *
78
     * @return void
79
     */
80
    function instance(string $abstract, mixed $instance): void
81
    {
82 1
        app()->instance($abstract, $instance);
83 1
    }
84
}
85
86
if (! function_exists('isForwardingEnabled')) {
87
    /**
88
     * @return bool
89
     */
90
    function isForwardingEnabled(): bool
91
    {
92 62
        return (bool) config('enhanced-container.forwarding_enabled');
93
    }
94
}
95
96
if (! function_exists('enableMethodForwarding')) {
97
    /**
98
     * @return void
99
     */
100
    function enableMethodForwarding(): void
101
    {
102 6
        config(['enhanced-container.forwarding_enabled' => true]);
103 6
    }
104
}
105
106
if (! function_exists('disableMethodForwarding')) {
107
    /**
108
     * @return void
109
     */
110
    function disableMethodForwarding(): void
111
    {
112 6
        config(['enhanced-container.forwarding_enabled' => false]);
113 6
    }
114
}
115
116
if (! function_exists('runWithoutForwarding')) {
117
    /**
118
     * @param Closure $closure
119
     *
120
     * @return mixed
121
     */
122
    function runWithoutForwarding(\Closure $closure): mixed
123
    {
124 4
        disableMethodForwarding();
125
126 4
        $callback = $closure();
127
128 4
        enableMethodForwarding();
129
130 4
        return $callback;
131
    }
132
}
133
134
if (! function_exists('runWithForwarding')) {
135
    /**
136
     * @param Closure $closure
137
     *
138
     * @return mixed
139
     */
140
    function runWithForwarding(\Closure $closure): mixed
141
    {
142 1
        enableMethodForwarding();
143
144 1
        $callback = $closure();
145
146 1
        disableMethodForwarding();
147
148 1
        return $callback;
149
    }
150
}
151
152
if (! function_exists('single')) {
153
    /**
154
     * Checks if an array has only a single element.
155
     *
156
     * @param Countable|array $parameters
157
     *
158
     * @return bool
159
     */
160
    function single(\Countable|array $parameters): bool
161
    {
162 7
        return count($parameters) === 1;
163
    }
164
}
165