Completed
Pull Request — master (#9)
by
unknown
04:33
created

Facade::resolveMissingDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Imanghafoori\SmartFacades;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\Facades\Facade as LaravelFacade;
7
use Illuminate\Support\Str;
8
use ReflectionMethod;
9
use RuntimeException;
10
use TypeError;
11
12
class Facade extends LaravelFacade
13
{
14
    protected static $tmpDriver = null;
15
16
    /**
17
     * Get the registered name of the component.
18
     *
19
     * @return string
20
     */
21 7
    protected static function getFacadeAccessor()
22
    {
23 7
        if ($tmp = static::$tmpDriver) {
24
            static::$tmpDriver = null;
25
26
            return $tmp;
27
        }
28
29 7
        return static::class;
30
    }
31
32
    /**
33
     * Temporarily changes the driver, only for the next call.
34
     *
35
     * @param \Closure|string $name
36
     *
37
     * @return string
38
     */
39
    public static function changeProxyTo($name)
40
    {
41
        static::$tmpDriver = $name;
42
43
        return static::class;
44
    }
45
46
    /**
47
     * Temporarily changes the driver, only for the next call.
48
     *
49
     * @param \Closure|string $name
50
     *
51
     * @return string
52
     */
53
    public static function withDriver($name)
54
    {
55
        return static::changeProxyTo($name);
56
    }
57
58
    /**
59
     * Changes the default driver of the facade.
60
     *
61
     * @param \Closure|string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     *
63
     * @return string
64
     */
65 7
    public static function shouldProxyTo($class)
66
    {
67 7
        static::$app->singleton(self::getFacadeAccessor(), $class);
68
69 7
        return static::class;
70
    }
71
72
    /**
73
     * Sets up a listener to be invoked before the actual method call.
74
     *
75
     * @param string $methodName
76
     * @param \Closure|string $listener
77
     */
78 3
    public static function preCall($methodName, $listener)
79
    {
80 3
        $listener = self::makeListener($methodName, $listener);
81
82 3
        Event::listen(self::getPreCallEventName($methodName), $listener);
83 3
    }
84
85
    /**
86
     * Sets up a listener to be invoked after the actual method.
87
     *
88
     * @param string $methodName
89
     * @param \Closure|string $listener
90
     */
91 3
    public static function postCall($methodName, $listener)
92
    {
93 3
        $listener = self::makeListener($methodName, $listener);
94
95 3
        Event::listen(self::getPostCallEventName($methodName), $listener);
96 3
    }
97
98
    /**
99
     * Handle dynamic, static calls to the object.
100
     *
101
     * @param string $method
102
     * @param array $args
103
     * @return mixed
104
     *
105
     * @throws \RuntimeException
106
     * @throws \ReflectionException
107
     */
108 7
    public static function __callStatic($method, $args)
109
    {
110 7
        if (! $instance = static::getFacadeRoot()) {
111
            throw new RuntimeException('A facade root has not been set.');
112
        }
113
114 7
        Event::dispatch(self::getPreCallEventName($method), [$method, $args]);
115
116
        try {
117 7
            $result = $instance->$method(...$args);
118 4
        } catch (TypeError $error) {
119 4
            self::resolveMissingDependencies($instance, $method, $args);
120 4
            $result = $instance->$method(...$args);
121
        }
122
123 6
        Event::dispatch(self::getPostCallEventName($method), [$method, $args, $result]);
124
125 6
        return $result;
126
    }
127
128
    /**
129
     * Adds missing dependencies to the user-provided input.
130
     *
131
     * @param ReflectionParameter[] $parameters
132
     * @param array $inputData
133
     */
134 4
    private static function addMissingDependencies($parameters, array &$inputData)
135
    {
136 4
        foreach ($parameters as $i => $parameter) {
137
            // Injects missing type hinted parameters within the array
138 4
            $class = $parameter->getClass()->name ?? false;
139 4
            if ($class && ! ($inputData[$i] ?? false) instanceof $class) {
140 4
                array_splice($inputData, $i, 0, [self::$app[$class]]);
141 3
            } elseif (! array_key_exists($i, $inputData) && $parameter->isDefaultValueAvailable()) {
142 3
                $inputData[] = $parameter->getDefaultValue();
143
            }
144
        }
145 4
    }
146
147 3
    private static function makeListener(string $method, $listener)
148
    {
149 3
        if (Str::contains($method, '*')) {
150
            // The $_eventName variable is passed to us by laravel
151
            // but we do not need it, because we already know it.
152
            return function ($_eventName, $methodAndArguments) use ($listener) {
153 1
                static::$app->call($listener, $methodAndArguments);
154 1
            };
155
        }
156
157
        return function ($methodName, $args, $result = null) use ($listener) {
158 1
            static::$app->call($listener, [
159 1
                $methodName,
160 1
                $args,
161 1
                $result,
162
            ]);
163 2
        };
164
    }
165
166 7
    private static function getPreCallEventName($methodName) {
167 7
        return 'calling: '.static::class.'@'.$methodName;
168
    }
169
170 6
    private static function getPostCallEventName($methodName) {
171 6
        return 'called: '.static::class.'@'.$methodName;
172
    }
173
174 4
    private static function resolveMissingDependencies($instance, $method, &$args) {
175 4
        $params = (new ReflectionMethod($instance, $method))->getParameters();
176 4
        self::addMissingDependencies($params, $args);
0 ignored issues
show
Documentation introduced by
$params is of type array<integer,object<ReflectionParameter>>, but the function expects a array<integer,object<Ima...s\ReflectionParameter>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177 4
    }
178
}
179