Skip to content

Commit

Permalink
Optimize Exporters.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 10, 2020
1 parent e43a16c commit d03f537
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 52 deletions.
17 changes: 0 additions & 17 deletions spec/loophp/phptree/Exporter/SimpleArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace spec\loophp\phptree\Exporter;

use InvalidArgumentException;
use loophp\phptree\Exporter\SimpleArray;
use loophp\phptree\Node\Node;
use loophp\phptree\Node\ValueNode;
use PhpSpec\ObjectBehavior;

Expand Down Expand Up @@ -77,21 +75,6 @@ public function it_can_export_to_an_array(): void
->shouldReturn($return);
}

public function it_can_throw_an_error_when_tree_is_not_a_valuenode(): void
{
$tree = new Node();

foreach (range('A', 'Z') as $key => $value) {
$nodes[$value] = new Node();
}

$tree->add(...array_values($nodes));

$this
->shouldThrow(InvalidArgumentException::class)
->during('export', [$tree]);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(SimpleArray::class);
Expand Down
27 changes: 3 additions & 24 deletions src/Exporter/Ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CachingIterator;
use loophp\phptree\Node\NodeInterface;
use loophp\phptree\Node\ValueNodeInterface;
use RecursiveArrayIterator;
use RecursiveTreeIterator;

Expand All @@ -22,9 +21,11 @@ final class Ascii implements ExporterInterface
*/
public function export(NodeInterface $node): string
{
$exporter = new SimpleArray();

$tree = new RecursiveTreeIterator(
new RecursiveArrayIterator(
$this->doExportAsArray($node)
$exporter->export($node)
),
RecursiveTreeIterator::SELF_FIRST,
CachingIterator::CATCH_GET_CHILD,
Expand All @@ -50,26 +51,4 @@ public function export(NodeInterface $node): string

return $output;
}

/**
* Export the tree in an array.
*
* @param \loophp\phptree\Node\NodeInterface $node
* The node
*
* @return array<int, mixed>
* The tree exported into an array
*/
private function doExportAsArray(NodeInterface $node): array
{
$children = [];
/** @var ValueNodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->doExportAsArray($child);
}

return [] === $children ?
[$node->label()] :
[$node->label(), $children];
}
}
2 changes: 1 addition & 1 deletion src/Exporter/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function createVertex(NodeInterface $node): Vertex
*/
private function createVertexId(NodeInterface $node): string
{
return spl_object_hash($node);
return sha1(spl_object_hash($node));
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Exporter/SimpleArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace loophp\phptree\Exporter;

use InvalidArgumentException;
use loophp\phptree\Node\NodeInterface;
use loophp\phptree\Node\ValueNodeInterface;

/**
* Class SimpleArray.
Expand All @@ -18,12 +16,9 @@ final class SimpleArray implements ExporterInterface
*/
public function export(NodeInterface $node)
{
if (!($node instanceof ValueNodeInterface)) {
throw new InvalidArgumentException('Must implements ValueNodeInterface');
}

$children = [];
/** @var ValueNodeInterface $child */

/** @var NodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->export($child);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Exporter/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\phptree\Exporter;

use loophp\phptree\Node\NodeInterface;
use loophp\phptree\Node\ValueNodeInterface;

/**
* Class Text.
Expand All @@ -19,7 +18,7 @@ public function export(NodeInterface $node): string
{
$children = [];

/** @var ValueNodeInterface $child */
/** @var NodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->export($child);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Importer/NikicPhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ static function (string $subNodeName) use ($astNode): array {
$astNode->getSubNodeNames()
);

return array_merge(...$astNodes);
return [] === $astNodes ?
[] :
array_merge(...$astNodes);
}

/**
Expand Down

0 comments on commit d03f537

Please sign in to comment.