Skip to content

Commit

Permalink
skip trait/interface/enum (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Sep 6, 2024
1 parent d36e2a2 commit 8db44af
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/PhpParser/NodeVisitor/FindClassConstFetchNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
Expand All @@ -34,7 +35,7 @@ final class FindClassConstFetchNodeVisitor extends NodeVisitorAbstract

public function enterNode(Node $node): Node|int|null
{
if ($node instanceof Interface_ || $node instanceof Trait_) {
if ($node instanceof Interface_ || $node instanceof Trait_ || $node instanceof Enum_) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder;

use Rector\SwissKnife\Contract\ClassConstantFetchInterface;
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Rector\SwissKnife\PhpParser\ClassConstantFetchFinder;
use Rector\SwissKnife\Tests\AbstractTestCase;
Expand All @@ -23,13 +24,15 @@ protected function setUp(): void
$this->classConstantsFetchFinder = $this->make(ClassConstantFetchFinder::class);
}

public function test(): void
public function testSkipInterfaceTraitAndEnum(): void
{
$progressBar = new ProgressBar(new NullOutput());

$fileInfos = PhpFilesFinder::find([__DIR__ . '/Fixture']);
$classConstantFetches = $this->findInDirectory(__DIR__ . '/Fixture/Skip');
$this->assertCount(0, $classConstantFetches);
}

$classConstantFetches = $this->classConstantsFetchFinder->find($fileInfos, $progressBar, false);
public function test(): void
{
$classConstantFetches = $this->findInDirectory(__DIR__ . '/Fixture/Standard');
$this->assertCount(2, $classConstantFetches);

$firstClassConstantFetch = $classConstantFetches[0];
Expand All @@ -38,4 +41,15 @@ public function test(): void
$secondClassConstantFetch = $classConstantFetches[1];
$this->assertInstanceOf(ExternalClassAccessConstantFetch::class, $secondClassConstantFetch);
}

/**
* @return ClassConstantFetchInterface[]
*/
private function findInDirectory(string $directory): array
{
$progressBar = new ProgressBar(new NullOutput());
$fileInfos = PhpFilesFinder::find([$directory]);

return $this->classConstantsFetchFinder->find($fileInfos, $progressBar, false);
}
}
13 changes: 13 additions & 0 deletions tests/PhpParser/ClassConstantFetchFinder/Fixture/Skip/SomeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture\Skip;

enum SomeEnum
{
public const VALUE = 1000;

public function run()
{
return self::VALUE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture\Skip;

interface SomeInterface
{
public const VALUE = 1000;

public function go($input = self::VALUE);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture\Skip;

trait SomeTrait
{
public const VALUE = 1000;

public function run()
{
return self::VALUE;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture;
namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture\Standard;

final class AnotherClassWithConstant
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture;
namespace Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\Fixture\Standard;

final class SomeFileWithConstants
{
Expand Down

0 comments on commit 8db44af

Please sign in to comment.