| Total Complexity | 6 |
| Total Lines | 40 |
| Duplicated Lines | 0 % |
| Coverage | 73.32% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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()) { |
|
|
|
|||
| 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 |
||
| 54 | } |
||
| 55 | } |
||
| 56 |