Complex classes like Pool 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 Pool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Pool implements ArrayAccess |
||
13 | { |
||
14 | public static $forceSynchronous = false; |
||
15 | |||
16 | protected $concurrency = 20; |
||
17 | protected $tasksPerProcess = 1; |
||
18 | protected $timeout = 300; |
||
19 | protected $sleepTime = 50000; |
||
20 | |||
21 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
22 | protected $queue = []; |
||
23 | |||
24 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
25 | protected $inProgress = []; |
||
26 | |||
27 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
28 | protected $finished = []; |
||
29 | |||
30 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
31 | protected $failed = []; |
||
32 | |||
33 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
34 | protected $timeouts = []; |
||
35 | |||
36 | protected $results = []; |
||
37 | |||
38 | protected $status; |
||
39 | |||
40 | protected $stopped = false; |
||
41 | |||
42 | protected $binary = PHP_BINARY; |
||
43 | |||
44 | public function __construct() |
||
45 | { |
||
46 | if (static::isSupported()) { |
||
47 | $this->registerListener(); |
||
48 | } |
||
49 | |||
50 | $this->status = new PoolStatus($this); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return static |
||
55 | */ |
||
56 | public static function create() |
||
57 | { |
||
58 | return new static(); |
||
59 | } |
||
60 | |||
61 | public static function isSupported(): bool |
||
62 | { |
||
63 | return |
||
64 | function_exists('pcntl_async_signals') |
||
65 | && function_exists('posix_kill') |
||
66 | && ! self::$forceSynchronous; |
||
67 | } |
||
68 | |||
69 | public function concurrency(int $concurrency): self |
||
70 | { |
||
71 | $this->concurrency = $concurrency; |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | public function timeout(float $timeout): self |
||
77 | { |
||
78 | $this->timeout = $timeout; |
||
|
|||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | public function autoload(string $autoloader): self |
||
84 | { |
||
85 | ParentRuntime::init($autoloader); |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | public function sleepTime(int $sleepTime): self |
||
91 | { |
||
92 | $this->sleepTime = $sleepTime; |
||
93 | |||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | public function withBinary(string $binary): self |
||
98 | { |
||
99 | $this->binary = $binary; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | public function notify() |
||
118 | |||
119 | /** |
||
120 | * @param \Spatie\Async\Process\Runnable|callable $process |
||
121 | * @param int|null $outputLength |
||
122 | * |
||
123 | * @return \Spatie\Async\Process\Runnable |
||
124 | */ |
||
125 | public function add($process, ?int $outputLength = null): Runnable |
||
126 | { |
||
143 | |||
144 | public function wait(?callable $intermediateCallback = null): array |
||
170 | |||
171 | public function putInQueue(Runnable $process) |
||
177 | |||
178 | public function putInProgress(Runnable $process) |
||
194 | |||
195 | public function markAsFinished(Runnable $process) |
||
205 | |||
206 | public function markAsTimedOut(Runnable $process) |
||
217 | |||
218 | public function markAsFailed(Runnable $process) |
||
228 | |||
229 | public function offsetExists($offset) |
||
235 | |||
236 | public function offsetGet($offset) |
||
240 | |||
241 | public function offsetSet($offset, $value) |
||
245 | |||
246 | public function offsetUnset($offset) |
||
250 | |||
251 | /** |
||
252 | * @return \Spatie\Async\Process\Runnable[] |
||
253 | */ |
||
254 | public function getQueue(): array |
||
258 | |||
259 | /** |
||
260 | * @return \Spatie\Async\Process\Runnable[] |
||
261 | */ |
||
262 | public function getInProgress(): array |
||
266 | |||
267 | /** |
||
268 | * @return \Spatie\Async\Process\Runnable[] |
||
269 | */ |
||
270 | public function getFinished(): array |
||
274 | |||
275 | /** |
||
276 | * @return \Spatie\Async\Process\Runnable[] |
||
277 | */ |
||
278 | public function getFailed(): array |
||
282 | |||
283 | /** |
||
284 | * @return \Spatie\Async\Process\Runnable[] |
||
285 | */ |
||
286 | public function getTimeouts(): array |
||
290 | |||
291 | public function status(): PoolStatus |
||
295 | |||
296 | protected function registerListener() |
||
324 | |||
325 | public function stop() |
||
329 | } |
||
330 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.