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 |
||
24 | class ProcessManager implements Manager |
||
25 | { |
||
26 | /** |
||
27 | * @var Task[] |
||
28 | */ |
||
29 | private $waiting = []; |
||
30 | |||
31 | /** |
||
32 | * @var Task[] |
||
33 | */ |
||
34 | private $running = []; |
||
35 | |||
36 | /** |
||
37 | * @var null|SplObjectStorage |
||
38 | */ |
||
39 | private $timings = null; |
||
40 | |||
41 | /** |
||
42 | * @var null|string |
||
43 | */ |
||
44 | private $logPath; |
||
45 | |||
46 | /** |
||
47 | * @var null|Rules |
||
48 | */ |
||
49 | private $rules; |
||
50 | |||
51 | /** |
||
52 | * @var null|Shell |
||
53 | */ |
||
54 | protected $shell; |
||
55 | |||
56 | /** |
||
57 | * @var null|string |
||
58 | */ |
||
59 | private $binary; |
||
60 | |||
61 | /** |
||
62 | * @var null|string |
||
63 | */ |
||
64 | private $worker; |
||
65 | |||
66 | /** |
||
67 | * Get a list of the waiting tasks. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getWaiting() |
||
75 | |||
76 | /** |
||
77 | * Get a list of the running tasks. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getRunning() |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | * |
||
89 | * @param Task $task |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | 1 | public function addTask(Task $task) |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 1 | public function tick() |
|
160 | |||
161 | /** |
||
162 | * Assemble the command |
||
163 | * this can be used to customise command by subclassing and overwriting this function |
||
164 | * |
||
165 | * @param string $binary |
||
166 | * @param string $worker |
||
167 | * @param string $stdout |
||
168 | * @param string $stderr |
||
169 | * @return string |
||
170 | */ |
||
171 | 1 | protected function getCommand($binary, $worker, $stdout, $stderr) |
|
175 | |||
176 | /** |
||
177 | * Stops sibling processes of a task. |
||
178 | * |
||
179 | * @param Task $task |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | private function stopSiblingTasks(Task $task) |
||
200 | |||
201 | /** |
||
202 | * Checks whether a new task can be run. |
||
203 | * |
||
204 | * @param Task $task |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 1 | private function canRunTask(Task $task) |
|
230 | |||
231 | /** |
||
232 | * Gets the load profile related to a task. |
||
233 | * |
||
234 | * @param Task $task |
||
235 | * @param array $processes |
||
236 | * |
||
237 | * @return Profile |
||
238 | */ |
||
239 | 1 | private function getProfileForProcesses(Task $task, array $processes) |
|
261 | |||
262 | /** |
||
263 | * Gets processor and memory stats for a list of processes. |
||
264 | * |
||
265 | * @param Process[] $processes |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | 1 | private function getStatsForProcesses(array $processes) |
|
301 | |||
302 | /** |
||
303 | * Gets or creates a Shell instance. |
||
304 | * |
||
305 | * @return Shell |
||
306 | */ |
||
307 | 2 | public function getShell() |
|
315 | |||
316 | /** |
||
317 | * @param Shell $shell |
||
318 | * |
||
319 | * @return $this |
||
320 | */ |
||
321 | 1 | public function setShell(Shell $shell) |
|
327 | |||
328 | /** |
||
329 | * Creates a new Shell instance. |
||
330 | * |
||
331 | * @return Shell |
||
332 | */ |
||
333 | 2 | private function newShell() |
|
337 | |||
338 | /** |
||
339 | * Creates a new Profile instance. |
||
340 | * |
||
341 | * @return Profile |
||
342 | */ |
||
343 | 1 | private function newProfile() |
|
347 | |||
348 | /** |
||
349 | * Gets or creates a new Rules instance. |
||
350 | * |
||
351 | * @return Rules |
||
352 | */ |
||
353 | 2 | public function getRules() |
|
361 | |||
362 | /** |
||
363 | * @param Rules $rules |
||
364 | * |
||
365 | * @return $this |
||
366 | */ |
||
367 | 1 | public function setRules(Rules $rules) |
|
373 | |||
374 | /** |
||
375 | * Creates a new Rules instance. |
||
376 | * |
||
377 | * @return Rules |
||
378 | */ |
||
379 | 2 | private function newRules() |
|
383 | |||
384 | /** |
||
385 | * @param string $binary |
||
386 | * |
||
387 | * @return $this |
||
388 | */ |
||
389 | public function setBinary($binary) |
||
395 | |||
396 | /** |
||
397 | * Gets the path of the PHP runtime. |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | 1 | public function getBinary() |
|
409 | |||
410 | /** |
||
411 | * @param string $worker |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setWorker($worker) |
||
421 | |||
422 | /** |
||
423 | * Gets the path of the worker script. |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | 1 | public function getWorker() |
|
435 | |||
436 | /** |
||
437 | * Gets the path to write stdout to. |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 1 | private function getStdOut() |
|
449 | |||
450 | /** |
||
451 | * @return null|string |
||
452 | */ |
||
453 | 2 | public function getLogPath() |
|
457 | |||
458 | /** |
||
459 | * @param string $logPath |
||
460 | * |
||
461 | * @return $this |
||
462 | */ |
||
463 | 1 | public function setLogPath($logPath) |
|
469 | |||
470 | /** |
||
471 | * Gets the path to write stderr to. |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | 1 | private function getStdErr() |
|
483 | |||
484 | /** |
||
485 | * Gets a string representation of a task, to pass to the worker script. |
||
486 | * |
||
487 | * @param Task $task |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | 1 | private function getTaskString(Task $task) |
|
495 | |||
496 | /** |
||
497 | * Checks whether a task can be removed from the list of running processes. |
||
498 | * |
||
499 | * @param Task $task |
||
500 | * |
||
501 | * @return bool |
||
502 | */ |
||
503 | 1 | private function canRemoveTask(Task $task) |
|
533 | |||
534 | /** |
||
535 | * Check if the given task is expired |
||
536 | * |
||
537 | * @param Task $task |
||
538 | * |
||
539 | * @return boolean |
||
540 | */ |
||
541 | 1 | private function isTaskExpired(Task $task) |
|
554 | |||
555 | /** |
||
556 | * Check if the given task is cancelled. |
||
557 | * |
||
558 | * @param Task $task |
||
559 | * |
||
560 | * @return bool |
||
561 | */ |
||
562 | 1 | private function isTaskCancelled(Task $task) |
|
570 | |||
571 | /** |
||
572 | * Revoke any background processes attached to this task. |
||
573 | * |
||
574 | * @param Task $task |
||
575 | * |
||
576 | * @return bool |
||
577 | */ |
||
578 | private function killTask(Task $task) |
||
593 | |||
594 | /** |
||
595 | * @param Rule $rule |
||
596 | * |
||
597 | * @return $this |
||
598 | */ |
||
599 | 1 | public function addRule(Rule $rule) |
|
605 | |||
606 | /** |
||
607 | * @param Rule $rule |
||
608 | * |
||
609 | * @return $this |
||
610 | */ |
||
611 | 1 | public function removeRule(Rule $rule) |
|
617 | |||
618 | public function __destruct() |
||
624 | } |
||
625 |