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'; |
||
| 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) |
||
| 216 | |||
| 217 | public function markAsFailed(Runnable $process) |
||
| 227 | |||
| 228 | public function offsetExists($offset) |
||
| 234 | |||
| 235 | public function offsetGet($offset) |
||
| 239 | |||
| 240 | public function offsetSet($offset, $value) |
||
| 244 | |||
| 245 | public function offsetUnset($offset) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return \Spatie\Async\Process\Runnable[] |
||
| 252 | */ |
||
| 253 | public function getQueue(): array |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return \Spatie\Async\Process\Runnable[] |
||
| 260 | */ |
||
| 261 | public function getInProgress(): array |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return \Spatie\Async\Process\Runnable[] |
||
| 268 | */ |
||
| 269 | public function getFinished(): array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return \Spatie\Async\Process\Runnable[] |
||
| 276 | */ |
||
| 277 | public function getFailed(): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return \Spatie\Async\Process\Runnable[] |
||
| 284 | */ |
||
| 285 | public function getTimeouts(): array |
||
| 289 | |||
| 290 | public function status(): PoolStatus |
||
| 294 | |||
| 295 | protected function registerListener() |
||
| 323 | |||
| 324 | public function stop() |
||
| 328 | } |
||
| 329 |
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.