CallbackList::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Sminnee\CallbackList;
4
5
// @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
6
7
/**
8
 * Manages a list of callbacks that can be called sequentially with {@link call()}
9
 */
10
class CallbackList
11
{
12
    /** @var array<callable> */
13
    private $callbacks = [];
14
15
    /**
16
     * Add a callback to the end of the list
17
     *
18
     * @param callable $callback The callback to call. The arguments from {@link call()} will be passed
19
     * @param string $name An optional name to pass to get() and remove() in future
20
     */
21
    public function add(callable $callback, ?string $name = null): void
22
    {
23
        if ($name !== null) {
24
            $this->callbacks[$name] = $callback;
25
        } else {
26
            $this->callbacks[] = $callback;
27
        }
28
    }
29
30
31
    /**
32
     * Remove a named callback.
33
     *
34
     * @param string $name The name originally passed in add()
35
     * @return bool True if successfully removed, false if not found
36
     */
37
    public function remove(string $name): bool
38
    {
39
        if (!\array_key_exists($name, $this->callbacks)) {
40
            return false;
41
        }
42
43
        unset($this->callbacks[$name]);
44
45
        return true;
46
    }
47
48
49
    /**
50
     * Clear the callback list
51
     */
52
    public function clear(): void
53
    {
54
        $this->callbacks = [];
55
    }
56
57
58
    /**
59
     * Get a named callback
60
     *
61
     * @param string $name name originally passed in add()
62
     * @return callable The callback
63
     */
64
    public function get(string $name): ?callable
65
    {
66
        if (!\array_key_exists($name, $this->callbacks)) {
67
            return null;
68
        }
69
70
        return $this->callbacks[$name];
71
    }
72
73
74
    /**
75
     * Get all callbacks
76
     *
77
     * @return array<callable>
78
     */
79
    public function getAll(): array
80
    {
81
        return \array_values($this->callbacks);
82
    }
83
84
85
    /**
86
     * Call all the callbacks, passing the given arguments to each of them
87
     *
88
     * @param mixed ...$args
89
     * @return array<mixed>
90
     */
91
    public function call(...$args): array
92
    {
93
        /** @var array<mixed> $results */
94
        $results = [];
95
        foreach ($this->callbacks as $callback) {
96
            $results[] = \call_user_func_array($callback, $args);
97
        }
98
99
        return $results;
100
    }
101
102
103
    /**
104
     * Treat CallbackList as a callable
105
     *
106
     * @param mixed ...$args
107
     * @return array<mixed>
108
     */
109
    public function __invoke(...$args): array
110
    {
111
        return \call_user_func_array([$this, 'call'], $args);
112
    }
113
}
114