Skip to content

Merge release 0.8.3 into 0.9.x #35

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

Merged
merged 5 commits into from
Nov 13, 2020
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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
4 changes: 2 additions & 2 deletions src/Builder/ClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Code/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 5 additions & 11 deletions src/Code/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,22 @@ 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()) {
$docBlockType = new VarTag($typeHint);
}
$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())]];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/NodeVisitor/ClassUseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ 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)]));
}
}

if ($node instanceof Namespace_) {
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)]));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/NodeVisitor/NamespaceUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ 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]);
$useNamespace->as($import[1]);
} else {
$useNamespace = $this->builderFactory->use($import);
}
$node->stmts[] = $useNamespace->getNode(); // @phpstan-ignore-line
\array_unshift($stmts, $useNamespace->getNode());
}
$node->stmts = $stmts; // @phpstan-ignore-line
}
}

Expand Down
89 changes: 89 additions & 0 deletions tests/NodeVisitor/ClassUseTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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 OpenCodeModelingTest\CodeAst\NodeVisitor;

use OpenCodeModeling\CodeAst\NodeVisitor\ClassUseTrait;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PHPUnit\Framework\TestCase;

final class ClassUseTraitTest 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_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'
<?php

declare(strict_types=1);

namespace My\Awesome\Service;

class TestClass
{
use MyService\Foo;

public function testMethod()
{
}
}
EOF;
}

private function expectedClassCode(): string
{
return <<<'EOF'
<?php

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

class TestClass
{
use MyService\Bar;
use MyService\Foo;
public function testMethod()
{
}
}
EOF;
}
}
83 changes: 83 additions & 0 deletions tests/NodeVisitor/NamespaceUseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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 OpenCodeModelingTest\CodeAst\NodeVisitor;

use OpenCodeModeling\CodeAst\NodeVisitor\NamespaceUse;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PHPUnit\Framework\TestCase;

final class NamespaceUseTest 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_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'
<?php

declare(strict_types=1);

namespace My\Awesome\Service;

use MyService\Foo;

class TestClass
{
}
EOF;
}

private function expectedClassCode(): string
{
return <<<'EOF'
<?php

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

use MyService\Bar;
use MyService\Foo;
class TestClass
{
}
EOF;
}
}