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 | class ProcessManager implements Manager |
||
20 | { |
||
21 | /** |
||
22 | * @var Task[] |
||
23 | */ |
||
24 | protected $waiting = array(); |
||
25 | |||
26 | /** |
||
27 | * @var Task[] |
||
28 | */ |
||
29 | protected $running = array(); |
||
30 | |||
31 | /** |
||
32 | * @var null|SplObjectStorage |
||
33 | */ |
||
34 | protected $timings = null; |
||
35 | |||
36 | /** |
||
37 | * @var null|string |
||
38 | */ |
||
39 | protected $logPath; |
||
40 | |||
41 | /** |
||
42 | * @var null|Rules |
||
43 | */ |
||
44 | protected $rules; |
||
45 | |||
46 | /** |
||
47 | * @var null|Shell |
||
48 | */ |
||
49 | protected $shell; |
||
50 | |||
51 | /** |
||
52 | * @var null|string |
||
53 | */ |
||
54 | protected $binary; |
||
55 | |||
56 | /** |
||
57 | * @var null|string |
||
58 | */ |
||
59 | protected $worker; |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | * |
||
64 | * @param Task $task |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function addTask(Task $task) |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function tick() |
||
134 | |||
135 | /** |
||
136 | * Stops sibling processes of a task. |
||
137 | * |
||
138 | * @param Task $task |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | protected function stopSiblingTasks(Task $task) |
||
156 | |||
157 | /** |
||
158 | * Checks whether a new task can be run. |
||
159 | * |
||
160 | * @param Task $task |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | protected function canRunTask(Task $task) |
||
186 | |||
187 | /** |
||
188 | * Gets the load profile related to a task. |
||
189 | * |
||
190 | * @param Task $task |
||
191 | * @param array $processes |
||
192 | * |
||
193 | * @return Profile |
||
194 | */ |
||
195 | protected function getProfileForProcesses(Task $task, array $processes) |
||
217 | |||
218 | /** |
||
219 | * Gets processor and memory stats for a list of processes. |
||
220 | * |
||
221 | * @param Process[] $processes |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | protected function getStatsForProcesses(array $processes) |
||
257 | |||
258 | /** |
||
259 | * Gets or creates a Shell instance. |
||
260 | * |
||
261 | * @return Shell |
||
262 | */ |
||
263 | public function getShell() |
||
271 | |||
272 | /** |
||
273 | * @param Shell $shell |
||
274 | * |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function setShell(Shell $shell) |
||
283 | |||
284 | /** |
||
285 | * Creates a new Shell instance. |
||
286 | * |
||
287 | * @return Shell |
||
288 | */ |
||
289 | protected function newShell() |
||
293 | |||
294 | /** |
||
295 | * Creates a new Profile instance. |
||
296 | * |
||
297 | * @return Profile |
||
298 | */ |
||
299 | protected function newProfile() |
||
303 | |||
304 | /** |
||
305 | * Gets or creates a new Rules instance. |
||
306 | * |
||
307 | * @return Rules |
||
308 | */ |
||
309 | public function getRules() |
||
317 | |||
318 | /** |
||
319 | * @param Rules $rules |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | public function setRules(Rules $rules) |
||
329 | |||
330 | /** |
||
331 | * Creates a new Rules instance. |
||
332 | * |
||
333 | * @return Rules |
||
334 | */ |
||
335 | protected function newRules() |
||
339 | |||
340 | /** |
||
341 | * @param string $binary |
||
342 | * |
||
343 | * @return $this |
||
344 | */ |
||
345 | public function setBinary($binary) |
||
351 | |||
352 | /** |
||
353 | * Gets the path of the PHP runtime. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getBinary() |
||
365 | |||
366 | /** |
||
367 | * @param string $worker |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setWorker($worker) |
||
377 | |||
378 | /** |
||
379 | * Gets the path of the worker script. |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getWorker() |
||
391 | |||
392 | /** |
||
393 | * Gets the path to write stdout to. |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | protected function getStdOut() |
||
405 | |||
406 | /** |
||
407 | * @return null|string |
||
408 | */ |
||
409 | public function getLogPath() |
||
413 | |||
414 | /** |
||
415 | * @param string $logPath |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function setLogPath($logPath) |
||
425 | |||
426 | /** |
||
427 | * Gets the path to write stderr to. |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function getStdErr() |
||
439 | |||
440 | /** |
||
441 | * Gets a string representation of a task, to pass to the worker script. |
||
442 | * |
||
443 | * @param Task $task |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | protected function getTaskString(Task $task) |
||
451 | |||
452 | /** |
||
453 | * Checks whether a task can be removed from the list of running processes. |
||
454 | * |
||
455 | * @param Task $task |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | protected function canRemoveTask(Task $task) |
||
489 | |||
490 | /** |
||
491 | * Check if the given task is expired |
||
492 | * |
||
493 | * @param Task $task |
||
494 | * @return boolean |
||
495 | */ |
||
496 | protected function isTaskExpired(Task $task) { |
||
508 | |||
509 | /** |
||
510 | * Check if the given task is cancelled. |
||
511 | * |
||
512 | * @param Task $task |
||
513 | * |
||
514 | * @return bool |
||
515 | */ |
||
516 | protected function isTaskCancelled(Task $task) |
||
524 | |||
525 | /** |
||
526 | * Revoke any background processes attached to this task. |
||
527 | * |
||
528 | * @param Task $task |
||
529 | * |
||
530 | * @return bool |
||
531 | */ |
||
532 | protected function killTask(Task $task) |
||
544 | |||
545 | /** |
||
546 | * @param Rule $rule |
||
547 | * |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function addRule(Rule $rule) |
||
556 | |||
557 | /** |
||
558 | * @param Rule $rule |
||
559 | * |
||
560 | * @return $this |
||
561 | */ |
||
562 | public function removeRule(Rule $rule) |
||
568 | } |
||
569 |