Skip to content

Merge release 0.10.4 into 0.11.x #83

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 4 commits into from
Aug 27, 2021
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.10.4 - 2021-08-27


-----

### Release Notes for [0.10.4](https://github.com/open-code-modeling/php-code-ast/milestone/24)

0.10.x bugfix release (patch)

### 0.10.4

- Total issues resolved: **1**
- Total pull requests resolved: **0**
- Total contributors: **1**

#### bug

- [82: Exists check in NodeVisitor/ClassImplements not working properly](https://github.com/open-code-modeling/php-code-ast/issues/82) thanks to @sandrokeil

## 0.10.3 - 2021-01-29


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"phpstan/phpstan": "^0.12.33",
"phpstan/phpstan-strict-rules": "^0.12.4",
"phpunit/phpunit": "^9.5.0",
"prooph/php-cs-fixer-config": "^v0.3.1",
"prooph/php-cs-fixer-config": "^v0.4.0",
"roave/security-advisories": "dev-master"
},
"minimum-stability": "dev",
Expand Down
27 changes: 18 additions & 9 deletions src/NodeVisitor/ClassImplements.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,33 @@ public function afterTraverse(array $nodes): ?array

private function filterImplements(array $nodes): array
{
$implements = $this->implements;

foreach ($nodes as $node) {
if ($node instanceof Namespace_) {
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Stmt\Class_) {
foreach ($stmt->implements as $implementName) {
$implements = \array_filter($implements, static function (string $implement) use ($implementName) {
return $implement !== ($implementName instanceof FullyQualified
? '\\' . $implementName->toString()
: (string) $implementName);
});
}
return $this->filterClassImplements($stmt);
}
}
} elseif ($node instanceof Stmt\Class_) {
return $this->filterClassImplements($node);
}
}

return $this->implements;
}

private function filterClassImplements(Stmt\Class_ $node): array
{
$implements = $this->implements;

foreach ($node->implements as $implementName) {
$implements = \array_filter($implements, static function (string $implement) use ($implementName) {
return $implement !== ($implementName instanceof FullyQualified
? '\\' . $implementName->toString()
: (string) $implementName);
});
}

return $implements;
}
}
2 changes: 1 addition & 1 deletion src/NodeVisitor/NamespaceUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function afterTraverse(array $nodes): ?array
}
\array_unshift($stmts, $useNamespace->getNode());
}
$node->stmts = $stmts; // @phpstan-ignore-line
$node->stmts = $stmts;
}
}

Expand Down
217 changes: 217 additions & 0 deletions tests/NodeVisitor/ClassImplementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?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 Generator;
use OpenCodeModeling\CodeAst\Code\ClassGenerator;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassFile;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassImplements;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassNamespace;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PHPUnit\Framework\TestCase;

final class ClassImplementsTest extends TestCase
{
private Parser $parser;

private Standard $printer;

public function setUp(): void
{
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
$this->printer = new Standard(['shortArraySyntax' => true]);
}

/**
* Values are: interfaces
*
* @return Generator
*/
public function provideImplements(): Generator
{
yield '\\Awesome\\AcmeClass' => [['\\Awesome\\AcmeClass']];
yield '\\Foo' => [['\\Foo']];

yield '\\Awesome\\AcmeClass, \\My\\OtherInterface' => [['\\Awesome\\AcmeClass', '\\My\\OtherInterface']];
yield '\\Foo, \\Bar' => [['\\Foo', '\\Bar']];

yield 'FirstInterface, SecondInterface, ThirdInterface' => [['FirstInterface', 'SecondInterface', 'ThirdInterface']];
}

/**
* @test
* @dataProvider provideImplements
* @param array $interfaces
*/
public function it_generates_class_implements_for_empty_file(array $interfaces): void
{
$ast = $this->parser->parse('');

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassImplements(...$interfaces));

$extends = \implode(', ', $interfaces);

$expected = <<<EOF
<?php

class TestClass implements $extends
{
}
EOF;

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

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

/**
* @test
* @dataProvider provideImplements
* @param array $interfaces
*/
public function it_checks_class_implements_for_existing_file(array $interfaces): void
{
$ast = $this->parser->parse('<?php class TestClass {}');

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassImplements(...$interfaces));

$extends = \implode(', ', $interfaces);

$expected = <<<EOF
<?php

class TestClass implements $extends
{
}
EOF;

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

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

/**
* @test
* @dataProvider provideImplements
* @param array $interfaces
*/
public function it_generates_class_implements_with_namespace_for_empty_file(array $interfaces): void
{
$ast = $this->parser->parse('');

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassNamespace('My\\Awesome\\Service'));
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassImplements(...$interfaces));

$extends = \implode(', ', $interfaces);

$expected = <<<EOF
<?php

namespace My\Awesome\Service;

class TestClass implements $extends
{
}
EOF;

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

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

/**
* @test
* @dataProvider provideImplements
* @param array $interfaces
*/
public function it_generates_class_implements_for_namespace_file(array $interfaces): void
{
$code = <<<EOF
<?php

namespace My\Awesome\Service;
EOF;

$ast = $this->parser->parse($code);

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassImplements(...$interfaces));

$extends = \implode(', ', $interfaces);

$expected = <<<EOF
<?php

namespace My\Awesome\Service;

class TestClass implements $extends
{
}
EOF;

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

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

/**
* @test
* @dataProvider provideImplements
* @param array $interfaces
*/
public function it_checks_class_implements_with_namespace_for_existing_file(array $interfaces): void
{
$code = <<<EOF
<?php

namespace My\Awesome\Service;

class TestClass {}
EOF;

$ast = $this->parser->parse($code);

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass')));
$nodeTraverser->addVisitor(new ClassImplements(...$interfaces));

$extends = \implode(', ', $interfaces);

$expected = <<<EOF
<?php

namespace My\Awesome\Service;

class TestClass implements $extends
{
}
EOF;

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

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