-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add node collector to reverse engineer code generation via AST by a PHP template #5
Open
sandrokeil
wants to merge
1
commit into
master
Choose a base branch
from
feature/node-collector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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,16 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst; | ||
|
||
interface IdentifiedStatementGenerator extends StatementGenerator | ||
{ | ||
public function identifier(): string; | ||
} |
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,50 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst\Node; | ||
|
||
use OpenCodeModeling\CodeAst\IdentifiedStatementGenerator; | ||
use PhpParser\Node; | ||
|
||
final class StatementGenerator implements IdentifiedStatementGenerator | ||
{ | ||
/** | ||
* @var string | ||
**/ | ||
private $identifier; | ||
|
||
/** | ||
* @var array<Node\Stmt> | ||
**/ | ||
private $stmts; | ||
|
||
/** | ||
* @param string $identifier | ||
* @param Node\Stmt ...$stmts | ||
*/ | ||
public function __construct(string $identifier, Node\Stmt ...$stmts) | ||
{ | ||
$this->identifier = $identifier; | ||
$this->stmts = $stmts; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function identifier(): string | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
public function generate() | ||
{ | ||
return $this->stmts; | ||
} | ||
} |
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
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
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
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,68 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
final class Collector extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $visitors = []; | ||
|
||
public function afterTraverse(array $nodes): ?array | ||
{ | ||
$this->visitors = []; | ||
|
||
foreach ($nodes as $node) { | ||
$this->determineVisitor($node); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function determineVisitor(Node $node): void | ||
{ | ||
switch (true) { | ||
case $node instanceof Namespace_: | ||
$this->visitors[] = ClassNamespace::fromNode($node); | ||
break; | ||
case $node instanceof Node\Stmt\Class_: | ||
$this->visitors[] = ClassFile::fromNode($node); | ||
|
||
foreach ($node->stmts as $stmt) { | ||
$this->determineVisitor($stmt); | ||
} | ||
break; | ||
case $node instanceof Node\Stmt\ClassConst: | ||
$this->visitors[] = ClassConstant::fromNode($node); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
public function visitors(): array | ||
{ | ||
return $this->visitors; | ||
} | ||
|
||
public function injectVisitors(NodeTraverser $nodeTraverser): void | ||
{ | ||
foreach ($this->visitors as $visitor) { | ||
$nodeTraverser->addVisitor(clone $visitor); | ||
} | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst; | ||
|
||
use PhpParser\Node; | ||
|
||
interface StatementGenerator | ||
{ | ||
/** | ||
* @return Node\Stmt|Node\Stmt[] | ||
*/ | ||
public function generate(); | ||
} |
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
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModelingTest\CodeAst; | ||
|
||
use OpenCodeModeling\CodeAst\NodeVisitor\Collector; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\Parser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NodeVisitorTest extends TestCase | ||
{ | ||
/** | ||
* @var Parser | ||
*/ | ||
private $parser; | ||
|
||
/** | ||
* @var Standard | ||
*/ | ||
private $printer; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); | ||
$this->printer = new Standard(['shortArraySyntax' => true]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_detects_visitors_for_class_and_constant(): void | ||
{ | ||
$expectedCode = <<<'EOF' | ||
<?php | ||
|
||
class TestClass | ||
{ | ||
public const TYPE_STRING = 'string'; | ||
} | ||
EOF; | ||
|
||
$ast = $this->parser->parse($expectedCode); | ||
|
||
$visitorCollector = new Collector(); | ||
|
||
$nodeTraverser = new NodeTraverser(); | ||
$nodeTraverser->addVisitor($visitorCollector); | ||
|
||
$nodeTraverser->traverse($ast); | ||
|
||
$detectedVisitors = $visitorCollector->visitors(); | ||
|
||
$this->assertCount(2, $detectedVisitors); | ||
|
||
$this->assertCode($expectedCode, $visitorCollector, $ast); | ||
$this->assertCode($expectedCode, $visitorCollector, $this->parser->parse('')); | ||
} | ||
|
||
private function assertCode(string $expectedCode, Collector $visitorCollector, array $ast): void | ||
{ | ||
$nodeTraverser = new NodeTraverser(); | ||
|
||
$visitorCollector->injectVisitors($nodeTraverser); | ||
|
||
$this->assertSame($expectedCode, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codeliner Look at this test for the rough idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍