Complex classes like Executor 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 Executor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Executor |
||
| 27 | { |
||
| 28 | /** @var Executor */ |
||
| 29 | protected static $instance; |
||
| 30 | |||
| 31 | /** @var Config $config */ |
||
| 32 | protected $config; |
||
| 33 | |||
| 34 | /** @var Request $request */ |
||
| 35 | protected $request; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Returns a singletone instance of executor |
||
| 39 | * |
||
| 40 | * @return Executor |
||
| 41 | */ |
||
| 42 | public static function getInstance() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns an instance of config class |
||
| 53 | * |
||
| 54 | * @return Config |
||
| 55 | */ |
||
| 56 | public function getConfig() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialises the executor, creates request object |
||
| 63 | * |
||
| 64 | * @param Config $config Configuration object |
||
| 65 | */ |
||
| 66 | public function initWithConfig(Config $config) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Processes array of entities from response object, root entities must be either update entity |
||
| 74 | * or error entity in case of error response from Telegram's API. |
||
| 75 | * |
||
| 76 | * @param array $entities Array of entities (Update or Error) passed either from response object or |
||
| 77 | * directly to the method |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | * |
||
| 81 | * @throws Notice |
||
| 82 | */ |
||
| 83 | public function processEntities(array $entities) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns flow generated from nested sub-entities of the passed entity |
||
| 105 | * |
||
| 106 | * Generated hierarchy of the events: |
||
| 107 | * |
||
| 108 | * - Error |
||
| 109 | * - Update |
||
| 110 | * - Message |
||
| 111 | * - Command |
||
| 112 | * - <Command name> |
||
| 113 | * - Audio ... <Any supported entity> |
||
| 114 | * - Inline Query |
||
| 115 | * - Chosen Inline Result |
||
| 116 | * |
||
| 117 | * @param Update|Error $entity Update or Error entity object |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getEntitiesFlow($entity) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Processes generated entities flow, if triggered event returns false stops processing |
||
| 162 | * |
||
| 163 | * @param array $entitiesFlow Array of entities flow |
||
| 164 | * |
||
| 165 | * @throws Notice |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | protected function processEntitiesFlow(array $entitiesFlow) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Triggers desired event and returns boolean result from run method of the event. If run() returns |
||
| 193 | * false the processing in main process method will be stopped and further events (if any) |
||
| 194 | * will not be triggered otherwise process will continue until either first false returned or the very |
||
| 195 | * last event in the flow. |
||
| 196 | * |
||
| 197 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 198 | * @param AbstractEntity $parent Entity's parent if any |
||
| 199 | * @param bool $isCommand Boolean flag which indicates that passed entity should |
||
| 200 | * be treated as a command |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | protected function triggerEvent(AbstractEntity $entity, AbstractEntity $parent = null, $isCommand = false) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns mapped event class from the configuration (if it was previously defined) |
||
| 234 | * |
||
| 235 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 236 | * be treated as a command |
||
| 237 | * @return null|string |
||
| 238 | */ |
||
| 239 | protected function getMappedEventClass(AbstractEntity $entity) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Checks whether command is defined in config and matches the current one |
||
| 265 | * |
||
| 266 | * @param array $preDefinedEvent Pre defined event data |
||
| 267 | * @param string $command Command name |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | protected function isCommandSupported($preDefinedEvent, $command) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns default event class |
||
| 278 | * |
||
| 279 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 280 | * @param bool $isCommand Boolean flag which indicates that passed entity should |
||
| 281 | * be treated as a command |
||
| 282 | * |
||
| 283 | * @return null|string |
||
| 284 | */ |
||
| 285 | protected function getDefaultEventClass($entity, $isCommand) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Executes remote method and returns response object |
||
| 307 | * |
||
| 308 | * @param AbstractMethod $method Method instance |
||
| 309 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
| 310 | * to the entities in the result will not be triggered |
||
| 311 | * @param AbstractEntity $parent Parent entity (if any) |
||
| 312 | * |
||
| 313 | * @return Response |
||
| 314 | */ |
||
| 315 | public function callRemoteMethod(AbstractMethod $method, $silentMode = false, $parent = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns a response object and starts the entities processing (if not in silent mode). Method |
||
| 325 | * should be used only when webhook is set. |
||
| 326 | * |
||
| 327 | * @param string $data Raw json data either received from php input or passed manually |
||
| 328 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
| 329 | * to the entities in the result will not be triggered |
||
| 330 | * |
||
| 331 | * @return Response |
||
| 332 | */ |
||
| 333 | public function getWebhookResponse($data, $silentMode = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Processes entities from the response object if not in silent mode or error is received. |
||
| 342 | * |
||
| 343 | * @param Response $response Response object which includes entities |
||
| 344 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
| 345 | * to the entities in the result will not be triggered |
||
| 346 | * |
||
| 347 | * @return Response |
||
| 348 | */ |
||
| 349 | protected function processResponse(Response $response, $silentMode = false) |
||
| 357 | } |
||
| 358 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.