VertexMap   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 71
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A __construct() 0 3 1
A keySet() 0 3 1
A put() 0 5 1
A remove() 0 3 1
A offsetUnset() 0 5 1
1
<?php
2
3
namespace Graphp\vertex;
4
5
use ArrayObject;
6
use Graphp\Edge\EdgeContainerInterface;
7
8
/**
9
 * Class VertexMap
10
 *
11
 * @package Graphp\Vertex
12
 */
13
class VertexMap extends ArrayObject
14
{
15
    private $vertices = [];
16
17
    /**
18
     * Construct a vertex map. It maps vertices to corresponding edge containers
19
     *
20
     * @param array $input - array of vertex - edge container pairs
21
     */
22 42
    public function __construct(array $input = [])
23
    {
24 42
        parent::__construct($input, ArrayObject::ARRAY_AS_PROPS);
25 42
    }
26
27
    /**
28
     * Get the edge container of the specified vertex
29
     *
30
     * @return null|EdgeContainerInterface
31
     */
32 33
    public function get(VertexInterface $vertex): ?EdgeContainerInterface
33
    {
34 33
        $id = $vertex->getHash();
35 33
        if (array_key_exists($id, $this->getArrayCopy())) {
36 33
            return $this->getArrayCopy()[$id];
37
        }
38 1
        return null;
39
    }
40
41
    /**
42
     * Put a new value to vertexmap
43
     *
44
     * @param VertexInterface $vertex - the vertex
45
     * @param EdgeContainerInterface $ec - the edge container
46
     */
47 38
    public function put(VertexInterface $vertex, ?EdgeContainerInterface $ec = null): void
48
    {
49 38
        $id = $vertex->getHash();
50 38
        parent::offsetSet($id, $ec);
51 38
        $this->vertices[$id] = $vertex;
52 38
    }
53
54
    /**
55
     * Remove a vertex from the vertexmap
56
     *
57
     * @param VertexInterface $vertex - niddle
58
     */
59 5
    public function remove(VertexInterface $vertex): void
60
    {
61 5
        $this->offsetUnset($vertex->getHash());
62 5
    }
63
64
    /**
65
     * Unset VertexMap value by key
66
     *
67
     * @param mixed $offset - array offset
68
     */
69 5
    public function offsetUnset($offset): void
70
    {
71 5
        $iter = $this->getIterator();
72 5
        $iter->offsetUnset($offset);
73 5
        unset($this->vertices[$offset]);
74 5
    }
75
76
    /**
77
     * Get array of vertices
78
     *
79
     * @return array
80
     */
81 38
    public function keySet(): VertexSet
82
    {
83 38
        return new VertexSet(array_values($this->vertices));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Graphp\vertex...alues($this->vertices)) returns the type Graphp\vertex\VertexSet which is incompatible with the documented return type array.
Loading history...
84
    }
85
}
86