Passed
Push — master ( e847fb...cefdd7 )
by Shinji
03:11 queued 01:31
created

WorkerPool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
nc 1
nop 1
dl 0
loc 5
c 1
b 0
f 1
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
    /** @no-named-arguments */
35
    public function __construct(PhpReaderControllerInterface ...$contexts)
36
    {
37
        $this->contexts = $contexts;
38
        $this->is_free_list = array_fill(0, count($contexts), true);
39
        $this->on_read_list = array_fill(0, count($contexts), false);
40
    }
41
42
    public static function create(
43
        PhpReaderContextCreatorInterface $creator,
44
        int $number,
45
        TargetPhpSettings $target_php_settings,
46
        TraceLoopSettings $loop_settings,
47
        GetTraceSettings $get_trace_settings
48
    ): self {
49
        $contexts = [];
50
        $started = [];
51
        for ($i = 0; $i < $number; $i++) {
52
            $context = $creator->create();
53
            $started[] = $context->start();
54
            $contexts[] = $context;
55
        }
56
        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

56
        /** @scrutinizer ignore-call */ 
57
        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

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