Complex classes like Event often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Event extends BaseObject |
||
30 | { |
||
31 | /** |
||
32 | * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]]. |
||
33 | * Event handlers may use this property to check what event it is handling. |
||
34 | */ |
||
35 | public $name; |
||
36 | /** |
||
37 | * @var object the sender of this event. If not set, this property will be |
||
38 | * set as the object whose `trigger()` method is called. |
||
39 | * This property may also be a `null` when this event is a |
||
40 | * class-level event which is triggered in a static context. |
||
41 | */ |
||
42 | public $sender; |
||
43 | /** |
||
44 | * @var bool whether the event is handled. Defaults to `false`. |
||
45 | * When a handler sets this to be `true`, the event processing will stop and |
||
46 | * ignore the rest of the uninvoked event handlers. |
||
47 | */ |
||
48 | public $handled = false; |
||
49 | /** |
||
50 | * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler. |
||
51 | * Note that this varies according to which event handler is currently executing. |
||
52 | */ |
||
53 | public $data; |
||
54 | |||
55 | /** |
||
56 | * @var array contains all globally registered event handlers. |
||
57 | */ |
||
58 | private static $_events = []; |
||
59 | /** |
||
60 | * @var array the globally registered event handlers attached for wildcard patterns (event name wildcard => handlers) |
||
61 | * @since 2.0.14 |
||
62 | */ |
||
63 | private static $_eventWildcards = []; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Attaches an event handler to a class-level event. |
||
68 | * |
||
69 | * When a class-level event is triggered, event handlers attached |
||
70 | * to that class and all parent classes will be invoked. |
||
71 | * |
||
72 | * For example, the following code attaches an event handler to `ActiveRecord`'s |
||
73 | * `afterInsert` event: |
||
74 | * |
||
75 | * ```php |
||
76 | * Event::on(ActiveRecord::class, ActiveRecord::EVENT_AFTER_INSERT, function ($event) { |
||
77 | * Yii::debug(get_class($event->sender) . ' is inserted.'); |
||
78 | * }); |
||
79 | * ``` |
||
80 | * |
||
81 | * The handler will be invoked for EVERY successful ActiveRecord insertion. |
||
82 | * |
||
83 | * Since 2.0.14 you can specify either class name or event name as a wildcard pattern: |
||
84 | * |
||
85 | * ```php |
||
86 | * Event::on('app\models\db\*', '*Insert', function ($event) { |
||
87 | * Yii::debug(get_class($event->sender) . ' is inserted.'); |
||
88 | * }); |
||
89 | * ``` |
||
90 | * |
||
91 | * For more details about how to declare an event handler, please refer to [[Component::on()]]. |
||
92 | * |
||
93 | * @param string $class the fully qualified class name to which the event handler needs to attach. |
||
94 | * @param string $name the event name. |
||
95 | * @param callable $handler the event handler. |
||
96 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
97 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
98 | * @param bool $append whether to append new event handler to the end of the existing |
||
99 | * handler list. If `false`, the new handler will be inserted at the beginning of the existing |
||
100 | * handler list. |
||
101 | * @see off() |
||
102 | */ |
||
103 | 16 | public static function on($class, $name, $handler, $data = null, $append = true) |
|
122 | |||
123 | /** |
||
124 | * Detaches an event handler from a class-level event. |
||
125 | * |
||
126 | * This method is the opposite of [[on()]]. |
||
127 | * |
||
128 | * Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this |
||
129 | * wildcard will be removed, while handlers registered with plain names matching this wildcard will remain. |
||
130 | * |
||
131 | * @param string $class the fully qualified class name from which the event handler needs to be detached. |
||
132 | * @param string $name the event name. |
||
133 | * @param callable $handler the event handler to be removed. |
||
134 | * If it is `null`, all handlers attached to the named event will be removed. |
||
135 | * @return bool whether a handler is found and detached. |
||
136 | * @see on() |
||
137 | */ |
||
138 | 13 | public static function off($class, $name, $handler = null) |
|
139 | { |
||
140 | 13 | $class = ltrim($class, '\\'); |
|
141 | 13 | if (empty(self::$_events[$name][$class]) && empty(self::$_eventWildcards[$name][$class])) { |
|
142 | return false; |
||
143 | } |
||
144 | 13 | if ($handler === null) { |
|
145 | 8 | unset(self::$_events[$name][$class]); |
|
146 | 8 | unset(self::$_eventWildcards[$name][$class]); |
|
147 | 8 | return true; |
|
148 | } |
||
149 | |||
150 | // plain event names |
||
151 | 5 | if (isset(self::$_events[$name][$class])) { |
|
152 | 4 | $removed = false; |
|
153 | 4 | foreach (self::$_events[$name][$class] as $i => $event) { |
|
154 | 4 | if ($event[0] === $handler) { |
|
155 | 4 | unset(self::$_events[$name][$class][$i]); |
|
156 | 4 | $removed = true; |
|
157 | } |
||
158 | } |
||
159 | 4 | if ($removed) { |
|
160 | 4 | self::$_events[$name][$class] = array_values(self::$_events[$name][$class]); |
|
161 | 4 | return $removed; |
|
162 | } |
||
163 | } |
||
164 | |||
165 | // wildcard event names |
||
166 | 1 | $removed = false; |
|
167 | 1 | foreach (self::$_eventWildcards[$name][$class] as $i => $event) { |
|
168 | 1 | if ($event[0] === $handler) { |
|
169 | 1 | unset(self::$_eventWildcards[$name][$class][$i]); |
|
170 | 1 | $removed = true; |
|
171 | } |
||
172 | } |
||
173 | 1 | if ($removed) { |
|
174 | 1 | self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]); |
|
175 | // remove empty wildcards to save future redundant regex checks : |
||
176 | 1 | if (empty(self::$_eventWildcards[$name][$class])) { |
|
177 | 1 | unset(self::$_eventWildcards[$name][$class]); |
|
178 | 1 | if (empty(self::$_eventWildcards[$name])) { |
|
179 | 1 | unset(self::$_eventWildcards[$name]); |
|
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | 1 | return $removed; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Detaches all registered class-level event handlers. |
||
189 | * @see on() |
||
190 | * @see off() |
||
191 | * @since 2.0.10 |
||
192 | */ |
||
193 | 5 | public static function offAll() |
|
198 | |||
199 | /** |
||
200 | * Returns a value indicating whether there is any handler attached to the specified class-level event. |
||
201 | * Note that this method will also check all parent classes to see if there is any handler attached |
||
202 | * to the named event. |
||
203 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
204 | * @param string $name the event name. |
||
205 | * @return bool whether there is any handler attached to the event. |
||
206 | */ |
||
207 | 102 | public static function hasHandlers($class, $name) |
|
251 | |||
252 | /** |
||
253 | * Triggers a class-level event. |
||
254 | * This method will cause invocation of event handlers that are attached to the named event |
||
255 | * for the specified class and all its parent classes. |
||
256 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
257 | * @param string $name the event name. |
||
258 | * @param Event $event the event parameter. If not set, a default [[Event]] object will be created. |
||
259 | */ |
||
260 | 1835 | public static function trigger($class, $name, $event = null) |
|
316 | } |
||
317 |