Skip to content

Commit 311c9f3

Browse files
committed
PSR-2 compliance
1 parent d51920a commit 311c9f3

File tree

5 files changed

+331
-281
lines changed

5 files changed

+331
-281
lines changed

src/SchemaAnalyzer.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ class SchemaAnalyzer
5050
private $cachePrefix;
5151

5252
/**
53-
* Nested arrays containing table => column => cost
53+
* Nested arrays containing table => column => cost.
54+
*
5455
* @var float[][]
5556
*/
5657
private $alteredCosts = [];
5758

5859
/**
59-
* Array containing table cost
60+
* Array containing table cost.
61+
*
6062
* @var float[]
6163
*/
6264
private $alteredTableCosts = [];
@@ -175,7 +177,7 @@ private function isJunctionTable(Table $table)
175177
*/
176178
public function getShortestPath($fromTable, $toTable)
177179
{
178-
return $this->fromCache($this->cachePrefix.'_shortest_'.$fromTable.'```'.$toTable, function() use ($fromTable, $toTable) {
180+
return $this->fromCache($this->cachePrefix.'_shortest_'.$fromTable.'```'.$toTable, function () use ($fromTable, $toTable) {
179181
return $this->getShortestPathWithoutCache($fromTable, $toTable);
180182
});
181183
}
@@ -255,8 +257,8 @@ private function buildSchemaGraph()
255257
foreach ($table->getForeignKeys() as $fk) {
256258
// Create an undirected edge, with weight = 1
257259
$edge = $graph->getVertex($table->getName())->createEdge($graph->getVertex($fk->getForeignTableName()));
258-
if (isset($this->alteredCosts[$fk->getLocalTable()->getName()][implode(',',$fk->getLocalColumns())])) {
259-
$cost = $this->alteredCosts[$fk->getLocalTable()->getName()][implode(',',$fk->getLocalColumns())];
260+
if (isset($this->alteredCosts[$fk->getLocalTable()->getName()][implode(',', $fk->getLocalColumns())])) {
261+
$cost = $this->alteredCosts[$fk->getLocalTable()->getName()][implode(',', $fk->getLocalColumns())];
260262
} elseif ($this->isInheritanceRelationship($fk)) {
261263
$cost = self::$WEIGHT_INHERITANCE_FK;
262264
} else {
@@ -388,21 +390,25 @@ private function getTextualPath(array $path, Vertex $startVertex)
388390
*
389391
* @param string $tableName
390392
* @param string $columnName
391-
* @param float $cost
393+
* @param float $cost
394+
*
392395
* @return $this
393396
*/
394-
public function setForeignKeyCost($tableName, $columnName, $cost) {
397+
public function setForeignKeyCost($tableName, $columnName, $cost)
398+
{
395399
$this->alteredCosts[$tableName][$columnName] = $cost;
396400
}
397401

398402
/**
399403
* Sets the cost modifier of a table.
400404
*
401405
* @param string $tableName
402-
* @param float $cost
406+
* @param float $cost
407+
*
403408
* @return $this
404409
*/
405-
public function setTableCostModifier($tableName, $cost) {
410+
public function setTableCostModifier($tableName, $cost)
411+
{
406412
$this->alteredTableCosts[$tableName] = $cost;
407413
}
408414

@@ -411,7 +417,8 @@ public function setTableCostModifier($tableName, $cost) {
411417
*
412418
* @param array<string, float> $tableCosts The key is the table name, the value is the cost modifier.
413419
*/
414-
public function setTableCostModifiers(array $tableCosts) {
420+
public function setTableCostModifiers(array $tableCosts)
421+
{
415422
$this->alteredTableCosts = $tableCosts;
416423
}
417424

@@ -420,17 +427,21 @@ public function setTableCostModifiers(array $tableCosts) {
420427
*
421428
* @param array<string, array<string, float>> $fkCosts First key is the table name, second key is the column name, the value is the cost.
422429
*/
423-
public function setForeignKeyCosts(array $fkCosts) {
430+
public function setForeignKeyCosts(array $fkCosts)
431+
{
424432
$this->alteredCosts = $fkCosts;
425433
}
426434

427435
/**
428436
* Returns true if this foreign key represents an inheritance relationship,
429437
* i.e. if this foreign key is based on a primary key.
438+
*
430439
* @param ForeignKeyConstraint $fk
440+
*
431441
* @return true
432442
*/
433-
private function isInheritanceRelationship(ForeignKeyConstraint $fk) {
443+
private function isInheritanceRelationship(ForeignKeyConstraint $fk)
444+
{
434445
if (!$fk->getLocalTable()->hasPrimaryKey()) {
435446
return false;
436447
}
@@ -449,10 +460,12 @@ private function isInheritanceRelationship(ForeignKeyConstraint $fk) {
449460
* This function will return null if there is no parent table.
450461
*
451462
* @param string $tableName
463+
*
452464
* @return string|null
453465
*/
454-
public function getParentTable($tableName) {
455-
return $this->fromCache($this->cachePrefix.'_parent_'.$tableName, function() use ($tableName) {
466+
public function getParentTable($tableName)
467+
{
468+
return $this->fromCache($this->cachePrefix.'_parent_'.$tableName, function () use ($tableName) {
456469
return $this->getParentTableWithoutCache($tableName);
457470
});
458471
}
@@ -463,16 +476,19 @@ public function getParentTable($tableName) {
463476
* This function will return null if there is no parent table.
464477
*
465478
* @param string $tableName
479+
*
466480
* @return string|null
467481
*/
468-
private function getParentTableWithoutCache($tableName) {
482+
private function getParentTableWithoutCache($tableName)
483+
{
469484
$table = $this->getSchema()->getTable($tableName);
470485
foreach ($table->getForeignKeys() as $fk) {
471486
if ($this->isInheritanceRelationship($fk)) {
472487
return $fk->getForeignTableName();
473488
}
474489
}
475-
return null;
490+
491+
return;
476492
}
477493

478494
/**
@@ -481,10 +497,12 @@ private function getParentTableWithoutCache($tableName) {
481497
* This function will return an empty array if there are no children tables.
482498
*
483499
* @param string $tableName
500+
*
484501
* @return string[]
485502
*/
486-
public function getChildrenTables($tableName) {
487-
return $this->fromCache($this->cachePrefix.'_children_'.$tableName, function() use ($tableName) {
503+
public function getChildrenTables($tableName)
504+
{
505+
return $this->fromCache($this->cachePrefix.'_children_'.$tableName, function () use ($tableName) {
488506
return $this->getChildrenTablesWithoutCache($tableName);
489507
});
490508
}
@@ -495,9 +513,11 @@ public function getChildrenTables($tableName) {
495513
* This function will return an empty array if there are no children tables.
496514
*
497515
* @param string $tableName
516+
*
498517
* @return string[]
499518
*/
500-
private function getChildrenTablesWithoutCache($tableName) {
519+
private function getChildrenTablesWithoutCache($tableName)
520+
{
501521
$schema = $this->getSchema();
502522
$children = [];
503523
foreach ($schema->getTables() as $table) {
@@ -510,17 +530,20 @@ private function getChildrenTablesWithoutCache($tableName) {
510530
}
511531
}
512532
}
533+
513534
return $children;
514535
}
515536

516537
/**
517-
* Returns an item from cache or computes it using $closure and puts it in cache
538+
* Returns an item from cache or computes it using $closure and puts it in cache.
518539
*
519-
* @param string $key
540+
* @param string $key
520541
* @param callable $closure
542+
*
521543
* @return mixed
522544
*/
523-
private function fromCache($key, callable $closure) {
545+
private function fromCache($key, callable $closure)
546+
{
524547
$item = $this->cache->fetch($key);
525548
if ($item === false) {
526549
$item = $closure();

tests/MultiDijkstraTest.php

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
<?php
2+
23
namespace Mouf\Database\SchemaAnalyzer;
34

4-
use Doctrine\Common\Cache\ArrayCache;
5-
use Doctrine\DBAL\Schema\Schema;
65
use Fhaculty\Graph\Graph;
76
use Fhaculty\Graph\Vertex;
87
use Fhaculty\Graph\Edge;
98

109
class MultiDijkstraTest extends \PHPUnit_Framework_TestCase
1110
{
12-
public function testDijkstra() {
11+
public function testDijkstra()
12+
{
1313
$graph = new Graph();
1414

15-
$a = $graph->createVertex("a");
16-
$b = $graph->createVertex("b");
17-
$c = $graph->createVertex("c");
18-
$d = $graph->createVertex("d");
19-
$e = $graph->createVertex("e");
20-
$f = $graph->createVertex("f");
21-
$g = $graph->createVertex("g");
22-
$h = $graph->createVertex("h");
23-
$i = $graph->createVertex("i");
24-
$j = $graph->createVertex("j");
15+
$a = $graph->createVertex('a');
16+
$b = $graph->createVertex('b');
17+
$c = $graph->createVertex('c');
18+
$d = $graph->createVertex('d');
19+
$e = $graph->createVertex('e');
20+
$f = $graph->createVertex('f');
21+
$g = $graph->createVertex('g');
22+
$h = $graph->createVertex('h');
23+
$i = $graph->createVertex('i');
24+
$j = $graph->createVertex('j');
2525

2626
$a->createEdge($b)->setWeight(85);
2727
$a->createEdge($c)->setWeight(217);
@@ -50,13 +50,14 @@ public function testDijkstra() {
5050
/**
5151
* @expectedException \Mouf\Database\SchemaAnalyzer\MultiDijkstraAmbiguityException
5252
*/
53-
public function testDijkstraAmbiguity() {
53+
public function testDijkstraAmbiguity()
54+
{
5455
$graph = new Graph();
5556

56-
$a = $graph->createVertex("a");
57-
$b = $graph->createVertex("b");
58-
$c = $graph->createVertex("c");
59-
$d = $graph->createVertex("d");
57+
$a = $graph->createVertex('a');
58+
$b = $graph->createVertex('b');
59+
$c = $graph->createVertex('c');
60+
$d = $graph->createVertex('d');
6061

6162
$a->createEdge($b)->setWeight(12);
6263
$a->createEdge($c)->setWeight(42);
@@ -71,13 +72,14 @@ public function testDijkstraAmbiguity() {
7172
/**
7273
* @expectedException \Mouf\Database\SchemaAnalyzer\MultiDijkstraNoPathException
7374
*/
74-
public function testDijkstraNoPath() {
75+
public function testDijkstraNoPath()
76+
{
7577
$graph = new Graph();
7678

77-
$a = $graph->createVertex("a");
78-
$b = $graph->createVertex("b");
79-
$c = $graph->createVertex("c");
80-
$d = $graph->createVertex("d");
79+
$a = $graph->createVertex('a');
80+
$b = $graph->createVertex('b');
81+
$c = $graph->createVertex('c');
82+
$d = $graph->createVertex('d');
8183

8284
$a->createEdge($b)->setWeight(12);
8385
$a->createEdge($c)->setWeight(42);
@@ -88,30 +90,32 @@ public function testDijkstraNoPath() {
8890
/**
8991
* @expectedException \Fhaculty\Graph\Exception\UnexpectedValueException
9092
*/
91-
public function testDijkstraNegativeWeight() {
93+
public function testDijkstraNegativeWeight()
94+
{
9295
$graph = new Graph();
9396

94-
$a = $graph->createVertex("a");
95-
$b = $graph->createVertex("b");
97+
$a = $graph->createVertex('a');
98+
$b = $graph->createVertex('b');
9699

97100
$a->createEdge($b)->setWeight(-12);
98101

99102
MultiDijkstra::findShortestPaths($a, $b);
100103
}
101104

102-
public function testOptimizedExit() {
105+
public function testOptimizedExit()
106+
{
103107
$graph = new Graph();
104108

105-
$a = $graph->createVertex("a");
106-
$b = $graph->createVertex("b");
107-
$c = $graph->createVertex("c");
108-
$d = $graph->createVertex("d");
109-
$e = $graph->createVertex("e");
110-
$f = $graph->createVertex("f");
111-
$g = $graph->createVertex("g");
112-
$h = $graph->createVertex("h");
113-
$i = $graph->createVertex("i");
114-
$j = $graph->createVertex("j");
109+
$a = $graph->createVertex('a');
110+
$b = $graph->createVertex('b');
111+
$c = $graph->createVertex('c');
112+
$d = $graph->createVertex('d');
113+
$e = $graph->createVertex('e');
114+
$f = $graph->createVertex('f');
115+
$g = $graph->createVertex('g');
116+
$h = $graph->createVertex('h');
117+
$i = $graph->createVertex('i');
118+
$j = $graph->createVertex('j');
115119

116120
$a->createEdge($b)->setWeight(1);
117121
$a->createEdge($c)->setWeight(217);
@@ -134,17 +138,19 @@ public function testOptimizedExit() {
134138
$this->assertTrue($this->hasVertex($edges[0], $b));
135139
}
136140

137-
private function hasVertex(Edge\Base $edge, Vertex $vertex) {
141+
private function hasVertex(Edge\Base $edge, Vertex $vertex)
142+
{
138143
return $edge->getVerticesStart()->getVertexFirst() === $vertex || $edge->getVerticesTarget()->getVertexFirst() === $vertex;
139144
}
140145

141-
public function testDijkstraAmbiguity2() {
146+
public function testDijkstraAmbiguity2()
147+
{
142148
$graph = new Graph();
143149

144-
$a = $graph->createVertex("a");
145-
$b = $graph->createVertex("b");
146-
$c = $graph->createVertex("c");
147-
$d = $graph->createVertex("d");
150+
$a = $graph->createVertex('a');
151+
$b = $graph->createVertex('b');
152+
$c = $graph->createVertex('c');
153+
$d = $graph->createVertex('d');
148154

149155
$a->createEdge($b)->setWeight(12);
150156
$a->createEdge($c)->setWeight(42);
@@ -170,14 +176,15 @@ public function testDijkstraAmbiguity2() {
170176
$this->assertTrue($this->hasVertex($paths[1][1], $d));
171177
}
172178

173-
public function testDijkstraAmbiguity3() {
179+
public function testDijkstraAmbiguity3()
180+
{
174181
$graph = new Graph();
175182

176-
$a = $graph->createVertex("a");
177-
$b = $graph->createVertex("b");
178-
$c = $graph->createVertex("c");
179-
$d = $graph->createVertex("d");
180-
$e = $graph->createVertex("e");
183+
$a = $graph->createVertex('a');
184+
$b = $graph->createVertex('b');
185+
$c = $graph->createVertex('c');
186+
$d = $graph->createVertex('d');
187+
$e = $graph->createVertex('e');
181188

182189
$a->createEdge($b)->setWeight(12);
183190
$a->createEdge($c)->setWeight(42);
@@ -192,5 +199,4 @@ public function testDijkstraAmbiguity3() {
192199

193200
$this->assertCount(4, $paths);
194201
}
195-
196202
}

0 commit comments

Comments
 (0)