Skip to content

Commit

Permalink
Merge 373dd59 into 1934315
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 12, 2019
2 parents 1934315 + 373dd59 commit b80bc18
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/phpDocumentor/GraphViz/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace phpDocumentor\GraphViz;

use phpDocumentor\GraphViz\Contract\GraphAwareInterface;
use function addslashes;
use function preg_replace;
use function strstr;
Expand All @@ -22,8 +23,10 @@
*
* @link http://phpdoc.org
*/
class Attribute
class Attribute implements GraphAwareInterface
{
use GraphAware;

/** @var string The name of this attribute */
protected $key = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use function array_key_exists;

trait Attributes
trait AttributesAware
{
/** @var Attribute[] */
protected $attributes = [];
Expand Down
34 changes: 34 additions & 0 deletions src/phpDocumentor/GraphViz/Contract/AttributesAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see http://phpdoc.org
*/

namespace phpDocumentor\GraphViz\Contract;

use phpDocumentor\GraphViz\Attribute;

interface AttributesAwareInterface
{
/**
* @param string $name
*
* @return \phpDocumentor\GraphViz\Attribute
*/
public function getAttribute(string $name): Attribute;

/**
* @param string $name
* @param string $value
*
* @return mixed
*/
public function setAttribute(string $name, string $value);
}
31 changes: 31 additions & 0 deletions src/phpDocumentor/GraphViz/Contract/GraphAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see http://phpdoc.org
*/

namespace phpDocumentor\GraphViz\Contract;

use phpDocumentor\GraphViz\Graph;

interface GraphAwareInterface
{
/**
* @return \phpDocumentor\GraphViz\Graph|null
*/
public function getGraphRoot(): ?Graph;

/**
* @param \phpDocumentor\GraphViz\Graph $graph
*
* @return mixed
*/
public function setGraphRoot(Graph $graph);
}
17 changes: 14 additions & 3 deletions src/phpDocumentor/GraphViz/Edge.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace phpDocumentor\GraphViz;

use phpDocumentor\GraphViz\Contract\AttributesAwareInterface;
use phpDocumentor\GraphViz\Contract\GraphAwareInterface;
use function addslashes;
use function implode;
use function strtolower;
Expand All @@ -23,9 +25,10 @@
*
* @link http://phpdoc.org
*/
class Edge
class Edge implements AttributesAwareInterface, GraphAwareInterface
{
use Attributes;
use AttributesAware;
use GraphAware;

/** @var Node Node from where to link */
private $from;
Expand Down Expand Up @@ -123,8 +126,16 @@ public function __toString() : string
$from_name = addslashes($this->getFrom()->getName());
$to_name = addslashes($this->getTo()->getName());

$direction = '->';

if (null !== $graph = $this->getGraphRoot()) {
if ('digraph' !== $graph->getType()) {
$direction = '--';
}
}

return <<<DOT
"${from_name}" -> "${to_name}" [
"${from_name}" ${direction} "${to_name}" [
${attributes}
]
DOT;
Expand Down
9 changes: 7 additions & 2 deletions src/phpDocumentor/GraphViz/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
use phpDocumentor\GraphViz\Contract\AttributesAwareInterface;

/**
* Class representing a graph; this may be a main graph but also a subgraph.
Expand All @@ -44,9 +45,9 @@
* @method Graph setSplines(string $splines)
* @method Graph setConcentrate(string $concentrate)
*/
class Graph
class Graph implements AttributesAwareInterface
{
use Attributes;
use AttributesAware;

/** @var string Name of this graph */
protected $name = 'G';
Expand Down Expand Up @@ -251,6 +252,8 @@ public function getGraph(string $name) : self
*/
public function setNode(Node $node) : self
{
$node->setGraphRoot($this);

$this->nodes[$node->getName()] = $node;
return $this;
}
Expand Down Expand Up @@ -311,6 +314,8 @@ public function __get(string $name) : ?Node
*/
public function link(Edge $edge) : self
{
$edge->setGraphRoot($this);

$this->edges[] = $edge;
return $this;
}
Expand Down
30 changes: 30 additions & 0 deletions src/phpDocumentor/GraphViz/GraphAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see http://phpdoc.org
*/

namespace phpDocumentor\GraphViz;

trait GraphAware
{
/** @var null|\phpDocumentor\GraphViz\Graph */
protected $graph;

public function getGraphRoot(): ?Graph
{
return $this->graph;
}

public function setGraphRoot(Graph $graph): void
{
$this->graph = $graph;
}
}
7 changes: 5 additions & 2 deletions src/phpDocumentor/GraphViz/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace phpDocumentor\GraphViz;

use phpDocumentor\GraphViz\Contract\AttributesAwareInterface;
use phpDocumentor\GraphViz\Contract\GraphAwareInterface;
use function addslashes;
use function implode;
use function strtolower;
Expand All @@ -25,9 +27,10 @@
*
* @method void setLabel(string $name) Sets the label for this node.
*/
class Node
class Node implements AttributesAwareInterface, GraphAwareInterface
{
use Attributes;
use AttributesAware;
use GraphAware;

/** @var string Name for this node */
protected $name = '';
Expand Down
4 changes: 4 additions & 0 deletions tests/phpDocumentor/GraphViz/Test/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function testGetGraph() : void
public function testSetNode() : void
{
$mock = m::mock(Node::class);
$mock->expects('setGraphRoot');
$mock->expects('getName')->andReturn('MyName');

$this->assertSame(
Expand All @@ -288,6 +289,7 @@ public function testFindNode() : void
$this->assertNull($this->fixture->findNode('MyNode'));

$mock = m::mock(Node::class);
$mock->expects('setGraphRoot');
$mock->expects('getName')->andReturn('MyName');

$this->fixture->setNode($mock);
Expand All @@ -298,6 +300,7 @@ public function testFindNode() : void

$subGraph = Graph::create();
$mock2 = m::mock(Node::class);
$mock2->expects('setGraphRoot');
$mock2->expects('getName')->andReturn('MyName2');

$subGraph->setNode($mock2);
Expand Down Expand Up @@ -342,6 +345,7 @@ public function test__get() : void
public function testLink() : void
{
$mock = m::mock(Edge::class);
$mock->expects('setGraphRoot');

$this->assertSame(
$this->fixture,
Expand Down

0 comments on commit b80bc18

Please sign in to comment.