Completed
Push — 0.6.x ( 6a35ea...8b7bff )
by Shinji
16s queued 13s
created

ProcessDescriptorCache::removeDisappeared()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
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\Searcher\Worker;
15
16
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessDescriptor;
17
18
final class ProcessDescriptorCache
19
{
20
    /** @var array<int, TargetProcessDescriptor> */
21
    private $cache = [];
22
23
    public function set(TargetProcessDescriptor $target_process_descriptor): void
24
    {
25
        $this->cache[$target_process_descriptor->pid] = $target_process_descriptor;
26
    }
27
28
    public function setInvalid(int $pid): void
29
    {
30
        $this->cache[$pid] = TargetProcessDescriptor::getInvalid();
31
    }
32
33
    public function get(int $pid): ?TargetProcessDescriptor
34
    {
35
        return $this->cache[$pid] ?? null;
36
    }
37
38
    public function removeDisappeared(int ...$pids): void
39
    {
40
        $cached = array_keys($this->cache);
41
        $diff = array_diff($cached, $pids);
42
        foreach ($diff as $disappeared) {
43
            unset($this->cache[$disappeared]);
44
        }
45
    }
46
}
47