Skip to content

Commit 7791f64

Browse files
committed
Swap Vertex ctor $graph and $id attributes
A Vertex can not exist without a Graph instance
1 parent 692f70c commit 7791f64

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

lib/Fhaculty/Graph/Graph.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function createVertex($id = NULL, $returnDuplicate = false)
8282
return $this->vertices->getVertexId($id);
8383
}
8484

85-
return new Vertex($id, $this);
85+
return new Vertex($this, $id);
8686
}
8787

8888
/**
@@ -98,7 +98,7 @@ public function createVertexClone(Vertex $originalVertex)
9898
if ($this->vertices->hasVertexId($id)) {
9999
throw new RuntimeException('Id of cloned vertex already exists');
100100
}
101-
$newVertex = new Vertex($id, $this);
101+
$newVertex = new Vertex($this, $id);
102102
// TODO: properly set attributes of vertex
103103
$newVertex->setLayout($originalVertex->getLayout());
104104
$newVertex->setBalance($originalVertex->getBalance());
@@ -256,7 +256,7 @@ public function createVertices($n)
256256
$vertices = array();
257257
if (is_int($n) && $n >= 0) {
258258
for ($id = $this->getNextId(), $n += $id; $id < $n; ++$id) {
259-
$vertices[$id] = new Vertex($id, $this);
259+
$vertices[$id] = new Vertex($this, $id);
260260
}
261261
} elseif (is_array($n)) {
262262
// array given => check to make sure all given IDs are available (atomic operation)
@@ -275,7 +275,7 @@ public function createVertices($n)
275275

276276
// actually create all requested vertices
277277
foreach ($n as $id) {
278-
$vertices[$id] = new Vertex($id, $this);
278+
$vertices[$id] = new Vertex($this, $id);
279279
}
280280
} else {
281281
throw new InvalidArgumentException('Invalid number of vertices given. Must be non-negative integer or an array of Vertex IDs');

lib/Fhaculty/Graph/Vertex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class Vertex extends Layoutable implements EdgesAggregate
4545
/**
4646
* Create a new Vertex
4747
*
48-
* @param string|int $id identifier used to uniquely identify this vertex in the graph
4948
* @param Graph $graph graph to be added to
49+
* @param string|int $id identifier used to uniquely identify this vertex in the graph
5050
* @see Graph::createVertex() to create new vertices
5151
*/
52-
public function __construct($id, Graph $graph)
52+
public function __construct(Graph $graph, $id)
5353
{
5454
if (!is_int($id) && !is_string($id)) {
5555
throw new InvalidArgumentException('Vertex ID has to be of type integer or string');

tests/Fhaculty/Graph/VertexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testPrecondition()
2121

2222
public function testConstructor()
2323
{
24-
$v2 = new Vertex(2, $this->graph);
24+
$v2 = new Vertex($this->graph, 2);
2525

2626
$this->assertCount(2, $this->graph->getVertices());
2727
$this->assertTrue($this->graph->hasVertex(2));
@@ -34,7 +34,7 @@ public function testConstructor()
3434
*/
3535
public function testCanNotConstructDuplicateVertex()
3636
{
37-
$v2 = new Vertex(1, $this->graph);
37+
$v2 = new Vertex($this->graph, 1);
3838
}
3939

4040
public function testEdges()

0 commit comments

Comments
 (0)