Skip to content

Commit e45ef36

Browse files
authored
Merge pull request #3 from open-code-modeling/fix/order_of_constants
Fix order of class constants does not match w/visitor order
2 parents 10dd6b3 + 2370c35 commit e45ef36

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/NodeVisitor/ClassConstant.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(IdentifierGenerator $lineGenerator)
3131

3232
public static function forClassConstant(
3333
string $constantName,
34-
string $constantValue,
34+
$constantValue,
3535
int $flags = ClassConstGenerator::FLAG_PUBLIC
3636
): ClassConstant {
3737
return new self(
@@ -56,8 +56,8 @@ public function afterTraverse(array $nodes): ?array
5656
return null;
5757
}
5858
$stmt->stmts = \array_merge(
59-
$this->lineGenerator->generate(),
60-
$stmt->stmts
59+
$stmt->stmts,
60+
$this->lineGenerator->generate()
6161
);
6262
}
6363
}
@@ -66,8 +66,8 @@ public function afterTraverse(array $nodes): ?array
6666
return null;
6767
}
6868
$node->stmts = \array_merge(
69-
$this->lineGenerator->generate(),
70-
$node->stmts
69+
$node->stmts,
70+
$this->lineGenerator->generate()
7171
);
7272
}
7373
}

tests/NodeVisitor/ClassConstantTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant;
1010
use OpenCodeModeling\CodeAst\NodeVisitor\ClassFile;
1111
use OpenCodeModeling\CodeAst\NodeVisitor\ClassNamespace;
12+
use OpenCodeModeling\CodeAst\NodeVisitor\ClassUseTrait;
13+
use OpenCodeModeling\CodeAst\NodeVisitor\NamespaceUse;
1214
use OpenCodeModeling\CodeAst\NodeVisitor\StrictType;
1315
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\BooleanFactory;
1416
use PhpParser\NodeTraverser;
@@ -137,6 +139,40 @@ class TestClass
137139
{
138140
private const TYPE_STRING = 'string';
139141
}
142+
EOF;
143+
144+
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
145+
}
146+
147+
/**
148+
* @test
149+
*/
150+
public function it_preserves_order_of_registered_visitors()
151+
{
152+
$ast = $this->parser->parse('');
153+
154+
$nodeTraverser = new NodeTraverser();
155+
$nodeTraverser->addVisitor(new StrictType());
156+
$nodeTraverser->addVisitor(new ClassNamespace('My\\Awesome\\Service'));
157+
$nodeTraverser->addVisitor(new NamespaceUse('My\\Awesome\\ServiceTrait'));
158+
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
159+
$nodeTraverser->addVisitor(new ClassUseTrait('ServiceTrait'));
160+
$nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string'));
161+
$nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_INT', 3));
162+
163+
$expected = <<<'EOF'
164+
<?php
165+
166+
declare (strict_types=1);
167+
namespace My\Awesome\Service;
168+
169+
use My\Awesome\ServiceTrait;
170+
class TestClass
171+
{
172+
use ServiceTrait;
173+
public const TYPE_STRING = 'string';
174+
public const TYPE_INT = 3;
175+
}
140176
EOF;
141177

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

0 commit comments

Comments
 (0)