Passed
Pull Request — master (#31)
by Shinji
04:07 queued 02:36
created

WorkerPool::setOnRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
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 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...
17
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreatorInterface;
18
use PhpProfiler\Inspector\Daemon\Reader\Controller\PhpReaderControllerInterface;
19
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettings;
20
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
21
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;
22
23
final class WorkerPool implements WorkerPoolInterface
24
{
25
    /** @var array<int, PhpReaderControllerInterface> */
26
    private array $contexts;
27
28
    /** @var array<int, bool> */
29
    private array $is_free_list;
30
31
    /** @var array<int, bool> */
32
    private array $on_read_list;
33
34
    public function __construct(PhpReaderControllerInterface ...$contexts)
35
    {
36
        $this->contexts = $contexts;
37
        $this->is_free_list = array_fill(0, count($contexts), true);
38
        $this->on_read_list = array_fill(0, count($contexts), false);
39
    }
40
41
    public static function create(
42
        PhpReaderContextCreatorInterface $creator,
43
        int $number,
44
        TargetPhpSettings $target_php_settings,
45
        TraceLoopSettings $loop_settings,
46
        GetTraceSettings $get_trace_settings
47
    ): self {
48
        $contexts = [];
49
        $started = [];
50
        for ($i = 0; $i < $number; $i++) {
51
            $context = $creator->create();
52
            $started[] = $context->start();
53
            $contexts[] = $context;
54
        }
55
        Promise\wait(Promise\all($started));
0 ignored issues
show
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

55
        /** @scrutinizer ignore-call */ 
56
        Promise\wait(Promise\all($started));
Loading history...
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

55
        Promise\wait(/** @scrutinizer ignore-call */ Promise\all($started));
Loading history...
56
        $send_settings = [];
57
        for ($i = 0; $i < $number; $i++) {
58
            $send_settings[] = $contexts[$i]->sendSettings(
59
                $target_php_settings,
60
                $loop_settings,
61
                $get_trace_settings
62
            );
63
        }
64
        Promise\wait(Promise\all($send_settings));
65
66
        return new self(...$contexts);
67
    }
68
69
    public function getFreeWorker(): ?PhpReaderControllerInterface
70
    {
71
        foreach ($this->contexts as $key => $context) {
72
            if ($this->is_free_list[$key]) {
73
                $this->is_free_list[$key] = false;
74
                return $context;
75
            }
76
        }
77
        return null;
78
    }
79
80
    /**
81
     * @return iterable<int, PhpReaderControllerInterface>
82
     */
83
    public function getWorkers(): iterable
84
    {
85
        foreach ($this->contexts as $key => $context) {
86
            yield $key => $context;
87
        }
88
    }
89
90
    public function returnWorkerToPool(PhpReaderControllerInterface $context_to_return): void
91
    {
92
        foreach ($this->contexts as $key => $context) {
93
            if ($context === $context_to_return) {
94
                $this->is_free_list[$key] = true;
95
            }
96
        }
97
    }
98
}
99