Passed
Push — master ( 69fef7...de637e )
by Yaroslav
10:51
created

HasHooks::__callStatic()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
namespace LaravelDuskReporter\Generation;
4
5
use Illuminate\Support\Str;
6
7
trait HasHooks
8
{
9
    protected static array $hookActions = [];
10
11
    public static function addHookAction(string $name, \Closure $callback, int $order = 10)
12
    {
13
        if (!isset(static::$hookActions[$name]) || !is_array(static::$hookActions[$name])) {
14
            static::$hookActions[$name] = [];
15
        }
16
        if (!isset(static::$hookActions[$name][$order]) || !is_array(static::$hookActions[$name][$order])) {
17
            static::$hookActions[$name][$order] = [];
18
        }
19
        static::$hookActions[$name][$order][] = $callback;
20
    }
21
22
    protected function runHookAction(string $name, ...$arguments)
23
    {
24
        if (isset(static::$hookActions[$name]) && is_array(static::$hookActions[$name])) {
25
            sort(static::$hookActions[$name]);
26
            foreach (static::$hookActions[$name] as $orderedActions) {
27
                foreach ($orderedActions as $action) {
28
                    if (is_callable($action)) {
29
                        call_user_func($action, ...$arguments);
30
                    }
31
                }
32
            }
33
        }
34
    }
35
36
    public static function __callStatic(string $name, array $arguments)
37
    {
38
        if (
39
            Str::startsWith($name, 'addAction')
40
            && ($hookAction = Str::camel(Str::after($name, 'addAction')))
41
        ) {
42
            static::addHookAction($hookAction, ...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $callback of LaravelDuskReporter\Gene...sHooks::addHookAction() does not expect variable arguments. ( Ignorable by Annotation )

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

42
            static::addHookAction($hookAction, /** @scrutinizer ignore-type */ ...$arguments);
Loading history...
43
44
            return;
45
        }
46
47
        throw new \BadMethodCallException("Method [$name] not exists");
48
    }
49
}
50