PredictionContextCache   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 40
ccs 11
cts 15
cp 0.7332
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A length() 0 3 1
A __construct() 0 3 1
A get() 0 3 1
A add() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antlr\Antlr4\Runtime\PredictionContexts;
6
7
use Antlr\Antlr4\Runtime\Utils\Map;
8
9
/**
10
 * Used to cache {@see PredictionContext} objects. Its used for the shared
11
 * context cash associated with contexts in DFA states. This cache
12
 * can be used for both lexers and parsers.
13
 */
14
class PredictionContextCache
15
{
16
    /** @var Map */
17
    protected $cache;
18
19 1
    public function __construct()
20
    {
21 1
        $this->cache = new Map();
22 1
    }
23
24
    /**
25
     * Add a context to the cache and return it. If the context already exists,
26
     * return that one instead and do not add a new context to the cache.
27
     * Protect shared cache from unsafe thread access.
28
     */
29 1
    public function add(PredictionContext $ctx) : PredictionContext
30
    {
31 1
        if ($ctx === PredictionContext::empty()) {
0 ignored issues
show
introduced by
The condition $ctx === Antlr\Antlr4\Ru...dictionContext::empty() is always false.
Loading history...
32
            return $ctx;
33
        }
34
35 1
        $existing = $this->cache->get($ctx);
36
37 1
        if ($existing !== null) {
38
            return $existing;
39
        }
40
41 1
        $this->cache->put($ctx, $ctx);
42
43 1
        return $ctx;
44
    }
45
46 2
    public function get(PredictionContext $ctx) : ?PredictionContext
47
    {
48 2
        return $this->cache->get($ctx);
49
    }
50
51
    public function length() : int
52
    {
53
        return $this->cache->count();
54
    }
55
}
56