Passed
Pull Request — master (#9)
by Shinji
01:54
created

WorkerPool::getFreeWorker()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Daemon\Dispatcher;
15
16
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContext;
17
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreator;
18
use Amp\Promise;
0 ignored issues
show
Bug introduced by
The type Amp\Promise was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
final class WorkerPool
21
{
22
    /** @var array<int, PhpReaderContext> */
23
    private array $contexts;
24
25
    /** @var array<int, bool> */
26
    private array $is_free_list;
27
28
    public function __construct(PhpReaderContext ...$contexts)
29
    {
30
        $this->contexts = $contexts;
31
        $this->is_free_list = array_fill(0, count($contexts), true);
32
    }
33
34
    public static function create(PhpReaderContextCreator $creator, int $number): self
35
    {
36
        $contexts = [];
37
        $started = [];
38
        for ($i = 0; $i < $number; $i++) {
39
            $context = $creator->create();
40
            $started[] = $context->start();
41
            $contexts[] = $context;
42
        }
43
        Promise\wait(Promise\all($started));
0 ignored issues
show
Bug introduced by
The function all was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        Promise\wait(/** @scrutinizer ignore-call */ Promise\all($started));
Loading history...
Bug introduced by
The function wait was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        /** @scrutinizer ignore-call */ 
44
        Promise\wait(Promise\all($started));
Loading history...
44
        return new self(...$contexts);
45
    }
46
47
    public function getFreeWorker(): ?PhpReaderContext
48
    {
49
        foreach ($this->contexts as $key => $context) {
50
            if ($this->is_free_list[$key]) {
51
                $this->is_free_list[$key] = false;
52
                return $context;
53
            }
54
        }
55
        return null;
56
    }
57
58
    public function returnWorkerToPool(PhpReaderContext $context_to_return): void
59
    {
60
        foreach ($this->contexts as $key => $context) {
61
            if ($context === $context_to_return) {
62
                $this->is_free_list[$key] = true;
63
            }
64
        }
65
    }
66
}
67