TraceCache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

11 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 4 1
A __construct() 0 4 1
A updateCacheKey() 0 5 2
A clearCache() 0 3 1
A hp$0 ➔ getDereferencer() 0 20 2
setCache() 0 3 ?
A hp$0 ➔ getCache() 0 4 1
A hp$0 ➔ deref() 0 11 2
getDereferencer() 0 20 ?
A hp$0 ➔ setCache() 0 3 1
getCache() 0 4 ?
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Lib\PhpProcessReader\CallTraceReader;
15
16
use Reli\Lib\Process\Pointer\Dereferencable as T;
17
use Reli\Lib\Process\Pointer\Dereferencer;
18
use Reli\Lib\Process\Pointer\Pointer;
19
20
final class TraceCache
21
{
22
    /** @param array<string, array<int, \Reli\Lib\Process\Pointer\Dereferencable>> $cache */
23
    public function __construct(
24
        private float $key = 0,
25
        private array $cache = [],
26
    ) {
27
    }
28
29
    public function clearCache(): void
30
    {
31
        $this->cache = [];
32
    }
33
34
    public function updateCacheKey(float $key): void
35
    {
36
        if ($this->key !== $key) {
37
            $this->key = $key;
38
            $this->clearCache();
39
        }
40
    }
41
42
    public function getDereferencer(Dereferencer $dereferencer): Dereferencer
43
    {
44
        return new class ($dereferencer, $this) implements Dereferencer {
45
            public function __construct(
46
                private Dereferencer $dereferencer,
47
                private TraceCache $trace_cache,
48
            ) {
49
            }
50
51
            public function deref(Pointer $pointer): mixed
52
            {
53
                $item = $this->trace_cache->getCache($pointer);
54
                if (is_null($item)) {
55
                    $item = $this->dereferencer->deref($pointer);
56
                    $this->trace_cache->setCache(
57
                        $pointer,
58
                        $item,
59
                    );
60
                }
61
                return $item;
62
            }
63
        };
64
    }
65
66
    /**
67
     * @template TCached of \Reli\Lib\Process\Pointer\Dereferencable
68
     * @param Pointer<TCached> $pointer
69
     * @param TCached $item
0 ignored issues
show
Bug introduced by
The type Reli\Lib\PhpProcessReader\CallTraceReader\TCached 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...
70
     */
71
    public function setCache(Pointer $pointer, mixed $item): void
72
    {
73
        $this->cache[$pointer->type][$pointer->address] = $item;
74
    }
75
76
    /**
77
     * @template TCached of \Reli\Lib\Process\Pointer\Dereferencable
78
     * @param Pointer<TCached> $pointer
79
     * @return TCached|null
80
     */
81
    public function getCache(Pointer $pointer): mixed
82
    {
83
        /** @var TCached|null */
84
        return $this->cache[$pointer->type][$pointer->address] ?? null;
85
    }
86
}
87