Dictionary   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A empty() 0 4 1
A has() 0 4 1
A get() 0 8 2
A add() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic\DataStructure;
6
7
use Fundic\DataStructure\Maybe\Maybe;
8
9
final class Dictionary
10
{
11
    private $values = [];
12
13
    private function __construct()
14
    {
15
    }
16
17
    public static function empty() : self
18
    {
19
        return new self();
20
    }
21
22
    public function has(string $id) : bool
23
    {
24
        return array_key_exists($id, $this->values);
25
    }
26
27
    public function get(string $id) : Maybe
28
    {
29
        if (array_key_exists($id, $this->values)) {
30
            return Maybe::just($this->values[$id]);
31
        }
32
33
        return Maybe::nothing();
34
    }
35
36
    public function add(string $id, $value) : self
37
    {
38
        $instance = clone $this;
39
        $instance->values[$id] = $value;
40
41
        return $instance;
42
    }
43
}
44