-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new microsoft/tolerant-php-parser importer.
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
spec/loophp/phptree/Importer/MicrosoftTolerantPhpParserSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\loophp\phptree\Importer; | ||
|
||
use loophp\phptree\Importer\MicrosoftTolerantPhpParser; | ||
use loophp\phptree\Node\AttributeNodeInterface; | ||
use Microsoft\PhpParser\Parser; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class MicrosoftTolerantPhpParserSpec extends ObjectBehavior | ||
{ | ||
public function it_can_import(): void | ||
{ | ||
$file = __DIR__ . '/../../../../src/Node/Node.php'; | ||
|
||
$parser = new Parser(); | ||
$ast = $parser->parseSourceFile(file_get_contents($file)); | ||
|
||
$this | ||
->import($ast) | ||
->shouldImplement(AttributeNodeInterface::class); | ||
|
||
$this | ||
->import($ast) | ||
->count() | ||
->shouldReturn(582); | ||
|
||
$file = __DIR__ . '/../../../../tests/sample.php'; | ||
|
||
$ast = $parser->parseSourceFile(file_get_contents($file)); | ||
|
||
$this | ||
->import($ast) | ||
->count() | ||
->shouldReturn(112); | ||
} | ||
|
||
public function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(MicrosoftTolerantPhpParser::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\phptree\Importer; | ||
|
||
use Exception; | ||
use loophp\phptree\Node\AttributeNode; | ||
use loophp\phptree\Node\AttributeNodeInterface; | ||
use loophp\phptree\Node\NodeInterface; | ||
use Microsoft\PhpParser\Node; | ||
use Microsoft\PhpParser\Node\SourceFileNode; | ||
|
||
/** | ||
* Class MicrosoftTolerantPhpParser. | ||
*/ | ||
final class MicrosoftTolerantPhpParser implements ImporterInterface | ||
{ | ||
/** | ||
* @param SourceFileNode $data | ||
* | ||
* @throws Exception | ||
* | ||
* @return \loophp\phptree\Node\NodeInterface | ||
*/ | ||
public function import($data): NodeInterface | ||
{ | ||
return $this->parseNode($data, (new AttributeNode(['label' => 'root']))); | ||
} | ||
|
||
/** | ||
* @param \Microsoft\PhpParser\Node $astNode | ||
* @param \loophp\phptree\Node\AttributeNodeInterface $parent | ||
* | ||
* @return \loophp\phptree\Node\AttributeNodeInterface | ||
*/ | ||
private function parseNode(Node $astNode, AttributeNodeInterface $parent): AttributeNodeInterface | ||
{ | ||
$node = new AttributeNode([ | ||
'label' => $astNode->getNodeKindName(), | ||
'astNode' => $astNode, | ||
]); | ||
|
||
$parent->add($node); | ||
|
||
foreach ($astNode->getChildNodes() as $child) { | ||
$this->parseNode($child, $node); | ||
} | ||
|
||
return $parent; | ||
} | ||
} |