Complex classes like ProcessManager 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 ProcessManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class ProcessManager implements Manager |
||
20 | { |
||
21 | /** |
||
22 | * @var Task[] |
||
23 | */ |
||
24 | private $waiting = []; |
||
25 | |||
26 | /** |
||
27 | * @var Task[] |
||
28 | */ |
||
29 | private $running = []; |
||
30 | |||
31 | /** |
||
32 | * @var null|SplObjectStorage |
||
33 | */ |
||
34 | private $timings = null; |
||
35 | |||
36 | /** |
||
37 | * @var null|string |
||
38 | */ |
||
39 | private $logPath; |
||
40 | |||
41 | /** |
||
42 | * @var null|Rules |
||
43 | */ |
||
44 | private $rules; |
||
45 | |||
46 | /** |
||
47 | * @var null|Shell |
||
48 | */ |
||
49 | protected $shell; |
||
50 | |||
51 | /** |
||
52 | * @var null|string |
||
53 | */ |
||
54 | private $binary; |
||
55 | |||
56 | /** |
||
57 | * @var null|string |
||
58 | */ |
||
59 | private $worker; |
||
60 | |||
61 | /** |
||
62 | * Get a list of the waiting tasks. |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getWaiting() |
||
70 | |||
71 | /** |
||
72 | * Get a list of the running tasks. |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public function getRunning() |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | * |
||
84 | * @param Task $task |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | 1 | public function addTask(Task $task) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 1 | public function tick() |
|
154 | |||
155 | /** |
||
156 | * Stops sibling processes of a task. |
||
157 | * |
||
158 | * @param Task $task |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | private function stopSiblingTasks(Task $task) |
||
176 | |||
177 | /** |
||
178 | * Checks whether a new task can be run. |
||
179 | * |
||
180 | * @param Task $task |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | private function canRunTask(Task $task) |
|
206 | |||
207 | /** |
||
208 | * Gets the load profile related to a task. |
||
209 | * |
||
210 | * @param Task $task |
||
211 | * @param array $processes |
||
212 | * |
||
213 | * @return Profile |
||
214 | */ |
||
215 | 1 | private function getProfileForProcesses(Task $task, array $processes) |
|
237 | |||
238 | /** |
||
239 | * Gets processor and memory stats for a list of processes. |
||
240 | * |
||
241 | * @param Process[] $processes |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | 1 | private function getStatsForProcesses(array $processes) |
|
277 | |||
278 | /** |
||
279 | * Gets or creates a Shell instance. |
||
280 | * |
||
281 | * @return Shell |
||
282 | */ |
||
283 | 2 | public function getShell() |
|
291 | |||
292 | /** |
||
293 | * @param Shell $shell |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | 1 | public function setShell(Shell $shell) |
|
303 | |||
304 | /** |
||
305 | * Creates a new Shell instance. |
||
306 | * |
||
307 | * @return Shell |
||
308 | */ |
||
309 | 2 | private function newShell() |
|
313 | |||
314 | /** |
||
315 | * Creates a new Profile instance. |
||
316 | * |
||
317 | * @return Profile |
||
318 | */ |
||
319 | 1 | private function newProfile() |
|
323 | |||
324 | /** |
||
325 | * Gets or creates a new Rules instance. |
||
326 | * |
||
327 | * @return Rules |
||
328 | */ |
||
329 | 2 | public function getRules() |
|
337 | |||
338 | /** |
||
339 | * @param Rules $rules |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | 1 | public function setRules(Rules $rules) |
|
349 | |||
350 | /** |
||
351 | * Creates a new Rules instance. |
||
352 | * |
||
353 | * @return Rules |
||
354 | */ |
||
355 | 2 | private function newRules() |
|
359 | |||
360 | /** |
||
361 | * @param string $binary |
||
362 | * |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function setBinary($binary) |
||
371 | |||
372 | /** |
||
373 | * Gets the path of the PHP runtime. |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | 1 | public function getBinary() |
|
385 | |||
386 | /** |
||
387 | * @param string $worker |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | public function setWorker($worker) |
||
397 | |||
398 | /** |
||
399 | * Gets the path of the worker script. |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 1 | public function getWorker() |
|
411 | |||
412 | /** |
||
413 | * Gets the path to write stdout to. |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | 1 | private function getStdOut() |
|
425 | |||
426 | /** |
||
427 | * @return null|string |
||
428 | */ |
||
429 | 2 | public function getLogPath() |
|
433 | |||
434 | /** |
||
435 | * @param string $logPath |
||
436 | * |
||
437 | * @return $this |
||
438 | */ |
||
439 | 1 | public function setLogPath($logPath) |
|
445 | |||
446 | /** |
||
447 | * Gets the path to write stderr to. |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | 1 | private function getStdErr() |
|
459 | |||
460 | /** |
||
461 | * Gets a string representation of a task, to pass to the worker script. |
||
462 | * |
||
463 | * @param Task $task |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | 1 | private function getTaskString(Task $task) |
|
471 | |||
472 | /** |
||
473 | * Checks whether a task can be removed from the list of running processes. |
||
474 | * |
||
475 | * @param Task $task |
||
476 | * |
||
477 | * @return bool |
||
478 | */ |
||
479 | 1 | private function canRemoveTask(Task $task) |
|
509 | |||
510 | /** |
||
511 | * Check if the given task is expired |
||
512 | * |
||
513 | * @param Task $task |
||
514 | * |
||
515 | * @return boolean |
||
516 | */ |
||
517 | 1 | private function isTaskExpired(Task $task) |
|
530 | |||
531 | /** |
||
532 | * Check if the given task is cancelled. |
||
533 | * |
||
534 | * @param Task $task |
||
535 | * |
||
536 | * @return bool |
||
537 | */ |
||
538 | 1 | private function isTaskCancelled(Task $task) |
|
546 | |||
547 | /** |
||
548 | * Revoke any background processes attached to this task. |
||
549 | * |
||
550 | * @param Task $task |
||
551 | * |
||
552 | * @return bool |
||
553 | */ |
||
554 | private function killTask(Task $task) |
||
566 | |||
567 | /** |
||
568 | * @param Rule $rule |
||
569 | * |
||
570 | * @return $this |
||
571 | */ |
||
572 | 1 | public function addRule(Rule $rule) |
|
578 | |||
579 | /** |
||
580 | * @param Rule $rule |
||
581 | * |
||
582 | * @return $this |
||
583 | */ |
||
584 | 1 | public function removeRule(Rule $rule) |
|
590 | |||
591 | public function __destruct() |
||
597 | } |
||
598 |