Passed
Pull Request — master (#9)
by Shinji
02:03
created

TargetProcessList::getDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
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
final class TargetProcessList implements TargetProcessListInterface
17
{
18
    /** @var int[] */
19
    private array $pid_list;
20
21
    public function __construct(int ...$pid_list)
22
    {
23
        $this->pid_list = $pid_list;
24
    }
25
26
    public function pickOne(): ?int
27
    {
28
        if ($this->pid_list === []) {
29
            return null;
30
        }
31
        $key = array_rand($this->pid_list);
32
        $value = $this->pid_list[$key];
33
        unset($this->pid_list[$key]);
34
        return $value;
35
    }
36
37
    public function putOne(int $pid): void
38
    {
39
        $this->pid_list[] = $pid;
40
    }
41
42
    public function getDiff(TargetProcessListInterface $compare_list): self
43
    {
44
        return new self(...array_diff($this->pid_list, $compare_list->getArray()));
45
    }
46
47
    /**
48
     * @return int[]
49
     */
50
    public function getArray(): array
51
    {
52
        return $this->pid_list;
53
    }
54
}
55