Skip to content

Commit 3e12b71

Browse files
committed
Throw Exceptions for parallel edges
1 parent 203ce7b commit 3e12b71

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Tree/BaseDirected.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function isTree()
4949
catch (UnderflowException $e) {
5050
return false;
5151
}
52+
catch (UnexpectedValueException $e) {
53+
return false;
54+
}
5255

5356
try {
5457
$num = count($this->getVerticesSubtree($root));
@@ -86,6 +89,7 @@ public function getVertexParent(Vertex $vertex)
8689
*
8790
* @param Vertex $vertex
8891
* @return Vertex[]
92+
* @throws UnexpectedValueException if the given $vertex contains invalid / parallel links (check isTree()!)
8993
*/
9094
abstract public function getVerticesChildren(Vertex $vertex);
9195

src/Tree/InTree.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ class InTree extends DirectedTree
1515
{
1616
public function getVerticesChildren(Vertex $vertex)
1717
{
18-
return $vertex->getVerticesEdgeFrom();
18+
$vertices = $vertex->getVerticesEdgeFrom();
19+
if (count($vertices) !== $vertex->getDegreeIn()) {
20+
throw new UnexpectedValueException();
21+
}
22+
23+
return $vertices;
1924
}
2025

2126
protected function getVerticesParent(Vertex $vertex)
2227
{
23-
return $vertex->getVerticesEdgeTo();
28+
$vertices = $vertex->getVerticesEdgeTo();
29+
if (count($vertices) !== $vertex->getDegreeOut()) {
30+
throw new UnexpectedValueException();
31+
}
32+
33+
return $vertices;
2434
}
2535
}

src/Tree/OutTree.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ class OutTree extends DirectedTree
1616
{
1717
public function getVerticesChildren(Vertex $vertex)
1818
{
19-
return $vertex->getVerticesEdgeTo();
19+
$vertices = $vertex->getVerticesEdgeTo();
20+
if (count($vertices) !== $vertex->getDegreeOut()) {
21+
throw new UnexpectedValueException();
22+
}
23+
24+
return $vertices;
2025
}
2126

2227
protected function getVerticesParent(Vertex $vertex)
2328
{
24-
return $vertex->getVerticesEdgeFrom();
29+
$vertices = $vertex->getVerticesEdgeFrom();
30+
if (count($vertices) !== $vertex->getDegreeIn()) {
31+
throw new UnexpectedValueException();
32+
}
33+
34+
return $vertices;
2535
}
2636
}

0 commit comments

Comments
 (0)