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 const DEFAULT_PHP_BINARY = 'php'; |
||
15 | |||
16 | public static $forceSynchronous = false; |
||
17 | |||
18 | protected $concurrency = 20; |
||
19 | protected $tasksPerProcess = 1; |
||
20 | protected $timeout = 300; |
||
21 | protected $sleepTime = 50000; |
||
22 | |||
23 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
24 | protected $queue = []; |
||
25 | |||
26 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
27 | protected $inProgress = []; |
||
28 | |||
29 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
30 | protected $finished = []; |
||
31 | |||
32 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
33 | protected $failed = []; |
||
34 | |||
35 | /** @var \Spatie\Async\Process\Runnable[] */ |
||
36 | protected $timeouts = []; |
||
37 | |||
38 | protected $results = []; |
||
39 | |||
40 | protected $status; |
||
41 | |||
42 | protected $stopped = false; |
||
43 | |||
44 | protected $binary = self::DEFAULT_PHP_BINARY; |
||
45 | |||
46 | public function __construct() |
||
54 | |||
55 | /** |
||
56 | * @return static |
||
57 | */ |
||
58 | public static function create() |
||
62 | |||
63 | public static function isSupported(): bool |
||
70 | |||
71 | public function concurrency(int $concurrency): self |
||
77 | |||
78 | public function timeout(float $timeout): self |
||
84 | |||
85 | public function withBinary(string $binary = self::DEFAULT_PHP_BINARY): self |
||
91 | |||
92 | public function autoload(string $autoloader): self |
||
98 | |||
99 | public function sleepTime(int $sleepTime): self |
||
105 | |||
106 | public function notify() |
||
120 | |||
121 | /** |
||
122 | * @param \Spatie\Async\Process\Runnable|callable $process |
||
123 | * @param int|null $outputLength |
||
124 | * |
||
125 | * @return \Spatie\Async\Process\Runnable |
||
126 | */ |
||
127 | public function add($process, ?int $outputLength = null): Runnable |
||
142 | |||
143 | public function wait(?callable $intermediateCallback = null): array |
||
169 | |||
170 | public function putInQueue(Runnable $process) |
||
176 | |||
177 | public function putInProgress(Runnable $process) |
||
193 | |||
194 | public function markAsFinished(Runnable $process) |
||
204 | |||
205 | public function markAsTimedOut(Runnable $process) |
||
215 | |||
216 | public function markAsFailed(Runnable $process) |
||
226 | |||
227 | public function offsetExists($offset) |
||
233 | |||
234 | public function offsetGet($offset) |
||
238 | |||
239 | public function offsetSet($offset, $value) |
||
243 | |||
244 | public function offsetUnset($offset) |
||
248 | |||
249 | /** |
||
250 | * @return \Spatie\Async\Process\Runnable[] |
||
251 | */ |
||
252 | public function getQueue(): array |
||
256 | |||
257 | /** |
||
258 | * @return \Spatie\Async\Process\Runnable[] |
||
259 | */ |
||
260 | public function getInProgress(): array |
||
264 | |||
265 | /** |
||
266 | * @return \Spatie\Async\Process\Runnable[] |
||
267 | */ |
||
268 | public function getFinished(): array |
||
272 | |||
273 | /** |
||
274 | * @return \Spatie\Async\Process\Runnable[] |
||
275 | */ |
||
276 | public function getFailed(): array |
||
280 | |||
281 | /** |
||
282 | * @return \Spatie\Async\Process\Runnable[] |
||
283 | */ |
||
284 | public function getTimeouts(): array |
||
288 | |||
289 | public function status(): PoolStatus |
||
293 | |||
294 | protected function registerListener() |
||
322 | |||
323 | public function stop() |
||
327 | } |
||
328 |
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.