Skip to content

Commit

Permalink
Re-use MovedFile (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 9, 2021
1 parent c913346 commit fa8e3bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
8 changes: 7 additions & 1 deletion packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ protected function assertFilesWereAdded(array $expectedAddedFileWithContents): v
private function resolveAddedFilePathsWithContents(): array
{
$addedFilePathsWithContents = $this->removedAndAddedFilesCollector->getAddedFilesWithContent();
$nodesWithFileDestinationPrinter = $this->getService(NodesWithFileDestinationPrinter::class);

$movedFiles = $this->removedAndAddedFilesCollector->getMovedFiles();
foreach ($movedFiles as $movedFile) {
$fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($movedFile);
$addedFilePathsWithContents[] = new AddedFileWithContent($movedFile->getNewFilePath(), $fileContent);
}

$addedFilesWithNodes = $this->removedAndAddedFilesCollector->getAddedFilesWithNodes();
if ($addedFilesWithNodes === []) {
return $addedFilePathsWithContents;
}

$nodesWithFileDestinationPrinter = $this->getService(NodesWithFileDestinationPrinter::class);
foreach ($addedFilesWithNodes as $addedFileWithNode) {
$fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNode);
$addedFilePathsWithContents[] = new AddedFileWithContent($addedFileWithNode->getFilePath(), $fileContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Restoration\Rector\ClassLike\UpdateFileNameByClassNameFileSystemRector\Fixture;

final class SkipDifferentClassName
{

}
26 changes: 5 additions & 21 deletions rules/PhpSpecToPHPUnit/PHPUnitTypeDeclarationDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

namespace Rector\PhpSpecToPHPUnit;

use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use ReflectionMethod;
use ReflectionNamedType;

/**
* Decorate setUp() and tearDown() with "void" when local TestClass class uses them
*/
final class PHPUnitTypeDeclarationDecorator
{
public function __construct(
private ReflectionResolver $reflectionResolver
private AstResolver $astResolver
) {
}

Expand All @@ -29,24 +26,11 @@ public function decorate(ClassMethod $classMethod): void
return;
}

$reflectionMethod = $this->reflectionResolver->resolveNativeClassMethodReflection(
'PHPUnit\Framework\TestCase',
MethodName::SET_UP
);

if (! $reflectionMethod instanceof ReflectionMethod) {
$setUpClassMethod = $this->astResolver->resolveClassMethod('PHPUnit\Framework\TestCase', MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
return;
}

if (! $reflectionMethod->hasReturnType()) {
return;
}

$returnType = $reflectionMethod->getReturnType();
$returnTypeName = $returnType instanceof ReflectionNamedType
? $returnType->getName()
: (string) $returnType;

$classMethod->returnType = new Identifier($returnTypeName);
$classMethod->returnType = $setUpClassMethod->returnType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use Rector\Core\Rector\AbstractRector;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -64,13 +65,14 @@ public function refactor(Node $node): ?Node
return null;
}

$file = $node->getAttribute(AttributeKey::FILE);
if (! $file instanceof File) {
return null;
}

// no match → rename file
$newFileLocation = $smartFileInfo->getPath() . DIRECTORY_SEPARATOR . $classShortName . '.php';

$addedFileWithContent = new AddedFileWithContent($newFileLocation, $smartFileInfo->getContents());
$this->removedAndAddedFilesCollector->removeFile($smartFileInfo);

$this->removedAndAddedFilesCollector->addAddedFile($addedFileWithContent);
$this->removedAndAddedFilesCollector->addMovedFile($file, $newFileLocation);

return null;
}
Expand Down

0 comments on commit fa8e3bd

Please sign in to comment.