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 |
||
| 79 | * @var EventInterface[] |
||
| 80 | */ |
||
| 81 | private static $events = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Array containing partial classes for class |
||
| 85 | * @var string[] |
||
| 86 | */ |
||
| 87 | private static $partials = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Propagated properties cache |
||
| 91 | * @var bool[] |
||
| 92 | */ |
||
| 93 | private static $propagated = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Attaches an event handler to a class-level event. |
||
| 97 | * |
||
| 98 | * When a class-level event is triggered, event handlers attached |
||
| 99 | * to that class and all parent classes will be invoked. |
||
| 100 | * |
||
| 101 | * For example, the following code attaches an event handler to document's |
||
| 102 | * `afterInsert` event: |
||
| 103 | * |
||
| 104 | * ~~~ |
||
| 105 | * Event::on($model, EntityManager::EventAfterInsert, function ($event) { |
||
| 106 | * var_dump(get_class($event->sender) . ' is inserted.'); |
||
| 107 | 20 | * }); |
|
| 108 | * ~~~ |
||
| 109 | 20 | * |
|
| 110 | 20 | * The handler will be invoked for every successful document insertion. |
|
| 111 | 20 | * |
|
| 112 | 20 | * **NOTE:** Each call will attach new event handler. When placing event |
|
| 113 | 20 | * initialization in class constructors etc. ensure that it is evaluated once, |
|
| 114 | * or it might trigger same event handler multiple times. |
||
| 115 | * |
||
| 116 | * @param AnnotatedInterface|object|string $model the object specifying the class-level event. |
||
| 117 | * @param string $name the event name. |
||
| 118 | 20 | * @param callable $handler the event handler. |
|
| 119 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
| 120 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
| 121 | * @param boolean $append whether to append new event handler to the end of the existing |
||
| 122 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
| 123 | * handler list. |
||
| 124 | * @see off() |
||
| 125 | */ |
||
| 126 | public static function on($model, $name, $handler, $data = null, $append = true) |
||
| 138 | |||
| 139 | 10 | /** |
|
| 140 | 10 | * Detaches an event handler from a class-level event. |
|
| 141 | * |
||
| 142 | * This method is the opposite of [[on()]]. |
||
| 143 | * |
||
| 144 | * @param AnnotatedInterface|object|string $model the object specifying the class-level event. |
||
| 145 | * @param string $name the event name. |
||
| 146 | 10 | * @param callable $handler the event handler to be removed. |
|
| 147 | 10 | * If it is null, all handlers attached to the named event will be removed. |
|
| 148 | * @return boolean whether a handler is found and detached. |
||
| 149 | 10 | * @see on() |
|
| 150 | 10 | */ |
|
| 151 | 10 | public static function off($model, $name, $handler = null) |
|
| 181 | 13 | ||
| 182 | 13 | /** |
|
| 183 | 17 | * Triggers a class-level event. |
|
| 184 | 17 | * This method will cause invocation of event handlers that are attached to the named event |
|
| 185 | * for the specified class and all its parent classes. |
||
| 186 | 17 | * @param AnnotatedInterface $model the object specifying the class-level event. |
|
| 187 | 17 | * @param string $name the event name. |
|
| 188 | 13 | * @param ModelEvent $event the event parameter. If not set, a default `ModelEvent` object will be created. |
|
| 189 | 13 | * @return bool True if event was triggered. |
|
| 190 | 17 | */ |
|
| 191 | public static function trigger(AnnotatedInterface $model, $name, &$event = null) |
||
| 242 | |||
| 243 | /** |
||
| 244 | 1 | * Triggers a class-level event and checks if it's valid. |
|
| 245 | * If don't have event handler returns true. If event handler is set, return true if `Event::isValid`. |
||
| 246 | 1 | * This method will cause invocation of event handlers that are attached to the named event |
|
| 247 | 1 | * for the specified class and all its parent classes. |
|
| 248 | 1 | * @param AnnotatedInterface $model the object specifying the class-level event. |
|
| 249 | * @param string $name the event name. |
||
| 250 | * @param ModelEvent $event the event parameter. If not set, a default [[ModelEvent]] object will be created. |
||
| 251 | * @return bool True if event was triggered and is valid. |
||
| 252 | */ |
||
| 253 | public static function valid(AnnotatedInterface $model, $name, $event = null) |
||
| 264 | |||
| 265 | /** |
||
| 266 | 56 | * Triggers a class-level event and checks if it's handled. |
|
| 267 | 56 | * If don't have event handler returns true. If event handler is set, return true if `Event::handled`. |
|
| 268 | 1 | * This method will cause invocation of event handlers that are attached to the named event |
|
| 269 | * for the specified class and all its parent classes. |
||
| 270 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
| 271 | 56 | * @param string $name the event name. |
|
| 272 | 56 | * @param ModelEvent $event the event parameter. If not set, a default [[Event]] object will be created. |
|
| 273 | * @return bool|null True if handled, false otherway, null if not triggered |
||
| 274 | */ |
||
| 275 | public static function handled(AnnotatedInterface $model, $name, $event = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Check if model has event handler. |
||
| 286 | * **IMPORTANT**: It does not check for propagated events |
||
| 287 | * |
||
| 288 | * @param AnnotatedInterface|string $class the object specifying the class-level event |
||
| 289 | * @param string $name the event name. |
||
| 290 | * @return bool True if has handler |
||
| 291 | 107 | */ |
|
| 292 | public static function hasHandler($class, $name) |
||
| 305 | 32 | ||
| 306 | 32 | /** |
|
| 307 | 18 | * Get class name |
|
| 308 | * @param AnnotatedInterface|object|string $class |
||
| 309 | * @return string |
||
| 310 | 26 | */ |
|
| 311 | 26 | private static function getName($class) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Propagate event |
||
| 329 | * @param AnnotatedInterface $model |
||
| 330 | * @param string $name |
||
| 331 | * @param ModelEvent|null $event |
||
| 332 | */ |
||
| 333 | private static function propagate(AnnotatedInterface $model, $name, &$event = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get properties which should be propagated. |
||
| 365 | * NOTE: This is cached, as it might be called numerous times |
||
| 366 | * @param object $model |
||
| 367 | * @return bool[] |
||
| 368 | */ |
||
| 369 | private static function getPropagatedProperties($model) |
||
| 387 | |||
| 388 | public static function getPartials($className) |
||
| 415 | |||
| 416 | } |
||
| 417 |
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.