Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Code/AbstractMemberGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-code/blob/master/LICENSE.md New BSD License
*/
abstract class AbstractMemberGenerator implements StatementGenerator
abstract class AbstractMemberGenerator implements \OpenCodeModeling\CodeAst\StatementGenerator
{
/**#@+
* @const int Flags for construction usage
Expand Down
2 changes: 1 addition & 1 deletion src/Code/BodyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use PhpParser\Parser;

final class BodyGenerator implements StatementGenerator
final class BodyGenerator implements \OpenCodeModeling\CodeAst\StatementGenerator
{
/**
* @var Parser
Expand Down
2 changes: 1 addition & 1 deletion src/Code/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-code/blob/master/LICENSE.md New BSD License
*/
final class ClassGenerator implements StatementGenerator
final class ClassGenerator implements \OpenCodeModeling\CodeAst\StatementGenerator
{
public const FLAG_ABSTRACT = Class_::MODIFIER_ABSTRACT;
public const FLAG_FINAL = Class_::MODIFIER_FINAL;
Expand Down
12 changes: 9 additions & 3 deletions src/Code/IdentifierGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace OpenCodeModeling\CodeAst\Code;

use OpenCodeModeling\CodeAst\IdentifiedStatementGenerator;
use PhpParser\Node\Stmt;

final class IdentifierGenerator implements StatementGenerator
final class IdentifierGenerator implements IdentifiedStatementGenerator
{
/**
* @var StatementGenerator
* @var \OpenCodeModeling\CodeAst\StatementGenerator
*/
private $statementGenerator;

Expand All @@ -24,7 +25,7 @@ final class IdentifierGenerator implements StatementGenerator
*/
private $identifier;

public function __construct(string $identifier, StatementGenerator $statementGenerator)
public function __construct(string $identifier, \OpenCodeModeling\CodeAst\StatementGenerator $statementGenerator)
{
$this->identifier = $identifier;
$this->statementGenerator = $statementGenerator;
Expand All @@ -35,6 +36,11 @@ public function getIdentifier(): string
return $this->identifier;
}

public function identifier(): string
{
return $this->identifier;
}

public function generate(): array
{
$stmt = $this->statementGenerator->generate();
Expand Down
3 changes: 3 additions & 0 deletions src/Code/StatementGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

use PhpParser\Node;

/**
* @deprecated Use \OpenCodeModeling\CodeAst\StatementGenerator
*/
interface StatementGenerator
{
/**
Expand Down
16 changes: 16 additions & 0 deletions src/IdentifiedStatementGenerator.php
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;
}
50 changes: 50 additions & 0 deletions src/Node/StatementGenerator.php
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;
}
}
18 changes: 15 additions & 3 deletions src/NodeVisitor/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
use OpenCodeModeling\CodeAst\Code\IdentifierGenerator;
use OpenCodeModeling\CodeAst\IdentifiedStatementGenerator;
use OpenCodeModeling\CodeAst\Node\StatementGenerator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Namespace_;
Expand All @@ -20,15 +22,25 @@
final class ClassConstant extends NodeVisitorAbstract
{
/**
* @var IdentifierGenerator
* @var IdentifiedStatementGenerator
*/
private $lineGenerator;

public function __construct(IdentifierGenerator $lineGenerator)
public function __construct(IdentifiedStatementGenerator $lineGenerator)
{
$this->lineGenerator = $lineGenerator;
}

public static function fromNode(Node\Stmt\ClassConst $node): self
{
return new self(
new StatementGenerator(
$node->consts[0]->name->name,
$node
)
);
}

public static function forClassConstant(
string $constantName,
$constantValue,
Expand Down Expand Up @@ -79,7 +91,7 @@ private function checkConstantExists(Class_ $node): bool
{
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Node\Stmt\ClassConst
&& $stmt->consts[0]->name->name === $this->lineGenerator->getIdentifier()
&& $stmt->consts[0]->name->name === $this->lineGenerator->identifier()
) {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/NodeVisitor/ClassFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function __construct(ClassGenerator $classGenerator)
$this->classGenerator = $classGenerator;
}

public static function fromNode(Class_ $node): self
{
return new self(new ClassGenerator($node->name->name));
}

public function beforeTraverse(array $nodes)
{
foreach ($nodes as $node) {
Expand Down
5 changes: 5 additions & 0 deletions src/NodeVisitor/ClassNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function __construct(string $namespace)
$this->builderFactory = new BuilderFactory();
}

public static function fromNode(Stmt\Namespace_ $node): self
{
return new self($node->name->toString());
}

public function afterTraverse(array $nodes): ?array
{
if ($this->hasNamespace($nodes)) {
Expand Down
68 changes: 68 additions & 0 deletions src/NodeVisitor/Collector.php
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);
}
}
}
21 changes: 21 additions & 0 deletions src/StatementGenerator.php
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();
}
1 change: 0 additions & 1 deletion tests/NodeVisitor/ClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use OpenCodeModeling\CodeAst\NodeVisitor\ClassUseTrait;
use OpenCodeModeling\CodeAst\NodeVisitor\NamespaceUse;
use OpenCodeModeling\CodeAst\NodeVisitor\StrictType;
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\BooleanFactory;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
Expand Down
71 changes: 71 additions & 0 deletions tests/NodeVisitorTest.php
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(''));
Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

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)));
}
}