Passed
Push — master ( f62ea3...7ec3d6 )
by Johnny
06:28
created

EventEmitter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 42
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A emit() 0 11 3
A on() 0 9 2
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Johnny Mast <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Events;
12
13
/**
14
 * EventEmitter trait
15
 *
16
 * This trait allows the Rivescript-php core
17
 * to communicate with the developer using the
18
 * interpreter.
19
 *
20
 * PHP version 7.4 and higher.
21
 *
22
 * @category Core
23
 * @package  Events
24
 * @author   Johnny Mast <[email protected]>
25
 * @license  https://opensource.org/licenses/MIT MIT
26
 * @link     https://github.com/axiom-labs/rivescript-php
27
 * @since    0.4.0
28
 */
29
trait EventEmitter
30
{
31
    protected array $registered = [];
32
33
    /**
34
     * Receive a message.
35
     *
36
     * @param string $event   The event string.
37
     * @param mixed  $handler The event handler.
38
     *
39
     * @return self
40
     */
41
    public function on(string $event, $handler): self
42
    {
43
        if (!isset($this->registered[$event])) {
44
            $this->registered = [];
45
        }
46
47
        $this->registered[$event][] = $handler;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Send out a message.
54
     *
55
     * @param string $event       The event string.
56
     * @param        ...$userdata Arguments for the callback.
0 ignored issues
show
Bug introduced by
The type Axiom\Rivescript\Events\Arguments was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     *
58
     * @return self
59
     */
60
    public function emit(string $event, ...$userdata): self
61
    {
62
        if (isset($this->registered[$event]) === true) {
63
            $set = $this->registered[$event];
64
65
            foreach ($set as $handler) {
66
                call_user_func_array($handler, $userdata);
67
            }
68
        }
69
70
        return $this;
71
    }
72
}
73