Complex classes like Processor 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 Processor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Processor |
||
30 | { |
||
31 | /** @var ConfigContainer $config */ |
||
32 | protected $config; |
||
33 | |||
34 | /** @var HttpClient $httpClient */ |
||
35 | protected $httpClient; |
||
36 | |||
37 | /** |
||
38 | * Processor constructor. |
||
39 | * |
||
40 | * @param ConfigContainer $config |
||
41 | * @param HttpClient $httpClient |
||
42 | */ |
||
43 | public function __construct(ConfigContainer $config, HttpClient $httpClient) |
||
48 | |||
49 | /** |
||
50 | * Returns configuration container |
||
51 | * |
||
52 | * @return ConfigContainer |
||
53 | */ |
||
54 | public function getConfig() |
||
58 | |||
59 | /** |
||
60 | * Processes array of entities from response object, root entities must be either update entity |
||
61 | * or error entity in case of error response from Telegram's API. |
||
62 | * |
||
63 | * @param array $entities Array of entities (Update or Error) passed either from response object or |
||
64 | * directly to the method |
||
65 | * |
||
66 | * @return bool |
||
67 | * |
||
68 | * @throws ProcessEntitiesException |
||
69 | */ |
||
70 | public function processEntities(array $entities) |
||
89 | |||
90 | /** |
||
91 | * Returns entities chain generated from nested sub-entities of the passed entity |
||
92 | * |
||
93 | * Generated hierarchy of the events: |
||
94 | * |
||
95 | * - Error |
||
96 | * - Update |
||
97 | * - Message |
||
98 | * - Command |
||
99 | * - <Command name> |
||
100 | * - Audio ... <Any supported entity> |
||
101 | * - Inline Query |
||
102 | * - Chosen Inline Result |
||
103 | * |
||
104 | * @param Update|Error $entity Update or Error entity object |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getEntitiesChain($entity) |
||
149 | |||
150 | /** |
||
151 | * Processes generated entities chain, if triggered event returns false stops processing |
||
152 | * |
||
153 | * @param array $entitiesChain Array of entities |
||
154 | * |
||
155 | * @throws ProcessEntitiesChainException |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | protected function processEntitiesChain(array $entitiesChain) |
||
176 | |||
177 | /** |
||
178 | * Triggers desired event and returns boolean result from run method of the event. If run() returns |
||
179 | * false the processing in main process method will be stopped and further events (if any) |
||
180 | * will not be triggered otherwise process will continue until either first false returned or the very |
||
181 | * last event in the flow. |
||
182 | * |
||
183 | * @param EntityInterface $entity Entity for which the corresponding event should be triggered |
||
184 | * @param EntityInterface $parent Entity's parent if any |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function triggerEventForEntity(EntityInterface $entity, EntityInterface $parent = null) |
||
222 | |||
223 | /** |
||
224 | * Returns event configuration item search by the data from entity |
||
225 | * |
||
226 | * @param EntityInterface $entity Entity for which the corresponding event should be triggered |
||
227 | * be treated as a command |
||
228 | * |
||
229 | * @return null|EventConfig |
||
230 | */ |
||
231 | protected function getEventConfiguration(EntityInterface $entity) |
||
267 | |||
268 | /** |
||
269 | * Checks whether command is defined in config and matches the current one |
||
270 | * |
||
271 | * @param string|null $preDefinedCommand Pre command |
||
272 | * @param string $command Command name |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | protected function isCommandSupported($preDefinedCommand, $command) |
||
280 | |||
281 | /** |
||
282 | * Executes remote method and returns response object |
||
283 | * |
||
284 | * @param AbstractMethod $method Method instance |
||
285 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
286 | * to the entities in the result will not be triggered |
||
287 | * @param EntityInterface $parent Parent entity (if any) |
||
288 | * |
||
289 | * @return Response |
||
290 | */ |
||
291 | public function call(AbstractMethod $method, $silentMode = false, $parent = null) |
||
298 | |||
299 | /** |
||
300 | * Returns a response object and starts the entities processing (if not in silent mode). Method |
||
301 | * should be used only when webhook is set. |
||
302 | * |
||
303 | * @param string $data Raw json data either received from php input or passed manually |
||
304 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
305 | * to the entities in the result will not be triggered |
||
306 | * |
||
307 | * @return Response |
||
308 | */ |
||
309 | public function getWebhookResponse($data, $silentMode = false) |
||
315 | |||
316 | /** |
||
317 | * Processes entities from the response object if not in silent mode or error is received. |
||
318 | * |
||
319 | * @param Response $response Response object which includes entities |
||
320 | * @param bool $silentMode If set to true then the events, mapped (in config or by default) |
||
321 | * to the entities in the result will not be triggered |
||
322 | * |
||
323 | * @return Response |
||
324 | */ |
||
325 | protected function processResponse(Response $response, $silentMode = false) |
||
333 | } |
||
334 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.