UniqueCache::exists()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 11
nc 6
nop 3
1
<?php
2
3
namespace Knp\FriendlyContexts\Utils;
4
5
class UniqueCache
6
{
7
    private $cache = [];
8
9
    public function exists($className, $field, $value)
10
    {
11
        if (!isset($this->cache[$className])) {
12
            return false;
13
        }
14
15
        if (!is_array($this->cache[$className]) || !isset($this->cache[$className][$field])) {
16
            return false;
17
        }
18
19
        if (!is_array($this->cache[$className][$field])) {
20
            return false;
21
        }
22
23
        foreach ($this->cache[$className][$field] as $cacheValue) {
24
            if ($value === $cacheValue) {
25
                return true;
26
            }
27
        }
28
29
        return false;
30
    }
31
32
    public function generate($className, $field, $callback)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
33
    {
34
        do {
35
            $value = $callback();
36
        } while ($this->exists($className, $field, $value));
37
38
        if (!isset($this->cache[$className])) {
39
            $this->cache[$className] = [];
40
        }
41
42
        if (!isset($this->cache[$className][$field])) {
43
            $this->cache[$className][$field] = [];
44
        }
45
46
        $this->cache[$className][$field][] = $value;
47
48
        return $value;
49
    }
50
51
    public function clear()
52
    {
53
        $this->cache = [];
54
    }
55
}
56