diff --git a/CHANGELOG.md b/CHANGELOG.md index bb8aaf3..85a3af4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. + ## 0.9.0 - TBD ### Added @@ -24,6 +25,25 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 0.8.3 - 2020-11-13 + + +----- + +### Release Notes for [0.8.3](https://github.com/open-code-modeling/php-code-ast/milestone/13) + +0.8.x bugfix release (patch) + +### 0.8.3 + +- Total issues resolved: **1** +- Total pull requests resolved: **0** +- Total contributors: **1** + +#### bug + + - [34: Fix namespace and trait imports](https://github.com/open-code-modeling/php-code-ast/issues/34) thanks to @sandrokeil + ## 0.8.2 - 2020-11-12 diff --git a/src/Builder/ClassBuilder.php b/src/Builder/ClassBuilder.php index 4d0ee30..4eaddf6 100644 --- a/src/Builder/ClassBuilder.php +++ b/src/Builder/ClassBuilder.php @@ -398,7 +398,7 @@ public function generate(Parser $parser): array $visitors[] = new ClassNamespace($this->namespace); } if ($this->namespaceImports) { - $visitors[] = new NamespaceUse(...$this->namespaceImports); + $visitors[] = new NamespaceUse(...\array_reverse($this->namespaceImports)); } $visitors[] = new ClassFile($this->classGenerator()); @@ -410,7 +410,7 @@ public function generate(Parser $parser): array $visitors[] = new ClassImplements(...$this->implements); } if ($this->traits) { - $visitors[] = new ClassUseTrait(...$this->traits); + $visitors[] = new ClassUseTrait(...\array_reverse($this->traits)); } if (\count($this->constants) > 0) { diff --git a/src/Code/MethodGenerator.php b/src/Code/MethodGenerator.php index e7d98ef..4b1fffc 100644 --- a/src/Code/MethodGenerator.php +++ b/src/Code/MethodGenerator.php @@ -257,7 +257,7 @@ private function generateAttributes(): array $returnType = null; - if ($this->returnType) { + if ($this->returnType !== null) { $returnType = $this->returnType->type(); } if ($this->returnTypeDocBlockHint !== null) { diff --git a/src/Code/PropertyGenerator.php b/src/Code/PropertyGenerator.php index a636862..55a6e1d 100644 --- a/src/Code/PropertyGenerator.php +++ b/src/Code/PropertyGenerator.php @@ -180,7 +180,7 @@ private function generateAttributes(): array if ($this->typed === false || $this->docBlockComment !== null || $this->typeDocBlockHint !== null) { $docBlockType = null; - if ($this->type) { + if ($this->type !== null) { $docBlockType = new VarTag($this->type->types()); } if ($typeHint = $this->getTypeDocBlockHint()) { @@ -188,20 +188,14 @@ private function generateAttributes(): array } $docBlock = null; - if ($this->docBlockComment) { + if ($docBlockType !== null) { + $docBlock = new DocBlock($this->docBlockComment); + $docBlock->addTag($docBlockType); + } elseif ($this->docBlockComment !== null) { $docBlock = new DocBlock($this->docBlockComment); - - if ($docBlockType !== null) { - $docBlock->addTag($docBlockType); - } - } - if ($this->docBlockComment === null && $docBlockType !== null) { - $docBlock = new DocBlock($this->docBlockComment, $docBlockType); } if ($docBlock !== null) { - $docBlock = new DocBlock($this->docBlockComment, $docBlockType); - $attributes = ['comments' => [new Doc($docBlock->generate())]]; } } diff --git a/src/NodeVisitor/ClassUseTrait.php b/src/NodeVisitor/ClassUseTrait.php index d1f523f..5e74474 100644 --- a/src/NodeVisitor/ClassUseTrait.php +++ b/src/NodeVisitor/ClassUseTrait.php @@ -42,7 +42,7 @@ public function afterTraverse(array $nodes): ?array if ($node instanceof Stmt\Class_) { foreach ($traits as $trait) { - $node->stmts[] = new Stmt\TraitUse([new Name($trait)]); + \array_unshift($node->stmts, new Stmt\TraitUse([new Name($trait)])); } } @@ -50,7 +50,7 @@ public function afterTraverse(array $nodes): ?array foreach ($node->stmts as $stmt) { if ($stmt instanceof Stmt\Class_) { foreach ($traits as $trait) { - $stmt->stmts[] = new Stmt\TraitUse([new Name($trait)]); + \array_unshift($stmt->stmts, new Stmt\TraitUse([new Name($trait)])); } } } diff --git a/src/NodeVisitor/NamespaceUse.php b/src/NodeVisitor/NamespaceUse.php index d622bfd..5294ff7 100644 --- a/src/NodeVisitor/NamespaceUse.php +++ b/src/NodeVisitor/NamespaceUse.php @@ -47,6 +47,7 @@ public function afterTraverse(array $nodes): ?array $newNodes[] = $node; if ($node instanceof Stmt\Namespace_) { + $stmts = $node->stmts; foreach ($imports as $import) { if (\is_array($import)) { $useNamespace = $this->builderFactory->use($import[0]); @@ -54,8 +55,9 @@ public function afterTraverse(array $nodes): ?array } else { $useNamespace = $this->builderFactory->use($import); } - $node->stmts[] = $useNamespace->getNode(); // @phpstan-ignore-line + \array_unshift($stmts, $useNamespace->getNode()); } + $node->stmts = $stmts; // @phpstan-ignore-line } } diff --git a/tests/NodeVisitor/ClassUseTraitTest.php b/tests/NodeVisitor/ClassUseTraitTest.php new file mode 100644 index 0000000..58294e5 --- /dev/null +++ b/tests/NodeVisitor/ClassUseTraitTest.php @@ -0,0 +1,89 @@ +parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); + $this->printer = new Standard(['shortArraySyntax' => true]); + } + + /** + * @test + */ + public function it_add_namespace_imports_in_correct_order(): void + { + $ast = $this->parser->parse($this->classCode()); + + $nodeTraverser = new NodeTraverser(); + $nodeTraverser->addVisitor(new ClassUseTrait('MyService\Foo', 'MyService\Bar')); + + $this->assertSame($this->expectedClassCode(), $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); + } + + private function classCode(): string + { + return <<<'EOF' + parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); + $this->printer = new Standard(['shortArraySyntax' => true]); + } + + /** + * @test + */ + public function it_add_namespace_imports_in_correct_order(): void + { + $ast = $this->parser->parse($this->classCode()); + + $nodeTraverser = new NodeTraverser(); + $nodeTraverser->addVisitor(new NamespaceUse('MyService\Foo', 'MyService\Bar')); + + $this->assertSame($this->expectedClassCode(), $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); + } + + private function classCode(): string + { + return <<<'EOF' +