remove MoveAndRenameClassRector, confusing, preffer class rename

This commit is contained in:
TomasVotruba 2020-06-18 22:42:29 +02:00
parent 14a72f15d8
commit eaf615c44e
5 changed files with 3 additions and 185 deletions

View File

@ -1,4 +1,4 @@
# All 511 Rectors Overview
# All 510 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -53,7 +53,6 @@
- [PhpSpecToPHPUnit](#phpspectophpunit) (7)
- [Polyfill](#polyfill) (2)
- [Privatization](#privatization) (7)
- [Refactoring](#refactoring) (1)
- [RemovingStatic](#removingstatic) (4)
- [Renaming](#renaming) (10)
- [Restoration](#restoration) (7)
@ -9067,43 +9066,6 @@ Privatize local-only property to private property
<br><br>
## Refactoring
### `MoveAndRenameClassRector`
- class: [`Rector\Refactoring\Rector\FileSystem\MoveAndRenameClassRector`](/../master/packages/refactoring/src/Rector/FileSystem/MoveAndRenameClassRector.php)
Move class to respect new location with respect to PSR-4 + follow up with class rename
```yaml
services:
Rector\Refactoring\Rector\FileSystem\MoveAndRenameClassRector:
$oldToNewClass:
SomeClass: DifferentClass
```
```diff
-// src/SomeClass.php
-class SomeClass
+// src/DifferentClass.php
+class DifferentClass
{
}
class AnotherClass
{
public function create()
{
- return new SomeClass;
+ return new DifferentClass;
}
}
```
<br><br>
## RemovingStatic
### `NewUniqueObjectToEntityFactoryRector`

View File

@ -16,7 +16,6 @@ use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\PhpParser\Printer\FormatPerservingPrinter;
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\PSR4\Collector\RenamedClassesCollector;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
@ -52,11 +51,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
*/
private $formatPerservingPrinter;
/**
* @var NodeScopeAndMetadataDecorator
*/
private $nodeScopeAndMetadataDecorator;
/**
* @var ParserFactory
*/
@ -89,7 +83,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
ParserFactory $parserFactory,
Lexer $lexer,
FormatPerservingPrinter $formatPerservingPrinter,
NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
Configuration $configuration,
BetterStandardPrinter $betterStandardPrinter,
ParameterProvider $parameterProvider,
@ -101,7 +94,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
$this->parserFactory = $parserFactory;
$this->lexer = $lexer;
$this->formatPerservingPrinter = $formatPerservingPrinter;
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->configuration = $configuration;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->parameterProvider = $parameterProvider;
@ -125,12 +117,12 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
$this->fileProcessor->parseFileInfoToLocalCache($smartFileInfo);
}
[, $oldStmts] = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);
[$newStmts, $oldStmts] = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);
// needed for format preserving
$this->oldStmts = $oldStmts;
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $smartFileInfo);
return $newStmts;
}
/**

View File

@ -1,108 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Refactoring\Rector\FileSystem;
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
use Rector\PSR4\FileRelocationResolver;
use Symplify\SmartFileSystem\SmartFileInfo;
final class MoveAndRenameClassRector extends AbstractFileSystemRector
{
/**
* @var string[]
*/
private $oldToNewClass = [];
/**
* @var FileRelocationResolver
*/
private $fileRelocationResolver;
/**
* @param string[] $oldToNewClass
*/
public function __construct(FileRelocationResolver $fileRelocationResolver, array $oldToNewClass = [])
{
$this->fileRelocationResolver = $fileRelocationResolver;
$this->oldToNewClass = $oldToNewClass;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Move class to respect new location with respect to PSR-4 + follow up with class rename', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
// src/SomeClass.php
class SomeClass
{
}
class AnotherClass
{
public function create()
{
return new SomeClass;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
// src/DifferentClass.php
class DifferentClass
{
}
class AnotherClass
{
public function create()
{
return new DifferentClass;
}
}
CODE_SAMPLE
, [
'$oldToNewClass' => [
'SomeClass' => 'DifferentClass',
],
]
),
]
);
}
public function refactor(SmartFileInfo $smartFileInfo): void
{
$fileNodes = $this->parseFileInfoToNodes($smartFileInfo);
$fileContent = $smartFileInfo->getContents();
$class = $this->betterNodeFinder->findFirstClass($fileNodes);
if ($class === null) {
return;
}
$className = $this->getName($class);
/** @var string $oldClass */
foreach ($this->oldToNewClass as $oldClass => $newClass) {
if ($className !== $oldClass) {
continue;
}
$newFileLocation = $this->fileRelocationResolver->resolveNewFileLocationFromOldClassToNewClass(
$smartFileInfo,
$oldClass,
$newClass
);
// create helping rename class rector.yaml + class_alias autoload file
$this->addClassRename($oldClass, $newClass);
$this->moveFile($smartFileInfo, $newFileLocation, $fileContent);
}
}
}

View File

@ -210,24 +210,6 @@ final class BetterNodeFinder
});
}
/**
* @param Node|Node[] $nodes
*/
public function findFirstClass($nodes): ?Class_
{
/** @var Class_[] $classes */
$classes = $this->findInstanceOf($nodes, Class_::class);
foreach ($classes as $class) {
if ($class->isAnonymous()) {
continue;
}
return $class;
}
return null;
}
/**
* @param string[] $types
*/

View File

@ -32,14 +32,4 @@ final class RenamedNamespaceValueObject
{
return str_replace($this->oldNamespace, $this->newNamespace, $this->currentName);
}
public function getOldNamespace(): string
{
return $this->oldNamespace;
}
public function getNewNamespace(): string
{
return $this->newNamespace;
}
}