Completed
Push — master ( 984bc1...bcdfa3 )
by Raed
12s queued 10s
created

CallbackRegistrar::loadCallbacks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use Exception;
6
use Illuminate\Support\Str;
7
use ReflectionClass;
8
use ReflectionMethod;
9
10
class CallbackRegistrar
11
{
12
    /**
13
     * The callbacks' proxies.
14
     *
15
     * @var array
16
     */
17
    protected $proxies;
18
19
    /**
20
     * Create a new CallbacksRegistrar instance.
21
     */
22 112
    public function __construct()
23
    {
24 112
        $this->proxies = [];
25 112
    }
26
27
    /**
28
     * Add a callback proxy from a given name and callable.
29
     *
30
     * @param string $name
31
     * @param callable $callback
32
     *
33
     * @return void
34
     */
35 70
    public function addCallback(string $name, callable $callback)
36
    {
37 70
        $this->proxies['or' . Str::studly($name)] = $callback;
38 70
    }
39
40
    /**
41
     * Load callbacks proxies from a given associative array.
42
     *
43
     * @param array $callbacks
44
     *
45
     * @return void
46
     */
47 70
    public function loadCallbacks(array $callbacks)
48
    {
49 70
        foreach ($callbacks as $key => $callback) {
50 56
            $this->addCallback($key, $callback);
51
        }
52 70
    }
53
54
    /**
55
     * Get or Load callbacks.
56
     *
57
     * If the callbacks parameter is present the callbacks
58
     * will be loaded, otherwise the current callbacks array
59
     * will be returned.
60
     *
61
     * @param array|null $callbacks
62
     *
63
     * @return array|null
64
     */
65 42
    public function callbacks(array $callbacks = null)
66
    {
67 42
        if ($callbacks) {
68 14
            return $this->loadCallbacks($callbacks);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->loadCallbacks($callbacks) targeting LaraCrafts\GeoRoutes\Cal...istrar::loadCallbacks() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->loadCallbacks($callbacks) returns the type void which is incompatible with the documented return type array|null.
Loading history...
69
        }
70
71 28
        return $this->proxies;
72
    }
73
74
    /**
75
     * Parse callbacks from a given class.
76
     *
77
     * This method will use reflection to loop through all of the static
78
     * methods.
79
     *
80
     * @param string $class
81
     *
82
     * @return void
83
     */
84 14
    public function parseCallbacks(string $class)
85
    {
86 14
        $reflection = new ReflectionClass($class);
87 14
        $callbacks = $reflection->getMethods(ReflectionMethod::IS_STATIC);
88
89 14
        foreach ($callbacks as $callback) {
90 14
            $this->addCallback($callback->getName(), $callback->getClosure());
91
        }
92 14
    }
93
94
    /**
95
     * Get/Set the callable for a given callback name.
96
     *
97
     * @param string $name
98
     * @param callable|null $callable
99
     *
100
     * @return mixed
101
     *
102
     * @throws \Exception
103
     */
104 14
    public function callback(string $name, callable $callable = null)
105
    {
106 14
        if (is_callable($callable)) {
107
            return $this->addCallback($name, $callable);
0 ignored issues
show
Bug introduced by
It seems like $callable can also be of type null; however, parameter $callback of LaraCrafts\GeoRoutes\Cal...egistrar::addCallback() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            return $this->addCallback($name, /** @scrutinizer ignore-type */ $callable);
Loading history...
Bug introduced by
Are you sure the usage of $this->addCallback($name, $callable) targeting LaraCrafts\GeoRoutes\Cal...egistrar::addCallback() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108
        }
109
110 14
        if ($this->hasProxy($name)) {
111 14
            return $this->proxies[$name];
112
        }
113
114 14
        if ($this->hasCallback($name)) {
115 14
            return $this->proxies['or' . Str::ucfirst($name)];
116
        }
117
118
        throw new Exception("Undefined callback [$name]");
119
    }
120
121
    /**
122
     * Determine if a given callback exists.
123
     *
124
     * @param string $name
125
     *
126
     * @return boolean
127
     */
128 42
    public function hasCallback(string $name)
129
    {
130 42
        return array_key_exists('or' . Str::ucfirst($name), $this->proxies);
131
    }
132
133
    /**
134
     * Determine if a given proxy exists.
135
     *
136
     * @param string $proxy
137
     *
138
     * @return boolean
139
     */
140 42
    public function hasProxy(string $proxy)
141
    {
142 42
        return array_key_exists($proxy, $this->proxies);
143
    }
144
}
145