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 |
||
33 | class Event extends BaseObject |
||
34 | { |
||
35 | /** |
||
36 | * @var string the event name. Event handlers may use this property to check what event it is handling. |
||
37 | */ |
||
38 | private $_name; |
||
39 | /** |
||
40 | * @var object|null the target/context from which event was triggered. |
||
41 | */ |
||
42 | private $_target; |
||
43 | /** |
||
44 | * @var bool whether the propagation of this event is stopped. |
||
45 | */ |
||
46 | private $_isPropagationStopped = false; |
||
47 | /** |
||
48 | * @var array the parameters that are passed to [[Component::on()]] when attaching an event handler. |
||
49 | * Note that this varies according to which event handler is currently executing. |
||
50 | */ |
||
51 | private $_params = []; |
||
52 | |||
53 | /** |
||
54 | * @var array contains all globally registered event handlers. |
||
55 | */ |
||
56 | private static $_events = []; |
||
57 | /** |
||
58 | * @var array the globally registered event handlers attached for wildcard patterns (event name wildcard => handlers) |
||
59 | * @since 2.0.14 |
||
60 | */ |
||
61 | private static $_eventWildcards = []; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Returns event name. |
||
66 | * @return string event name. |
||
67 | * @since 3.0.0 |
||
68 | */ |
||
69 | 482 | public function getName() |
|
76 | |||
77 | /** |
||
78 | * Sets the event name. |
||
79 | * @param string $name event name. |
||
80 | * @since 3.0.0 |
||
81 | */ |
||
82 | 940 | public function setName($name) |
|
86 | |||
87 | /** |
||
88 | * Generates default event name to be used in case it is not explicitly set. |
||
89 | * By default this method generates event name from its class name, converting it to 'dot.separated.string' format. |
||
90 | * Child classes may override this method providing their own implementation. |
||
91 | * @return string default event name. |
||
92 | * @since 3.0.0 |
||
93 | */ |
||
94 | 1 | protected function defaultName() |
|
98 | |||
99 | /** |
||
100 | * Returns target/context from which event was triggered. |
||
101 | * Target usually is set as the object whose `trigger()` method is called. |
||
102 | * This property may be a `null` when this event is a class-level event, |
||
103 | * which is triggered in a static context. |
||
104 | * @return object|null target/context from which event was triggered. |
||
105 | * @since 3.0.0 |
||
106 | */ |
||
107 | 719 | public function getTarget() |
|
111 | |||
112 | /** |
||
113 | * Sets target/context from which event was triggered. |
||
114 | * @param object|null $target target/context from which event was triggered. |
||
115 | * @since 3.0.0 |
||
116 | */ |
||
117 | 719 | public function setTarget($target) |
|
121 | |||
122 | /** |
||
123 | * Indicate whether or not to stop propagating this event. |
||
124 | * When a handler sets this to be `true`, the event processing will stop and |
||
125 | * ignore the rest of the event handlers, which have not been invoked yet. |
||
126 | * @param bool $flag whether or not to stop propagating this event. Default is `true`. |
||
127 | * @since 3.0.0 |
||
128 | */ |
||
129 | 719 | public function stopPropagation($flag = true) |
|
133 | |||
134 | /** |
||
135 | * Indicates whether or not the propagation of this event has been stopped. |
||
136 | * @return bool whether or not the propagation of this event has been stopped. |
||
137 | * @since 3.0.0 |
||
138 | */ |
||
139 | 117 | public function isPropagationStopped() |
|
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | * @since 3.0.0 |
||
147 | */ |
||
148 | public function getParams() |
||
152 | |||
153 | /** |
||
154 | * @param array $params |
||
155 | * @since 3.0.0 |
||
156 | */ |
||
157 | 118 | public function setParams(array $params) |
|
161 | |||
162 | /** |
||
163 | * Get a single parameter by name |
||
164 | * @param string $name parameter name. |
||
165 | * @param mixed|null $default default value. |
||
166 | * @return mixed parameter value. |
||
167 | * @since 3.0.0 |
||
168 | */ |
||
169 | public function getParam($name, $default = null) |
||
176 | |||
177 | /** |
||
178 | * Attaches an event handler to a class-level event. |
||
179 | * |
||
180 | * When a class-level event is triggered, event handlers attached |
||
181 | * to that class and all parent classes will be invoked. |
||
182 | * |
||
183 | * For example, the following code attaches an event handler to `ActiveRecord`'s |
||
184 | * `afterInsert` event: |
||
185 | * |
||
186 | * ```php |
||
187 | * Event::on(ActiveRecord::class, ActiveRecord::EVENT_AFTER_INSERT, function ($event) { |
||
188 | * Yii::debug(get_class($event->sender) . ' is inserted.'); |
||
189 | * }); |
||
190 | * ``` |
||
191 | * |
||
192 | * The handler will be invoked for EVERY successful ActiveRecord insertion. |
||
193 | * |
||
194 | * Since 2.0.14 you can specify either class name or event name as a wildcard pattern: |
||
195 | * |
||
196 | * ```php |
||
197 | * Event::on('app\models\db\*', '*Insert', function ($event) { |
||
198 | * Yii::debug(get_class($event->sender) . ' is inserted.'); |
||
199 | * }); |
||
200 | * ``` |
||
201 | * |
||
202 | * For more details about how to declare an event handler, please refer to [[Component::on()]]. |
||
203 | * |
||
204 | * @param string $class the fully qualified class name to which the event handler needs to attach. |
||
205 | * @param string $name the event name. |
||
206 | * @param callable $handler the event handler. |
||
207 | * @param array $params the parameters to be passed to the event handler when the event is triggered. |
||
208 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
209 | * @param bool $append whether to append new event handler to the end of the existing |
||
210 | * handler list. If `false`, the new handler will be inserted at the beginning of the existing |
||
211 | * handler list. |
||
212 | * @see off() |
||
213 | */ |
||
214 | 16 | public static function on($class, $name, $handler, array $params = [], $append = true) |
|
233 | |||
234 | /** |
||
235 | * Detaches an event handler from a class-level event. |
||
236 | * |
||
237 | * This method is the opposite of [[on()]]. |
||
238 | * |
||
239 | * Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this |
||
240 | * wildcard will be removed, while handlers registered with plain names matching this wildcard will remain. |
||
241 | * |
||
242 | * @param string $class the fully qualified class name from which the event handler needs to be detached. |
||
243 | * @param string $name the event name. |
||
244 | * @param callable $handler the event handler to be removed. |
||
245 | * If it is `null`, all handlers attached to the named event will be removed. |
||
246 | * @return bool whether a handler is found and detached. |
||
247 | * @see on() |
||
248 | */ |
||
249 | 13 | public static function off($class, $name, $handler = null) |
|
297 | |||
298 | /** |
||
299 | * Detaches all registered class-level event handlers. |
||
300 | * @see on() |
||
301 | * @see off() |
||
302 | * @since 2.0.10 |
||
303 | */ |
||
304 | 6 | public static function offAll() |
|
309 | |||
310 | /** |
||
311 | * Returns a value indicating whether there is any handler attached to the specified class-level event. |
||
312 | * Note that this method will also check all parent classes to see if there is any handler attached |
||
313 | * to the named event. |
||
314 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
315 | * @param string $name the event name. |
||
316 | * @return bool whether there is any handler attached to the event. |
||
317 | */ |
||
318 | 103 | public static function hasHandlers($class, $name) |
|
362 | |||
363 | /** |
||
364 | * Triggers a class-level event. |
||
365 | * This method will cause invocation of event handlers that are attached to the named event |
||
366 | * for the specified class and all its parent classes. |
||
367 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
368 | * @param Event|string $event the event instance or name. If string name passed, a default [[Event]] object will be created. |
||
369 | */ |
||
370 | 1914 | public static function trigger($class, $event) |
|
433 | } |
||
434 |