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 |
||
47 | class Event implements EventInterface |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]]. |
||
52 | * Event handlers may use this property to check what event it is handling. |
||
53 | */ |
||
54 | public $name; |
||
55 | |||
56 | /** |
||
57 | * @var object the sender of this event. If not set, this property will be |
||
58 | * set as the object whose "trigger()" method is called. |
||
59 | * This property may also be a `null` when this event is a |
||
60 | * class-level event which is triggered in a static context. |
||
61 | */ |
||
62 | public $sender; |
||
63 | |||
64 | /** |
||
65 | * @var boolean whether the event is handled. Defaults to false. |
||
66 | * When a handler sets this to be true, the event processing will stop and |
||
67 | * ignore the rest of the uninvoked event handlers. |
||
68 | */ |
||
69 | public $handled = false; |
||
70 | |||
71 | /** |
||
72 | * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler. |
||
73 | * Note that this varies according to which event handler is currently executing. |
||
74 | */ |
||
75 | public $data; |
||
76 | |||
77 | /** |
||
78 | * Array of events. This contains all event registered for application. |
||
79 | * @var EventInterface[] |
||
80 | */ |
||
81 | private static $events = []; |
||
82 | |||
83 | /** |
||
84 | * Array containing partial classes for class. This holds traits, interfaces, |
||
85 | * parent classes names for class. |
||
86 | * |
||
87 | * Structure is like below: |
||
88 | * ``` |
||
89 | * $partials = [ |
||
90 | * '\Vendor\Package\ClassOne' => [ |
||
91 | * '\Vendor\Package\TraitOne', |
||
92 | * '\Vendor\Package\TraitTwo', |
||
93 | * '\Vendor\Package\InterfaceX', |
||
94 | * '\Vendor\Package\AbstractSix' |
||
95 | * ], |
||
96 | * '\Vendor\Package\ClassTwo' => [ |
||
97 | * '\Vendor\Package\TraitBig', |
||
98 | * '\Vendor\Package\TraitSmall', |
||
99 | * '\Vendor\Package\InterfaceY', |
||
100 | * '\Vendor\Package\AbstractGuy' |
||
101 | * ], |
||
102 | * ]; |
||
103 | * ``` |
||
104 | * |
||
105 | * @var string[] |
||
106 | */ |
||
107 | private static $partials = []; |
||
108 | |||
109 | /** |
||
110 | * Propagated properties cache. It contains only properties which should |
||
111 | * propagate, others are skipped, thus value is always true. |
||
112 | * |
||
113 | * Structure is like follow: |
||
114 | * ``` |
||
115 | * $propagated = [ |
||
116 | * '\Vendor\Package\ClassOne' => [ |
||
117 | * 'fieldOne' => true, |
||
118 | * 'fieldTwo' => true |
||
119 | * ], |
||
120 | * '\Vendor\Package\ClassTwo' => [ |
||
121 | * 'otherFieldOne' => true, |
||
122 | * 'otherFieldTwo' => true |
||
123 | * ], |
||
124 | * |
||
125 | * ]; |
||
126 | * ``` |
||
127 | * @var bool[] |
||
128 | */ |
||
129 | private static $propagated = []; |
||
130 | |||
131 | /** |
||
132 | * Attaches an event handler to a class-level event. |
||
133 | * |
||
134 | * When a class-level event is triggered, event handlers attached |
||
135 | * to that class and all parent classes will be invoked. |
||
136 | * |
||
137 | * For example, the following code attaches an event handler to document's |
||
138 | * `afterInsert` event: |
||
139 | * |
||
140 | * ~~~ |
||
141 | * Event::on($model, EntityManager::EventAfterInsert, function ($event) { |
||
142 | * var_dump(get_class($event->sender) . ' is inserted.'); |
||
143 | * }); |
||
144 | * ~~~ |
||
145 | * |
||
146 | * The handler will be invoked for every successful document insertion. |
||
147 | * |
||
148 | * **NOTE:** Each call will attach new event handler. When placing event |
||
149 | * initialization in class constructors etc. ensure that it is evaluated once, |
||
150 | * or it might trigger same event handler multiple times. |
||
151 | * |
||
152 | * @param AnnotatedInterface|object|string $model the object specifying the class-level event. |
||
153 | * @param string $name the event name. |
||
154 | * @param callable $handler the event handler. |
||
155 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
156 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
157 | * @param boolean $append whether to append new event handler to the end of the existing |
||
158 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
159 | * handler list. |
||
160 | * @see off() |
||
161 | */ |
||
162 | 24 | public static function on($model, $name, $handler, $data = null, $append = true) |
|
174 | |||
175 | /** |
||
176 | * Detaches an event handler from a class-level event. |
||
177 | * |
||
178 | * This method is the opposite of [[on()]]. |
||
179 | * |
||
180 | * @param AnnotatedInterface|object|string $model the object specifying the class-level event. |
||
181 | * @param string $name the event name. |
||
182 | * @param callable $handler the event handler to be removed. |
||
183 | * If it is null, all handlers attached to the named event will be removed. |
||
184 | * @return boolean whether a handler is found and detached. |
||
185 | * @see on() |
||
186 | */ |
||
187 | 10 | public static function off($model, $name, $handler = null) |
|
217 | |||
218 | /** |
||
219 | * Triggers a class-level event. |
||
220 | * This method will cause invocation of event handlers that are attached to the named event |
||
221 | * for the specified class and all its parent classes. |
||
222 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
223 | * @param string $name the event name. |
||
224 | * @param ModelEvent $event the event parameter. If not set, a default `ModelEvent` object will be created. |
||
225 | * @return bool True if event was triggered. |
||
226 | */ |
||
227 | 113 | public static function trigger(AnnotatedInterface $model, $name, &$event = null) |
|
284 | |||
285 | /** |
||
286 | * Triggers a class-level event and checks if it's valid. |
||
287 | * If don't have event handler returns true. If event handler is set, return true if `Event::isValid`. |
||
288 | * This method will cause invocation of event handlers that are attached to the named event |
||
289 | * for the specified class and all its parent classes. |
||
290 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
291 | * @param string $name the event name. |
||
292 | * @param ModelEvent $event the event parameter. If not set, a default [[ModelEvent]] object will be created. |
||
293 | * @return bool True if event was triggered and is valid. |
||
294 | */ |
||
295 | 112 | public static function valid(AnnotatedInterface $model, $name, $event = null) |
|
306 | |||
307 | /** |
||
308 | * Triggers a class-level event and checks if it's handled. |
||
309 | * If don't have event handler returns true. If event handler is set, return true if `Event::handled`. |
||
310 | * This method will cause invocation of event handlers that are attached to the named event |
||
311 | * for the specified class and all its parent classes. |
||
312 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
313 | * @param string $name the event name. |
||
314 | * @param ModelEvent $event the event parameter. If not set, a default [[Event]] object will be created. |
||
315 | * @return bool|null True if handled, false otherway, null if not triggered |
||
316 | */ |
||
317 | 1 | public static function handled(AnnotatedInterface $model, $name, $event = null) |
|
325 | |||
326 | /** |
||
327 | * Check if model has event handler. |
||
328 | * **IMPORTANT**: It does not check for propagated events |
||
329 | * |
||
330 | * @param AnnotatedInterface|string $class the object specifying the class-level event |
||
331 | * @param string $name the event name. |
||
332 | * @return bool True if has handler |
||
333 | */ |
||
334 | 67 | public static function hasHandler($class, $name) |
|
347 | |||
348 | /** |
||
349 | * Get class name |
||
350 | * @param AnnotatedInterface|object|string $class |
||
351 | * @return string |
||
352 | */ |
||
353 | 80 | private static function getName($class) |
|
368 | |||
369 | /** |
||
370 | * Propagate event |
||
371 | * @param AnnotatedInterface $model |
||
372 | * @param string $name |
||
373 | * @param ModelEvent|null $event |
||
374 | */ |
||
375 | 113 | private static function propagate(AnnotatedInterface $model, $name, &$event = null) |
|
404 | |||
405 | /** |
||
406 | * Get properties which should be propagated. |
||
407 | * NOTE: This is cached, as it might be called numerous times |
||
408 | * @param object $model |
||
409 | * @return bool[] |
||
410 | */ |
||
411 | 113 | private static function getPropagatedProperties($model) |
|
429 | |||
430 | 80 | public static function getPartials($className) |
|
457 | |||
458 | protected static function destroyEvents() |
||
462 | |||
463 | } |
||
464 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.