Completed
Push — master ( 164c4c...6efc3b )
by Iman
01:31
created

Facade   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 48
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 16 3
A addMissingDependencies() 0 12 6
1
<?php
2
3
namespace Imanghafoori\SmartFacades;
4
5
use TypeError;
6
use ReflectionMethod;
7
use RuntimeException;
8
use Illuminate\Support\Facades\Facade as LaravelFacade;
9
10
class Facade extends LaravelFacade
11
{
12
    /**
13
     * Handle dynamic, static calls to the object.
14
     *
15
     * @param string $method
16
     * @param array $args
17
     * @return mixed
18
     *
19
     * @throws \RuntimeException
20
     * @throws \ReflectionException
21
     */
22 4
    public static function __callStatic($method, $args)
23
    {
24 4
        $instance = static::getFacadeRoot();
25
26 4
        if (! $instance) {
27
            throw new RuntimeException('A facade root has not been set.');
28
        }
29
30
        try {
31 4
            return $instance->$method(...$args);
32 4
        } catch (TypeError $error) {
33 4
            $params = (new ReflectionMethod($instance, $method))->getParameters();
34 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...
35 4
            return $instance->$method(...$args);
36
        }
37
    }
38
39
    /**
40
     * Adds missing dependencies to the user-provided input.
41
     *
42
     * @param ReflectionParameter[] $parameters
43
     * @param array $inputData
44
     */
45 4
    private static function addMissingDependencies($parameters, array &$inputData)
46
    {
47 4
        foreach ($parameters as $i => $parameter) {
48
            // Injects missing type hinted parameters within the array
49 4
            $class = $parameter->getClass()->name ?? false;
50 4
            if ($class && ! ($inputData[$i] ?? false) instanceof $class) {
51 4
                array_splice($inputData, $i, 0, [self::$app[$class]]);
52 3
            } elseif (! array_key_exists($i, $inputData) && $parameter->isDefaultValueAvailable()) {
53 4
                $inputData[] = $parameter->getDefaultValue();
54
            }
55
        }
56 4
    }
57
}
58