Skip to content

Commit 37b4c70

Browse files
committed
Add new microsoft/tolerant-php-parser importer.
1 parent 968e58e commit 37b4c70

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"friends-of-phpspec/phpspec-code-coverage": "^4.3.2",
2626
"graphp/graphviz": "^0.2",
2727
"infection/infection": "^0.13.6 || ^0.15.0",
28+
"microsoft/tolerant-php-parser": "^0.0.20",
2829
"nikic/php-parser": "^4.3",
2930
"phpbench/phpbench": "^0.16.10",
3031
"phpspec/phpspec": "^5.1.2 || ^6.1.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\loophp\phptree\Importer;
6+
7+
use loophp\phptree\Importer\MicrosoftTolerantPhpParser;
8+
use loophp\phptree\Node\AttributeNodeInterface;
9+
use Microsoft\PhpParser\Parser;
10+
use PhpSpec\ObjectBehavior;
11+
12+
class MicrosoftTolerantPhpParserSpec extends ObjectBehavior
13+
{
14+
public function it_can_import(): void
15+
{
16+
$file = __DIR__ . '/../../../../src/Node/Node.php';
17+
18+
$parser = new Parser();
19+
$ast = $parser->parseSourceFile(file_get_contents($file));
20+
21+
$this
22+
->import($ast)
23+
->shouldImplement(AttributeNodeInterface::class);
24+
25+
$this
26+
->import($ast)
27+
->count()
28+
->shouldReturn(582);
29+
30+
$file = __DIR__ . '/../../../../tests/sample.php';
31+
32+
$ast = $parser->parseSourceFile(file_get_contents($file));
33+
34+
$this
35+
->import($ast)
36+
->count()
37+
->shouldReturn(112);
38+
}
39+
40+
public function it_is_initializable(): void
41+
{
42+
$this->shouldHaveType(MicrosoftTolerantPhpParser::class);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace loophp\phptree\Importer;
6+
7+
use Exception;
8+
use loophp\phptree\Node\AttributeNode;
9+
use loophp\phptree\Node\AttributeNodeInterface;
10+
use loophp\phptree\Node\NodeInterface;
11+
use Microsoft\PhpParser\Node;
12+
use Microsoft\PhpParser\Node\SourceFileNode;
13+
14+
/**
15+
* Class MicrosoftTolerantPhpParser.
16+
*/
17+
final class MicrosoftTolerantPhpParser implements ImporterInterface
18+
{
19+
/**
20+
* @param SourceFileNode $data
21+
*
22+
* @throws Exception
23+
*
24+
* @return \loophp\phptree\Node\NodeInterface
25+
*/
26+
public function import($data): NodeInterface
27+
{
28+
return $this->parseNode($this->createNode(['label' => 'root']), $data);
29+
}
30+
31+
/**
32+
* @param array $attributes
33+
*
34+
* @return \loophp\phptree\Node\AttributeNodeInterface
35+
*/
36+
private function createNode(array $attributes): AttributeNodeInterface
37+
{
38+
return new AttributeNode($attributes);
39+
}
40+
41+
/**
42+
* @param \loophp\phptree\Node\AttributeNodeInterface $parent
43+
* @param \Microsoft\PhpParser\Node ...$astNodes
44+
*
45+
* @return \loophp\phptree\Node\NodeInterface
46+
*/
47+
private function parseNode(AttributeNodeInterface $parent, Node ...$astNodes): NodeInterface
48+
{
49+
return array_reduce(
50+
$astNodes,
51+
function (AttributeNodeInterface $carry, Node $astNode): NodeInterface {
52+
return $carry
53+
->add(
54+
$this->parseNode(
55+
$this->createNode([
56+
'label' => $astNode->getNodeKindName(),
57+
'astNode' => $astNode,
58+
]),
59+
...$astNode->getChildNodes()
60+
)
61+
);
62+
},
63+
$parent
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)