Passed
Push — master ( 42a16a...44846f )
by Andrea Marco
12:16 queued 14s
created

Enums::handleInvoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\Enum;
6
7
use Closure;
8
9
/**
10
 * The global behavior for all enums.
11
 */
12
class Enums
13
{
14
    /**
15
     * The logic to run when an inaccessible enum method is called.
16
     *
17
     * @var ?Closure(class-string $enum, string $name, array<array-key, mixed> $arguments): mixed
18
     */
19
    protected static ?Closure $onStaticCall = null;
20
21
    /**
22
     * The logic to run when an inaccessible case method is called.
23
     *
24
     * @var ?Closure(object $case, string $name, array<array-key, mixed> $arguments): mixed
25
     */
26
    protected static ?Closure $onCall = null;
27
28
    /**
29
     * The logic to run when a case is invoked.
30
     *
31
     * @var ?Closure(object $case, mixed ...$arguments): mixed
32
     */
33
    protected static ?Closure $onInvoke = null;
34
35
    /**
36
     * Set the logic to run when an inaccessible enum method is called.
37
     *
38
     * @param Closure(class-string $enum, string $name, array<array-key, mixed> $arguments): mixed $callback
39
     */
40 2
    public static function onStaticCall(Closure $callback): void
41
    {
42 2
        static::$onStaticCall = $callback;
43
    }
44
45
    /**
46
     * Set the logic to run when an inaccessible case method is called.
47
     *
48
     * @param Closure(object $case, string $name, array<array-key, mixed> $arguments): mixed $callback
49
     */
50 2
    public static function onCall(Closure $callback): void
51
    {
52 2
        static::$onCall = $callback;
53
    }
54
55
    /**
56
     * Set the logic to run when a case is invoked.
57
     *
58
     * @param Closure(object $case, mixed ...$arguments): mixed $callback
59
     */
60 2
    public static function onInvoke(Closure $callback): void
61
    {
62 2
        static::$onInvoke = $callback;
63
    }
64
65
    /**
66
     * Handle the call to an inaccessible enum method.
67
     *
68
     * @param class-string $enum
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
69
     * @param array<array-key, mixed> $arguments
70
     */
71 4
    public static function handleStaticCall(string $enum, string $name, array $arguments): mixed
72
    {
73 4
        return static::$onStaticCall
74 2
            ? (static::$onStaticCall)($enum, $name, $arguments)
75 4
            : $enum::fromName($name)->value();
76
    }
77
78
    /**
79
     * Handle the call to an inaccessible case method.
80
     *
81
     * @param array<array-key, mixed> $arguments
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
82
     */
83 14
    public static function handleCall(object $case, string $name, array $arguments): mixed
84
    {
85
        /** @phpstan-ignore method.notFound */
86 14
        return static::$onCall ? (static::$onCall)($case, $name, $arguments) : $case->resolveMetaAttribute($name);
87
    }
88
89
    /**
90
     * Handle the invocation of a case.
91
     */
92 2
    public static function handleInvoke(object $case, mixed ...$arguments): mixed
93
    {
94
        /** @phpstan-ignore method.notFound */
95 2
        return static::$onInvoke ? (static::$onInvoke)($case, ...$arguments) : $case->value();
96
    }
97
}
98