Complex classes like Handler 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 Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Handler |
||
29 | { |
||
30 | /** @var Handler */ |
||
31 | protected static $instance; |
||
32 | |||
33 | /** @var Config $config */ |
||
34 | protected $config; |
||
35 | |||
36 | /** @var Request $request */ |
||
37 | protected $request; |
||
38 | |||
39 | /** |
||
40 | * Returns a singletone instance of executor |
||
41 | * |
||
42 | * @return Handler |
||
43 | */ |
||
44 | public static function getInstance() |
||
52 | |||
53 | /** |
||
54 | * Returns an instance of config class |
||
55 | * |
||
56 | * @return Config |
||
57 | */ |
||
58 | public function getConfig() |
||
62 | |||
63 | /** |
||
64 | * Initialises the executor, creates request object |
||
65 | * |
||
66 | * @param Config $config Configuration object |
||
67 | */ |
||
68 | public function initWithConfig(Config $config) |
||
73 | |||
74 | /** |
||
75 | * Processes array of entities from response object, root entities must be either update entity |
||
76 | * or error entity in case of error response from Telegram's API. |
||
77 | * |
||
78 | * @param array $entities Array of entities (Update or Error) passed either from response object or |
||
79 | * directly to the method |
||
80 | * |
||
81 | * @return bool |
||
82 | * |
||
83 | * @throws Notice |
||
84 | */ |
||
85 | public function processEntities(array $entities) |
||
104 | |||
105 | /** |
||
106 | * Returns entities chain generated from nested sub-entities of the passed entity |
||
107 | * |
||
108 | * Generated hierarchy of the events: |
||
109 | * |
||
110 | * - Error |
||
111 | * - Update |
||
112 | * - Message |
||
113 | * - Command |
||
114 | * - <Command name> |
||
115 | * - Audio ... <Any supported entity> |
||
116 | * - Inline Query |
||
117 | * - Chosen Inline Result |
||
118 | * |
||
119 | * @param Update|Error $entity Update or Error entity object |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getEntitiesChain($entity) |
||
164 | |||
165 | /** |
||
166 | * Processes generated entities chain, if triggered event returns false stops processing |
||
167 | * |
||
168 | * @param array $entitiesFlow Array of entities flow |
||
169 | * |
||
170 | * @throws Notice |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | protected function processEntitiesChain(array $entitiesFlow) |
||
191 | |||
192 | /** |
||
193 | * Triggers desired event and returns boolean result from run method of the event. If run() returns |
||
194 | * false the processing in main process method will be stopped and further events (if any) |
||
195 | * will not be triggered otherwise process will continue until either first false returned or the very |
||
196 | * last event in the flow. |
||
197 | * |
||
198 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
199 | * @param AbstractEntity $parent Entity's parent if any |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | protected function triggerEventForEntity(AbstractEntity $entity, AbstractEntity $parent = null) |
||
226 | |||
227 | /** |
||
228 | * Returns mapped event class from the configuration (if it was previously defined) |
||
229 | * |
||
230 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
231 | * be treated as a command |
||
232 | * @return null|string |
||
233 | */ |
||
234 | protected function getEventClass(AbstractEntity $entity) |
||
258 | |||
259 | /** |
||
260 | * Checks whether command is defined in config and matches the current one |
||
261 | * |
||
262 | * @param array $preDefinedEvent Pre defined event data |
||
263 | * @param string $command Command name |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | protected function isCommandSupported($preDefinedEvent, $command) |
||
271 | |||
272 | /** |
||
273 | * Executes remote method and returns response object |
||
274 | * |
||
275 | * @param AbstractMethod $method Method instance |
||
276 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
277 | * to the entities in the result will not be triggered |
||
278 | * @param AbstractEntity $parent Parent entity (if any) |
||
279 | * |
||
280 | * @return Response |
||
281 | */ |
||
282 | public function callRemoteMethod(AbstractMethod $method, $silentMode = false, $parent = null) |
||
289 | |||
290 | /** |
||
291 | * Returns a response object and starts the entities processing (if not in silent mode). Method |
||
292 | * should be used only when webhook is set. |
||
293 | * |
||
294 | * @param string $data Raw json data either received from php input or passed manually |
||
295 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
296 | * to the entities in the result will not be triggered |
||
297 | * |
||
298 | * @return Response |
||
299 | */ |
||
300 | public function getWebhookResponse($data, $silentMode = false) |
||
306 | |||
307 | /** |
||
308 | * Processes entities from the response object if not in silent mode or error is received. |
||
309 | * |
||
310 | * @param Response $response Response object which includes entities |
||
311 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
312 | * to the entities in the result will not be triggered |
||
313 | * |
||
314 | * @return Response |
||
315 | */ |
||
316 | protected function processResponse(Response $response, $silentMode = false) |
||
324 | } |
||
325 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.