Skip to content

Commit

Permalink
Update the nikic/php-parser importer.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 4, 2020
1 parent 8352667 commit 968e58e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace spec\loophp\phptree\Importer;

use Error;
use loophp\phptree\Importer\NikicPhpParserImporter;
use loophp\phptree\Importer\NikicPhpParser;
use loophp\phptree\Node\AttributeNodeInterface;
use PhpParser\ParserFactory;
use PhpSpec\ObjectBehavior;

class NikicPhpParserImporterSpec extends ObjectBehavior
class NikicPhpParserSpec extends ObjectBehavior
{
public function it_can_import(): void
{
Expand Down Expand Up @@ -53,6 +53,6 @@ public function it_can_import(): void

public function it_is_initializable(): void
{
$this->shouldHaveType(NikicPhpParserImporter::class);
$this->shouldHaveType(NikicPhpParser::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use PhpParser\Node;
use SplObjectStorage;

use function get_class;
use function is_array;

final class NikicPhpParserImporter implements ImporterInterface
/**
* Class NikicPhpParser.
*/
final class NikicPhpParser implements ImporterInterface
{
/**
* @var SplObjectStorage
Expand All @@ -36,7 +38,7 @@ public function __construct()
public function import($data): NodeInterface
{
return (new AttributeNode(['label' => 'root']))
->add(...$this->parseNodes($data));
->add(...$this->parseNodes(...$data));
}

/**
Expand All @@ -49,15 +51,12 @@ public function import($data): NodeInterface
private function createNewNode(Node $astNode): NodeInterface
{
$defaultAttributes = [
'label' => sprintf('%s', $astNode->getType()),
'type' => $astNode->getType(),
'class' => get_class($astNode),
'shape' => 'rect',
'label' => $astNode->getType(),
'astNode' => $astNode,
];

return (new AttributeNode($defaultAttributes))
->add(...$this->parseNodes($this->getAllNodeChildren($astNode)));
->add(...$this->parseNodes(...$this->getAllNodeChildren($astNode)));
}

/**
Expand Down Expand Up @@ -103,13 +102,13 @@ private function getNodeFromCache(Node $astNode, callable $default): AttributeNo
}

/**
* @param Node[] $astNodes
* @param Node ...$astNodes
*
* @throws \Exception
*
* @return AttributeNodeInterface[]
*/
private function parseNodes(array $astNodes): array
private function parseNodes(Node ...$astNodes): array
{
$treeNodes = [];

Expand Down

0 comments on commit 968e58e

Please sign in to comment.