Skip to content

Commit

Permalink
[e2e] handle multiple classes with anonymous class on MultipleClassFi…
Browse files Browse the repository at this point in the history
…leToPsr4ClassesRector file removed (#3253)

* [e2e] handle multiple classes with anonymous class on MultipleClassFileToPsr4ClassesRector

* Fixed 🎉

* Final touch: eol

* Really final touch: use ClassAnalyzer->isAnonymous() in other rules as well

* [ci-review] Rector Rectify

* Handle match and no match class with file

* output in ci

* Really Really Really final touch: eol

* Final touch: display Applied rules: MultipleClassFileToPsr4ClassesRector

* This is final touch: space

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 25, 2022
1 parent bce8153 commit 9a0938d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- 'e2e/applied-rule-removed-node'
- 'e2e/parallel with space'
- 'e2e/removed-and-added-files-collector'
- 'e2e/multiple-class-psr4'

name: End to end test - ${{ matrix.directory }}

Expand Down
1 change: 1 addition & 0 deletions e2e/multiple-class-psr4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
7 changes: 7 additions & 0 deletions e2e/multiple-class-psr4/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"php": "^8.1"
},
"minimum-stability": "dev",
"prefer-stable": true
}
43 changes: 43 additions & 0 deletions e2e/multiple-class-psr4/expected-output.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
! [NOTE] File
! "./multiple-class-psr4/src/C
! lassMatchFileName.php" will be added

! [NOTE] File
! "./multiple-class-psr4/src/S
! omeInterface.php" will be added

! [NOTE] File
! "./multiple-class-psr4/src/S
! omeClass.php" will be added

! [NOTE] File
! "./multiple-class-psr4/src/S
! omeOtherInterface.php" will be added

[WARNING] File "src/ClassNoMatchFileName.php" will be removed


1 file with changes
===================

1) src/ClassMatchFileName.php:1

---------- begin diff ----------
@@ @@

namespace App;

-class ClassMatchFileName implements SomeInterface
-{
-}
-
-interface SomeInterface
-{
-}
----------- end diff -----------

Applied rules:
* MultipleClassFileToPsr4ClassesRector


[OK] 1 file would have changed (dry-run) by Rector
14 changes: 14 additions & 0 deletions e2e/multiple-class-psr4/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector;

return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->paths([
__DIR__ . '/src',
]);

$rectorConfig->rule(MultipleClassFileToPsr4ClassesRector::class);
};
11 changes: 11 additions & 0 deletions e2e/multiple-class-psr4/src/ClassMatchFileName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App;

class ClassMatchFileName implements SomeInterface
{
}

interface SomeInterface
{
}
11 changes: 11 additions & 0 deletions e2e/multiple-class-psr4/src/ClassNoMatchFileName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App;

class SomeClass implements SomeOtherInterface
{
}

interface SomeOtherInterface
{
}
9 changes: 9 additions & 0 deletions e2e/multiple-class-psr4/src/WithAnonymousClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class WithAnonymousClass
{
public function run()
{
new class {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -24,6 +25,11 @@ final class TargetRemoveClassMethodRector extends AbstractRector implements Conf
*/
private array $targetRemoveClassMethods = [];

public function __construct(
private readonly ClassAnalyzer $classAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove defined class method', [
Expand Down Expand Up @@ -58,7 +64,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->isAnonymous()) {
if ($this->classAnalyzer->isAnonymousClass($node)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\Printer\NeighbourClassLikePrinter;
use Rector\Core\Rector\AbstractRector;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\PSR4\FileInfoAnalyzer\FileInfoDeletionAnalyzer;
use Rector\PSR4\NodeManipulator\NamespaceManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -27,6 +29,7 @@ public function __construct(
private readonly FileInfoDeletionAnalyzer $fileInfoDeletionAnalyzer,
private readonly NeighbourClassLikePrinter $neighbourClassLikePrinter,
private readonly RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
private readonly ClassAnalyzer $classAnalyzer
) {
}

Expand Down Expand Up @@ -105,10 +108,17 @@ public function refactor(Node $node): ?Node
return $nodeToReturn;
}

// 2. nothing to return - remove the file
$this->removedAndAddedFilesCollector->removeFile($this->file->getFilePath());
$isInaddedFiles = array_filter(
$this->removedAndAddedFilesCollector->getAddedFilesWithContent(),
fn (AddedFileWithContent $addedFileWithContent): bool => $addedFileWithContent->getFilePath() === $this->file->getFilePath()
);

if ($isInaddedFiles === []) {
// 2. nothing to return - remove the file
$this->removedAndAddedFilesCollector->removeFile($this->file->getFilePath());
}

return null;
return $node;
}

private function hasAtLeastTwoClassLikes(Node $node): bool
Expand Down Expand Up @@ -173,12 +183,12 @@ private function findNonAnonymousClassLikes(Node $node): array
{
$classLikes = $this->betterNodeFinder->findInstanceOf([$node], ClassLike::class);

return array_filter($classLikes, static function (ClassLike $classLike): bool {
return array_filter($classLikes, function (ClassLike $classLike): bool {
if (! $classLike instanceof Class_) {
return true;
}

return ! $classLike->isAnonymous();
return ! $this->classAnalyzer->isAnonymousClass($classLike);
});
}
}

0 comments on commit 9a0938d

Please sign in to comment.