Completed
Push — master ( aa299a...fc4ba1 )
by Iman
01:42
created

Facade::changeProxyTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
            return $tmp;
26
        }
27
28 7
        return static::class;
29
    }
30
31
    /**
32
     * Temporarily changes the driver, only for the next call.
33
     *
34
     * @param \Closure|string $name
35
     *
36
     * @return string
37
     */
38
    public static function changeProxyTo($name)
39
    {
40
        static::$tmpDriver = $name;
41
42
        return static::class;
43
    }
44
45
    /**
46
     * Temporarily changes the driver, only for the next call.
47
     *
48
     * @param \Closure|string $name
49
     *
50
     * @return string
51
     */
52
    public static function withDriver($name)
53
    {
54
        return static::changeProxyTo($name);
55
    }
56
57
    /**
58
     * Changes the default driver of the facade
59
     *
60
     * @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...
61
     *
62
     * @return string
63
     */
64 7
    public static function shouldProxyTo($class)
65
    {
66 7
        static::$app->singleton(self::getFacadeAccessor(), $class);
67
68 7
        return static::class;
69
    }
70
71
    /**
72
     * Sets up a listener to be invoked before the actual method call
73
     *
74
     * @param string $methodName
75
     * @param \Closure|string $listener
76
     */
77 3
    public static function preCall($methodName, $listener)
78
    {
79 3
        $listener = self::makeListener($methodName, $listener);
80
81 3
        Event::listen('calling: '.static::class.'@'.$methodName, $listener);
82 3
    }
83
84
    /**
85
     * Sets up a listener to be invoked after the actual method
86
     *
87
     * @param string $methodName
88
     * @param \Closure|string $listener
89
     */
90 3
    public static function postCall($methodName, $listener)
91
    {
92 3
        $listener = self::makeListener($methodName, $listener);
93
94 3
        Event::listen('called: '.static::class.'@'.$methodName, $listener);
95 3
    }
96
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
        Event::dispatch('calling: '.static::class.'@'.$method, [$method, $args]);
111 7
        $instance = static::getFacadeRoot();
112
113 7
        if (! $instance) {
114
            throw new RuntimeException('A facade root has not been set.');
115
        }
116
117
        try {
118 7
            $result = $instance->$method(...$args);
119 6
            Event::dispatch('called: '.static::class.'@'.$method, [$method, $args, $result]);
120
121 6
            return $result;
122 4
        } catch (TypeError $error) {
123 4
            $params = (new ReflectionMethod($instance, $method))->getParameters();
124 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...
125 4
            $result = $instance->$method(...$args);
126 3
            Event::dispatch('called: '.static::class.'@'.$method, [$method, $args, $result]);
127
128 3
            return $result;
129
        }
130
    }
131
132
    /**
133
     * Adds missing dependencies to the user-provided input.
134
     *
135
     * @param ReflectionParameter[] $parameters
136
     * @param array $inputData
137
     */
138 4
    private static function addMissingDependencies($parameters, array &$inputData)
139
    {
140 4
        foreach ($parameters as $i => $parameter) {
141
            // Injects missing type hinted parameters within the array
142 4
            $class = $parameter->getClass()->name ?? false;
143 4
            if ($class && ! ($inputData[$i] ?? false) instanceof $class) {
144 4
                array_splice($inputData, $i, 0, [self::$app[$class]]);
145 3
            } elseif (! array_key_exists($i, $inputData) && $parameter->isDefaultValueAvailable()) {
146 3
                $inputData[] = $parameter->getDefaultValue();
147
            }
148
        }
149 4
    }
150
151 3
    private static function makeListener(string $method, $listener)
152
    {
153 3
        if (Str::contains($method, '*')) {
154
            // The $_eventName variable is passed to us by laravel
155
            // but we do not need it, because we already know it.
156
            return function ($_eventName, $methodAndArguments) use ($listener) {
157 1
                static::$app->call($listener, $methodAndArguments);
158 1
            };
159
        }
160
161
        return function ($methodName, $args, $result = null) use ($listener) {
162 1
            static::$app->call($listener, [
163 1
                $methodName,
164 1
                $args,
165 1
                $result,
166
            ]);
167 2
        };
168
    }
169
}
170