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 | public function processEntities(array $entities) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns flow generated from nested sub-entities of the passed entity |
||
| 99 | * |
||
| 100 | * Generated hierarchy of the events: |
||
| 101 | * |
||
| 102 | * - Error |
||
| 103 | * - Update |
||
| 104 | * - Message |
||
| 105 | * - Command |
||
| 106 | * - <Command name> |
||
| 107 | * - Audio ... <Any supported entity> |
||
| 108 | * - Inline Query |
||
| 109 | * - Chosen Inline Result |
||
| 110 | * |
||
| 111 | * @param Update|Error $entity Update or Error entity object |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | protected function getEntitiesFlow($entity) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Processes generated entities flow, if triggered event returns false stops processing |
||
| 156 | * |
||
| 157 | * @param array $entitiesFlow Array of entities flow |
||
| 158 | * |
||
| 159 | * @throws Notice |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | protected function processEntitiesFlow(array $entitiesFlow) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Triggers desired event and returns boolean result from run method of the event. If run() returns |
||
| 187 | * false the processing in main process method will be stopped and further events (if any) |
||
| 188 | * will not be triggered otherwise process will continue until either first false returned or the very |
||
| 189 | * last event in the flow. |
||
| 190 | * |
||
| 191 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 192 | * @param AbstractEntity $parent Entity's parent if any |
||
| 193 | * @param bool $isCommand Boolean flag which indicates that passed entity should |
||
| 194 | * be treated as a command |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | protected function triggerEvent(AbstractEntity $entity, AbstractEntity $parent = null, $isCommand = false) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns mapped event class from the configuration (if it was previously defined) |
||
| 228 | * |
||
| 229 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 230 | * @param bool $isCommand Boolean flag which indicates that passed entity should |
||
| 231 | * be treated as a command |
||
| 232 | * @return null|string |
||
| 233 | */ |
||
| 234 | protected function getMappedEventClass(AbstractEntity $entity, $isCommand) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns default event class |
||
| 262 | * |
||
| 263 | * @param AbstractEntity $entity Entity for which the corresponding event should be triggered |
||
| 264 | * @param bool $isCommand Boolean flag which indicates that passed entity should |
||
| 265 | * be treated as a command |
||
| 266 | * |
||
| 267 | * @return null|string |
||
| 268 | */ |
||
| 269 | protected function getDefaultEventClass($entity, $isCommand) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Executes remote method and returns response object |
||
| 291 | * |
||
| 292 | * @param AbstractMethod $method Method instance |
||
| 293 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
| 294 | * to the entities in the result will not be triggered |
||
| 295 | * @param AbstractMethod $parent Parent entity (if any) |
||
| 296 | * |
||
| 297 | * @return Response |
||
| 298 | */ |
||
| 299 | public function callRemoteMethod(AbstractMethod $method, $silentMode = false, $parent = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns a response object and starts the entities processing (if not in silent mode). Method |
||
| 309 | * should be used only when webhook is set. |
||
| 310 | * |
||
| 311 | * @param string $data Raw json data either received from php input or passed manually |
||
| 312 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
| 313 | * to the entities in the result will not be triggered |
||
| 314 | * |
||
| 315 | * @return Response |
||
| 316 | */ |
||
| 317 | public function getWebhookResponse($data, $silentMode = false) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Processes entities from the response object if not in silent mode or error is received. |
||
| 326 | * |
||
| 327 | * @param Response $response Response object which includes entities |
||
| 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 | protected function processResponse(Response $response, $silentMode = false) |
||
| 341 | } |
||
| 342 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.