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 | public function __construct() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return static |
||
| 49 | */ |
||
| 50 | public static function create() |
||
| 54 | |||
| 55 | public static function isSupported(): bool |
||
| 62 | |||
| 63 | public function concurrency(int $concurrency): self |
||
| 69 | |||
| 70 | public function timeout(int $timeout): self |
||
| 76 | |||
| 77 | public function autoload(string $autoloader): self |
||
| 83 | |||
| 84 | public function sleepTime(int $sleepTime): self |
||
| 90 | |||
| 91 | public function notify() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param \Spatie\Async\Process\Runnable|callable $process |
||
| 108 | * |
||
| 109 | * @return \Spatie\Async\Process\Runnable |
||
| 110 | */ |
||
| 111 | public function add($process): Runnable |
||
| 125 | |||
| 126 | public function wait(?callable $intermediateCallback = null): array |
||
| 152 | |||
| 153 | public function putInQueue(Runnable $process) |
||
| 159 | |||
| 160 | public function putInProgress(Runnable $process) |
||
| 172 | |||
| 173 | public function markAsFinished(Runnable $process) |
||
| 183 | |||
| 184 | public function markAsTimedOut(Runnable $process) |
||
| 194 | |||
| 195 | public function markAsFailed(Runnable $process) |
||
| 205 | |||
| 206 | public function offsetExists($offset) |
||
| 212 | |||
| 213 | public function offsetGet($offset) |
||
| 217 | |||
| 218 | public function offsetSet($offset, $value) |
||
| 222 | |||
| 223 | public function offsetUnset($offset) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return \Spatie\Async\Process\Runnable[] |
||
| 230 | */ |
||
| 231 | public function getQueue(): array |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return \Spatie\Async\Process\Runnable[] |
||
| 238 | */ |
||
| 239 | public function getInProgress(): array |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return \Spatie\Async\Process\Runnable[] |
||
| 246 | */ |
||
| 247 | public function getFinished(): array |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return \Spatie\Async\Process\Runnable[] |
||
| 254 | */ |
||
| 255 | public function getFailed(): array |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return \Spatie\Async\Process\Runnable[] |
||
| 262 | */ |
||
| 263 | public function getTimeouts(): array |
||
| 267 | |||
| 268 | public function status(): PoolStatus |
||
| 272 | |||
| 273 | protected function registerListener() |
||
| 301 | } |
||
| 302 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.