Completed
Branch master (d31b6a)
by Bingo
05:22 queued 01:30
created

UniformEdgeSpecifics   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 20
c 1
b 0
f 1
dl 0
loc 136
ccs 24
cts 30
cp 0.8
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getEdgeSet() 0 3 1
A getEdge() 0 6 2
A getEdgeWeight() 0 3 1
A remove() 0 4 2
A getEdgeSource() 0 3 1
A containsEdge() 0 3 1
A add() 0 11 2
A __construct() 0 3 1
A setEdgeWeight() 0 3 1
A getEdgeTarget() 0 3 1
1
<?php
2
3
namespace graphp\edge\specifics;
4
5
use BadMethodCallException;
6
use graphp\graph\GraphInterface;
7
use graphp\edge\EdgeInterface;
8
use graphp\edge\EdgeSet;
9
use graphp\vertex\VertexInterface;
10
11
/**
12
 * Class UniformEdgeSpecifics
13
 *
14
 * @package graphp\edge\specifics
15
 */
16
class UniformEdgeSpecifics implements EdgeSpecificsInterface
17
{
18
    /**
19
     * The edge set
20
     *
21
     * @var array
22
     */
23
    protected $edgeSet;
24
    
25
    /**
26
     * Construct a new specifics
27
     */
28 5
    public function __construct()
29
    {
30 5
        $this->edgeSet = new EdgeSet();
0 ignored issues
show
Documentation Bug introduced by
It seems like new graphp\edge\EdgeSet() of type graphp\edge\EdgeSet is incompatible with the declared type array of property $edgeSet.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31 5
    }
32
    
33
    /**
34
     * Check if the edge exists
35
     *
36
     * @param EdgeInterface $edge - the edge
37
     *
38
     * @return bool
39
     */
40 2
    public function containsEdge(EdgeInterface $edge): bool
41
    {
42 2
        return $this->edgeSet->contains($edge);
43
    }
44
    
45
     /**
46
     * Get the edge set
47
     *
48
     * @return EdgeSet
49
     */
50 2
    public function getEdgeSet(): EdgeSet
51
    {
52 2
        return $this->edgeSet;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->edgeSet returns the type array which is incompatible with the type-hinted return graphp\edge\EdgeSet.
Loading history...
53
    }
54
    
55
    /**
56
     * Remove the edge from the set
57
     *
58
     * @param EdgeInterface $edge - the edge
59
     */
60 1
    public function remove(EdgeInterface $edge): void
61
    {
62 1
        if ($this->edgeSet->contains($edge)) {
63 1
            $this->edgeSet->remove($edge);
64
        }
65 1
    }
66
    
67
    /**
68
     * Get the edge source vertex
69
     *
70
     * @param EdgeInterface $edge - the edge
71
     *
72
     * @return VertexInterface
73
     */
74 2
    public function getEdgeSource(EdgeInterface $edge): VertexInterface
75
    {
76 2
        return $edge->getSource();
0 ignored issues
show
Bug introduced by
The method getSource() does not exist on graphp\edge\EdgeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to graphp\edge\EdgeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        return $edge->/** @scrutinizer ignore-call */ getSource();
Loading history...
77
    }
78
    
79
     /**
80
     * Get the edge target vertex
81
     *
82
     * @param EdgeInterface $edge - the edge
83
     *
84
     * @return VertexInterface
85
     */
86 2
    public function getEdgeTarget(EdgeInterface $edge): VertexInterface
87
    {
88 2
        return $edge->getTarget();
0 ignored issues
show
Bug introduced by
The method getTarget() does not exist on graphp\edge\EdgeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to graphp\edge\EdgeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        return $edge->/** @scrutinizer ignore-call */ getTarget();
Loading history...
89
    }
90
    
91
    /**
92
     * Get the edge weight
93
     *
94
     * @param EdgeInterface $edge - the edge
95
     *
96
     * @return float
97
     */
98
    public function getEdgeWeight(EdgeInterface $edge): float
99
    {
100
        return GraphInterface::DEFAULT_EDGE_WEIGHT;
101
    }
102
    
103
    /**
104
     * Set the edge weight
105
     *
106
     * @param EdgeInterface $edge - the edge
107
     * @param float $weight - the weight
108
     *
109
     * @throws BadMethodCallException
110
     */
111
    public function setEdgeWeight(EdgeInterface $edge, float $weight): void
112
    {
113
        throw new BadMethodCallException("Method is not supported by this type of edge");
114
    }
115
    
116
    /**
117
     * Get the edge
118
     *
119
     * @param EdgeInterface $edge - the edge
120
     *
121
     * @return null|EdgeInterface
122
     */
123 1
    public function getEdge(EdgeInterface $edge): ?EdgeInterface
124
    {
125 1
        if (($id = array_search($edge, $this->edgeSet->getArrayCopy())) !== false) {
126 1
            return $this->edgeSet[$id];
127
        }
128
        return null;
129
    }
130
    
131
    /**
132
     * Add a new edge connecting the specified vertices
133
     * Return true, if edge was added
134
     *
135
     * @param EdgeInterface $edge - the edge
136
     * @param VertexInterface $sourceVertex - the source vertex
137
     * @param VertexInterface $sourceVertex - tge target vertex
138
     *
139
     * @return bool
140
     */
141 2
    public function add(EdgeInterface $edge, VertexInterface $sourceVertex, VertexInterface $targetVertex): bool
142
    {
143 2
        $edge->setSource($sourceVertex);
0 ignored issues
show
Bug introduced by
The method setSource() does not exist on graphp\edge\EdgeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to graphp\edge\EdgeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        $edge->/** @scrutinizer ignore-call */ 
144
               setSource($sourceVertex);
Loading history...
144 2
        $edge->setTarget($targetVertex);
0 ignored issues
show
Bug introduced by
The method setTarget() does not exist on graphp\edge\EdgeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to graphp\edge\EdgeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        $edge->/** @scrutinizer ignore-call */ 
145
               setTarget($targetVertex);
Loading history...
145
146 2
        if (!$this->containsEdge($edge)) {
147 2
            $this->edgeSet[] = $edge;
148 2
            return true;
149
        }
150
151
        return false;
152
    }
153
}
154