Skip to content
Merged
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
10 changes: 5 additions & 5 deletions src/NodeVisitor/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(IdentifierGenerator $lineGenerator)

public static function forClassConstant(
string $constantName,
string $constantValue,
$constantValue,
int $flags = ClassConstGenerator::FLAG_PUBLIC
): ClassConstant {
return new self(
Expand All @@ -56,8 +56,8 @@ public function afterTraverse(array $nodes): ?array
return null;
}
$stmt->stmts = \array_merge(
$this->lineGenerator->generate(),
$stmt->stmts
$stmt->stmts,
$this->lineGenerator->generate()
);
}
}
Expand All @@ -66,8 +66,8 @@ public function afterTraverse(array $nodes): ?array
return null;
}
$node->stmts = \array_merge(
$this->lineGenerator->generate(),
$node->stmts
$node->stmts,
$this->lineGenerator->generate()
);
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/NodeVisitor/ClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassFile;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassNamespace;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassUseTrait;
use OpenCodeModeling\CodeAst\NodeVisitor\NamespaceUse;
use OpenCodeModeling\CodeAst\NodeVisitor\StrictType;
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\BooleanFactory;
use PhpParser\NodeTraverser;
Expand Down Expand Up @@ -137,6 +139,40 @@ class TestClass
{
private const TYPE_STRING = 'string';
}
EOF;

$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
}

/**
* @test
*/
public function it_preserves_order_of_registered_visitors()
{
$ast = $this->parser->parse('');

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new StrictType());
$nodeTraverser->addVisitor(new ClassNamespace('My\\Awesome\\Service'));
$nodeTraverser->addVisitor(new NamespaceUse('My\\Awesome\\ServiceTrait'));
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassUseTrait('ServiceTrait'));
$nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string'));
$nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_INT', 3));

$expected = <<<'EOF'
<?php

declare (strict_types=1);
namespace My\Awesome\Service;

use My\Awesome\ServiceTrait;
class TestClass
{
use ServiceTrait;
public const TYPE_STRING = 'string';
public const TYPE_INT = 3;
}
EOF;

$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
Expand Down