CacheProxy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 50
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 3 1
A getCaller() 0 17 5
A get() 0 10 1
A set() 0 10 1
1
<?php
2
3
namespace LeKoala\DebugBar\Proxy;
4
5
use Symfony\Component\Cache\Psr16Cache;
6
7
class CacheProxy extends Psr16Cache
8
{
9
    protected static $data = [];
10
11
    public function set($key, $value, $ttl = null): bool
12
    {
13
        self::$data[__FUNCTION__ . $key] = [
14
            "key" => $key,
15
            "type" => __FUNCTION__,
16
            "value" => $value,
17
            "ttl" => $ttl,
18
            "caller" => $this->getCaller(),
19
        ];
20
        return parent::set($key, $value, $ttl);
21
    }
22
23
    public function get($key, $default = null): mixed
24
    {
25
        $value = parent::get($key, $default);
26
        self::$data[__FUNCTION__ . $key] = [
27
            "key" => $key,
28
            "type" => __FUNCTION__,
29
            "value" => $value,
30
            "caller" => $this->getCaller(),
31
        ];
32
        return $value;
33
    }
34
35
    private function getCaller(): string
36
    {
37
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
38
        $caller = "";
39
        $ignore = ["set", "get", "setCacheValue", "getCacheValue", "getCaller"];
40
        foreach ($trace as $t) {
41
            if (in_array($t['function'], $ignore)) {
42
                continue;
43
            }
44
            if (isset($t['file'])) {
45
                $caller = basename($t['file']) . ':' . $t['line'];
46
            } elseif (isset($t['class'])) {
47
                $caller = $t['class'];
48
            }
49
            break;
50
        }
51
        return $caller;
52
    }
53
54
    public static function getData(): array
55
    {
56
        return self::$data;
57
    }
58
}
59