• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / Event / 4146879500

pending completion
4146879500

push

github

Olivier Laviale
Tidy

200 of 200 new or added lines in 9 files covered. (100.0%)

191 of 200 relevant lines covered (95.5%)

6.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/lib/Event.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie;
13

14
use ICanBoogie\Accessor\AccessorTrait;
15

16
use function func_num_args;
17
use function get_called_class;
18
use function is_object;
19
use function trigger_error;
20

21
use const E_USER_DEPRECATED;
22

23
/**
24
 * An event.
25
 *
26
 * @property-read bool $stopped `true` when the event was stopped, `false` otherwise.
27
 */
28
abstract class Event
29
{
30
    /**
31
     * @uses get_stopped
32
     */
33
    use AccessorTrait;
34

35
    /**
36
     * @param object|class-string $sender
37
     *
38
     * @return string
39
     *     A qualified event type made of the sender class and the unqualified event type.
40
     *     e.g. "Exception::recover"
41
     */
42
    public static function for(string|object $sender): string
43
    {
44
        if (is_object($sender)) {
16✔
45
            $sender = $sender::class;
6✔
46
        }
47

48
        return $sender . '::' . get_called_class();
16✔
49
    }
50

51
    /**
52
     * The object the event is dispatched on.
53
     *
54
     * **Note:** The property is only initialized if the event is constructed with a sender.
55
     */
56
    public readonly object $sender;
57

58
    /**
59
     * Event unqualified type e.g. `MyEvent`.
60
     */
61
    public readonly string $unqualified_type;
62

63
    /**
64
     * Event qualified type. e.g. `Exception::MyEvent`
65
     */
66
    public readonly string $qualified_type;
67

68
    /**
69
     * `true` when the event was stopped, `false` otherwise.
70
     */
71
    private bool $stopped = false;
72

73
    /** @phpstan-ignore-next-line */
74
    private function get_stopped(): bool
75
    {
76
        return $this->stopped;
8✔
77
    }
78

79
    /**
80
     * @param object|null $sender The sender of the event.
81
     */
82
    public function __construct(object $sender = null)
83
    {
84
        if (func_num_args() > 1) {
10✔
85
            trigger_error(
×
86
                "The 'type' parameter is no longer supported, the event class is used instead.",
×
87
                E_USER_DEPRECATED
×
88
            );
×
89
        }
90

91
        if (func_num_args() > 2) {
10✔
92
            trigger_error(
×
93
                "The 'payload' parameter is no longer supported, better write an event class.",
×
94
                E_USER_DEPRECATED
×
95
            );
×
96
        }
97

98
        $this->unqualified_type = $this::class;
10✔
99

100
        if ($sender) {
10✔
101
            $this->sender = $sender;
6✔
102
            $this->qualified_type = static::for($sender);
6✔
103
        } else {
104
            $this->qualified_type = $this->unqualified_type; // @phpstan-ignore-line
4✔
105
        }
106
    }
107

108
    /**
109
     * Stops the hooks chain.
110
     *
111
     * After the `stop()` method is called the hooks chain is broken and no other hook is called.
112
     */
113
    public function stop(): void
114
    {
115
        $this->stopped = true;
2✔
116
    }
117

118
    /**
119
     * Chain of hooks to execute once the event has been fired.
120
     *
121
     * @var callable[]
122
     *
123
     * @internal
124
     */
125
    public array $internal_chain = [];
126

127
    /**
128
     * Add an event hook to the finish chain.
129
     *
130
     * The finish chain is executed after the event chain was traversed without being stopped.
131
     *
132
     * @phpstan-param (callable(Event, ?object): void) $hook
133
     *
134
     * @return $this
135
     */
136
    public function chain(callable $hook): static
137
    {
138
        $this->internal_chain[] = $hook;
1✔
139

140
        return $this;
1✔
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc