Complex classes like EventDispatcher 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 EventDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class EventDispatcher implements EventDispatcherInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var ArrayHashMap |
||
28 | */ |
||
29 | protected $listeners; |
||
30 | |||
31 | /** |
||
32 | * EventDispatcher constructor. |
||
33 | */ |
||
34 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function dispatch($event) |
||
68 | |||
69 | /** |
||
70 | * Ensure event input is of type EventInterface or convert it. |
||
71 | * |
||
72 | * @param string|EventInterface $event |
||
73 | * |
||
74 | * @throws InvalidArgumentException |
||
75 | * |
||
76 | * @return EventInterface |
||
77 | */ |
||
78 | public function ensureEvent($event) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function eventListeners($eventName) |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function listeners() |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function listenerPriority($eventName, callable $listener) |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function hasEventListeners($eventName) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function hasListeners() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function addListener($eventName, callable $listener, $priority = 0) |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function removeListener($eventName, callable $listener) |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function removeSubscriber(EventSubscriberInterface $subscriber) |
||
242 | |||
243 | /** |
||
244 | * Triggers the listeners of an event. |
||
245 | * |
||
246 | * @param SortedArrayHashMap $sortedListeners |
||
247 | * @param EventInterface $event |
||
248 | */ |
||
249 | protected function doDispatch(SortedArrayHashMap $sortedListeners, EventInterface $event) |
||
261 | } |
||
262 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.