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

TargetProcessList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 12
dl 0
loc 37
c 2
b 0
f 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A putOne() 0 3 1
A getArray() 0 3 1
A pickOne() 0 9 2
A getDiff() 0 3 1
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