Skip to content

Commit f75af46

Browse files
committed
Update documentation and code style.
1 parent 783703c commit f75af46

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ It provides different trees implementations:
2929

3030
Exporters and importers:
3131
* **Ascii**: Export a tree into an ascii graphic, just for swag and visualisation fun.
32-
* **Graph**: Export a tree into a graph using the [graphp/graphp](https://github.com/graphp/graph) library.
32+
* **Graph**: Export a tree into a Graph using the [graphp/graphp](https://github.com/graphp/graph) library.
33+
* **GraphViz**: Export a tree into a script in [GraphViz](http://www.graphviz.org/) format.
3334
* **Text**: Export a tree into a simple string, easy for storing in a database.
3435

3536
Modifier:
@@ -51,8 +52,7 @@ Blog post: [https://not-a-number.io/2018/phptree-a-fast-tree-implementation](htt
5152

5253
## Optional packages
5354

54-
* [graphp/graphp](https://github.com/graphp/graph): To convert a tree into a graph.
55-
* [graphp/graphviz](https://github.com/graphp/graphviz): To render a graph into dot format or an image.
55+
* [graphp/graphp](https://github.com/graphp/graph): To export a tree into a Graph.
5656

5757
## Usage
5858

@@ -61,8 +61,7 @@ Blog post: [https://not-a-number.io/2018/phptree-a-fast-tree-implementation](htt
6161

6262
declare(strict_types = 1);
6363

64-
use Graphp\GraphViz\GraphViz;
65-
use drupol\phptree\Exporter\Graph;
64+
use drupol\phptree\Exporter\Gv;
6665
use drupol\phptree\Node\ValueNode;
6766
use drupol\phptree\Exporter\Text;
6867

@@ -79,14 +78,15 @@ foreach (\range('A', 'Z') as $v) {
7978
// Add children to the root node.
8079
$tree->add(...$nodes);
8180

82-
// Export to an image.
83-
$graphViz = new GraphViz();
84-
$graphExporter = new Graph();
85-
$graphViz->display($graphExporter->export($tree));
86-
8781
// Export to text.
8882
$textExporter = new Text();
8983
echo $textExporter->export($tree); // [root[A[C[G[O][P]][H[Q][R]]][D[I[S][T]][J[U][V]]]][B[E[K[W][X]][L[Y][Z]]][F[M][N]]]]⏎
84+
85+
// Export to a GraphViz script.
86+
$exporter = new Gv();
87+
$dotScript = $exporter->export($tree);
88+
file_put_contents('graph.gv', $dotScript);
89+
// Then do "dot -Tsvg graph.gv -o graph.svg" to export the script to SVG.
9090
```
9191

9292
## Code quality, tests and benchmarks

src/Exporter/Gv.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class Gv implements ExporterInterface
1616
/**
1717
* The graph attributes.
1818
*
19-
* @var string[]
19+
* @var string[]|string[][]
2020
*/
2121
private $attributes = [];
22+
2223
/**
23-
* Is the graph directed or undirected.
24+
* The graph type.
2425
*
2526
* @var bool
2627
*/
@@ -31,10 +32,6 @@ class Gv implements ExporterInterface
3132
*/
3233
public function export(NodeInterface $node): string
3334
{
34-
$directed = true === $this->getDirected() ?
35-
'->' :
36-
'--';
37-
3835
$attributes = '';
3936
foreach ($this->attributes as $key => $attribute) {
4037
if (\is_string($attribute)) {
@@ -72,7 +69,7 @@ public function export(NodeInterface $node): string
7269
$edges .= \sprintf(
7370
' "%s" %s "%s";' . "\n",
7471
$this->getHash($parent),
75-
$directed,
72+
true === $this->getDirected() ? '->' : '--',
7673
$this->getHash($child)
7774
);
7875
}
@@ -92,7 +89,7 @@ public function getDirected(): bool
9289
}
9390

9491
/**
95-
* Set the graph as directed.
92+
* Set the graph type, directed or undirected.
9693
*
9794
* @param bool $directed
9895
* True for a directed graph, false otherwise.

src/Node/AttributeNodeInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface AttributeNodeInterface extends NaryNodeInterface
1313
* {@inheritdoc}
1414
*/
1515
public function getAttribute($key);
16+
1617
/**
1718
* {@inheritdoc}
1819
*/

src/Node/Node.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __clone()
3737
{
3838
$this->storage = clone $this->storage;
3939

40+
/** @var \drupol\phptree\Node\NodeInterface $child */
4041
foreach ($this->children() as $child) {
4142
$child->setParent($this);
4243
}
@@ -63,15 +64,14 @@ public function all(): \Traversable
6364
{
6465
yield $this;
6566

66-
foreach ($this->children() as $candidate) {
67-
yield from $candidate->all();
67+
/** @var \drupol\phptree\Node\NodeInterface $child */
68+
foreach ($this->children() as $child) {
69+
yield from $child->all();
6870
}
6971
}
7072

7173
/**
7274
* {@inheritdoc}
73-
*
74-
* @return \drupol\phptree\Node\NodeInterface|\Traversable
7575
*/
7676
public function children(): \Traversable
7777
{
@@ -137,6 +137,7 @@ public function depth(): int
137137
*/
138138
public function find(NodeInterface $node): ?NodeInterface
139139
{
140+
/** @var \drupol\phptree\Node\NodeInterface $candidate */
140141
foreach ($this->all() as $candidate) {
141142
if ($candidate === $node) {
142143
return $node;
@@ -201,6 +202,7 @@ public function height(): int
201202
{
202203
$height = $this->depth();
203204

205+
/** @var \drupol\phptree\Node\NodeInterface $child */
204206
foreach ($this->children() as $child) {
205207
$height = \max($height, $child->height());
206208
}

src/Node/NodeInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public function add(NodeInterface ...$node): NodeInterface;
2323
/**
2424
* Get all the nodes of a tree including the parent node itself.
2525
*
26-
* @return \drupol\phptree\Node\NodeInterface|\Traversable
26+
* @return \Traversable
2727
* The node.
2828
*/
2929
public function all(): \Traversable;
3030

3131
/**
3232
* Get the children.
3333
*
34-
* @return \drupol\phptree\Node\NodeInterface|\Traversable
34+
* @return \Traversable
3535
* The children
3636
*/
3737
public function children(): \Traversable;

src/Storage/StorageInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface StorageInterface
1515
* @param string $key
1616
* The identifier name of the value.
1717
*
18-
* @return null|int|mixed|string
18+
* @return mixed
1919
* The value.
2020
*/
2121
public function get($key);

0 commit comments

Comments
 (0)