Completed
Pull Request — master (#9)
by
unknown
02:59 queued 01:49
created

Facade::preCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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