mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
move suffix filesystem 2 (#4529)
Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
3d4762b3b5
commit
c03b66f7cd
2
.github/workflows/code_analysis.yaml
vendored
2
.github/workflows/code_analysis.yaml
vendored
@ -40,7 +40,7 @@ jobs:
|
||||
|
||||
-
|
||||
name: 'PHP Linter'
|
||||
run: vendor/bin/parallel-lint src bin/rector config compiler tests packages --exclude packages/rector-generator/templates --colors
|
||||
run: vendor/bin/parallel-lint src bin/rector config compiler tests packages rules --exclude packages/rector-generator/templates --colors
|
||||
|
||||
name: ${{ matrix.actions.name }}
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -66,8 +66,6 @@ It supports all versions of PHP from 5.2 and major open-source projects:
|
||||
- [Explore 600+ Rector Rules](/docs/rector_rules_overview.md)
|
||||
- [How Does Rector Work?](/docs/how_it_works.md)
|
||||
- [PHP Parser Nodes Overview](/docs/nodes_overview.md)
|
||||
- [How Does Rector Work?](/docs/how_it_works.md)
|
||||
- [How to Add Test for Rector Rule](/docs/how_to_add_test_for_rector_rule.md)
|
||||
|
||||
### Advanced
|
||||
|
||||
@ -78,6 +76,12 @@ It supports all versions of PHP from 5.2 and major open-source projects:
|
||||
- [How run Rector in Docker](/docs/how_to_run_rector_in_docker.md)
|
||||
- [Add Checkstyle with your CI](/docs/checkstyle.md)
|
||||
|
||||
### Contributing
|
||||
|
||||
- [How to Add Test for Rector Rule](/docs/how_to_add_test_for_rector_rule.md)
|
||||
- [How to Create New Rector Rule](/docs/create_own_rule.md)
|
||||
- [How to Generate New Rector Rule with Recipe](/docs/rector_recipe.md)
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
|
@ -185,7 +185,7 @@
|
||||
},
|
||||
"autoload-dev": {
|
||||
"classmap": [
|
||||
"rules/autodiscovery/tests/Rector/FileSystem/MoveInterfacesToContractNamespaceDirectoryRector/Expected",
|
||||
"rules/autodiscovery/tests/Rector/FileNode/MoveInterfacesToContractNamespaceDirectoryRector/Expected",
|
||||
"rules/autodiscovery/tests/Rector/FileNode/MoveServicesBySuffixToDirectoryRector/Expected",
|
||||
"rules/cakephp/tests/Rector/FileWithoutNamespace/ImplicitShortClassNameUseStatementRector/Source",
|
||||
"rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Source",
|
||||
|
@ -133,7 +133,7 @@ Turns `$this->getRepository()` in Symfony Controller to constructor injection an
|
||||
|
||||
### `MoveEntitiesToEntityDirectoryRector`
|
||||
|
||||
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveEntitiesToEntityDirectoryRector.php)
|
||||
- class: [`Rector\Autodiscovery\Rector\FileNode\MoveEntitiesToEntityDirectoryRector`](/rules/autodiscovery/src/Rector/FileNode/MoveEntitiesToEntityDirectoryRector.php)
|
||||
|
||||
Move entities to Entity namespace
|
||||
|
||||
@ -158,7 +158,7 @@ Move entities to Entity namespace
|
||||
|
||||
### `MoveInterfacesToContractNamespaceDirectoryRector`
|
||||
|
||||
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveInterfacesToContractNamespaceDirectoryRector.php)
|
||||
- class: [`Rector\Autodiscovery\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector`](/rules/autodiscovery/src/Rector/FileNode/MoveInterfacesToContractNamespaceDirectoryRector.php)
|
||||
|
||||
Move interface to "Contract" namespace
|
||||
|
||||
@ -218,7 +218,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
|
||||
### `MoveValueObjectsToValueObjectDirectoryRector`
|
||||
|
||||
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveValueObjectsToValueObjectDirectoryRector.php)
|
||||
- class: [`Rector\Autodiscovery\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector`](/rules/autodiscovery/src/Rector/FileNode/MoveValueObjectsToValueObjectDirectoryRector.php)
|
||||
|
||||
Move value object to ValueObject namespace/directory
|
||||
|
||||
@ -227,7 +227,7 @@ Move value object to ValueObject namespace/directory
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
|
@ -100,7 +100,7 @@ final class RectorsDocumentationPrinter
|
||||
|
||||
/**
|
||||
* @param RectorInterface[] $rectors
|
||||
* @return RectorInterface[][]
|
||||
* @return array<string, RectorInterface[]&mixed[]>
|
||||
*/
|
||||
private function groupRectorsByPackage(array $rectors): array
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\DocumentationGenerator\Tests\Printer\CodeSamplePrinter;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Core\Contract\Rector\RectorInterface;
|
||||
use Rector\Core\HttpKernel\RectorKernel;
|
||||
use Rector\DocumentationGenerator\Printer\CodeSamplePrinter;
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\FileSystemRector\Contract;
|
||||
|
||||
interface AddedFileInterface
|
||||
{
|
||||
public function getFilePath(): string;
|
||||
}
|
@ -10,6 +10,7 @@ use PhpParser\Node;
|
||||
use Rector\Core\Application\FileProcessor;
|
||||
use Rector\Core\Application\TokensByFilePathStorage;
|
||||
use Rector\Core\Configuration\Configuration;
|
||||
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\Core\PhpParser\Printer\FormatPerservingPrinter;
|
||||
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
|
||||
@ -22,6 +23,7 @@ use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
/**
|
||||
* @deprecated Use
|
||||
* @see \Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace node instead
|
||||
* @see FileNode node instead
|
||||
*/
|
||||
abstract class AbstractFileSystemRector implements FileSystemRectorInterface
|
||||
{
|
||||
|
@ -3,9 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\FileSystemRector\ValueObject;
|
||||
|
||||
use Rector\FileSystemRector\Contract\AddedFileInterface;
|
||||
|
||||
final class AddedFileWithContent implements AddedFileInterface
|
||||
final class AddedFileWithContent
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -46,7 +46,7 @@ final class MovedFileWithNodesFactory
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
public function create(
|
||||
public function createWithDesiredGroup(
|
||||
SmartFileInfo $oldFileInfo,
|
||||
array $nodes,
|
||||
string $desiredGroupName
|
||||
@ -87,8 +87,14 @@ final class MovedFileWithNodesFactory
|
||||
);
|
||||
|
||||
// 3. update fully qualifed name of the class like - will be used further
|
||||
/** @var ClassLike $classLike */
|
||||
/** @var ClassLike|null $classLike */
|
||||
$classLike = $this->betterNodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
|
||||
if ($classLike === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// clone to prevent deep override
|
||||
$classLike = clone $classLike;
|
||||
$classLike->namespacedName = new FullyQualified($newClassName);
|
||||
|
||||
return new MovedFileWithNodes($nodes, $newFileDestination, $oldFileInfo, $oldClassName, $newClassName);
|
||||
@ -118,6 +124,9 @@ final class MovedFileWithNodesFactory
|
||||
continue;
|
||||
}
|
||||
|
||||
// prevent namespace override
|
||||
$node = clone $node;
|
||||
|
||||
$node->name = new Name($newNamespaceName);
|
||||
break;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\Testing\PHPUnit;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\ValueObject\InputFilePathWithExpectedFile;
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\ValueObject\InputFilePathWithExpectedFile;
|
||||
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
|
||||
use Rector\Core\Configuration\Configuration;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
|
@ -14,6 +14,7 @@ use Rector\Core\ValueObject\StaticNonPhpFileSuffixes;
|
||||
use Rector\FileSystemRector\Contract\MovedFileInterface;
|
||||
use Rector\Testing\Contract\RunnableInterface;
|
||||
use Rector\Testing\PHPUnit\Behavior\MovingFilesTrait;
|
||||
use Rector\Testing\ValueObject\InputFilePathWithExpectedFile;
|
||||
use Symplify\EasyTesting\DataProvider\StaticFixtureUpdater;
|
||||
use Symplify\EasyTesting\StaticFixtureSplitter;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
@ -44,7 +45,10 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||
$this->autoloadTestFixture = true;
|
||||
}
|
||||
|
||||
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo): void
|
||||
/**
|
||||
* @param InputFilePathWithExpectedFile[] $extraFiles
|
||||
*/
|
||||
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo, array $extraFiles = []): void
|
||||
{
|
||||
$this->fixtureGuard->ensureFileInfoHasDifferentBeforeAndAfterContent($fixtureFileInfo);
|
||||
|
||||
@ -58,7 +62,7 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||
|
||||
$expectedFileInfo = $inputFileInfoAndExpectedFileInfo->getExpectedFileInfo();
|
||||
|
||||
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo);
|
||||
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo, $extraFiles);
|
||||
|
||||
$this->originalTempFileInfo = $inputFileInfo;
|
||||
|
||||
@ -117,19 +121,42 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||
return $this->removedAndAddedFilesCollector->getMovedFileByFileInfo($this->originalTempFileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InputFilePathWithExpectedFile[] $extraFiles
|
||||
*/
|
||||
private function doTestFileMatchesExpectedContent(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
SmartFileInfo $expectedFileInfo,
|
||||
SmartFileInfo $fixtureFileInfo
|
||||
SmartFileInfo $fixtureFileInfo,
|
||||
array $extraFiles = []
|
||||
): void {
|
||||
$this->setParameter(Option::SOURCE, [$originalFileInfo->getRealPath()]);
|
||||
|
||||
if ($originalFileInfo->getSuffix() === 'php') {
|
||||
// life-cycle trio :)
|
||||
$this->fileProcessor->parseFileInfoToLocalCache($originalFileInfo);
|
||||
$this->fileProcessor->refactor($originalFileInfo);
|
||||
if ($extraFiles === []) {
|
||||
$this->fileProcessor->parseFileInfoToLocalCache($originalFileInfo);
|
||||
$this->fileProcessor->refactor($originalFileInfo);
|
||||
$this->fileProcessor->postFileRefactor($originalFileInfo);
|
||||
} else {
|
||||
$fileInfosToProcess = [$originalFileInfo];
|
||||
|
||||
$this->fileProcessor->postFileRefactor($originalFileInfo);
|
||||
foreach ($extraFiles as $extraFile) {
|
||||
$fileInfosToProcess[] = $extraFile->getInputFileInfo();
|
||||
}
|
||||
|
||||
// life-cycle trio :)
|
||||
foreach ($fileInfosToProcess as $fileInfoToProcess) {
|
||||
$this->fileProcessor->parseFileInfoToLocalCache($fileInfoToProcess);
|
||||
}
|
||||
|
||||
foreach ($fileInfosToProcess as $fileInfoToProcess) {
|
||||
$this->fileProcessor->refactor($fileInfoToProcess);
|
||||
}
|
||||
|
||||
foreach ($fileInfosToProcess as $fileInfoToProcess) {
|
||||
$this->fileProcessor->postFileRefactor($fileInfoToProcess);
|
||||
}
|
||||
}
|
||||
|
||||
// mimic post-rectors
|
||||
$changedContent = $this->fileProcessor->printToString($originalFileInfo);
|
||||
|
@ -19,20 +19,25 @@ trait MovingFilesTrait
|
||||
$this->assertNull($movedFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AddedFileWithContent[] $expectedFilePathsWithContents
|
||||
*/
|
||||
protected function assertFilesWereAdded(array $expectedFilePathsWithContents): void
|
||||
protected function assertFileWithContentWasAdded(AddedFileWithContent $addedFileWithContent): void
|
||||
{
|
||||
Assert::allIsAOf($expectedFilePathsWithContents, AddedFileWithContent::class);
|
||||
$this->assertFilesWereAdded([$addedFileWithContent]);
|
||||
}
|
||||
|
||||
$addedFilePathsWithContents = $this->removedAndAddedFilesCollector->getAddedFiles();
|
||||
/**
|
||||
* @param AddedFileWithContent[] $addedFileWithContents
|
||||
*/
|
||||
protected function assertFilesWereAdded(array $addedFileWithContents): void
|
||||
{
|
||||
Assert::allIsAOf($addedFileWithContents, AddedFileWithContent::class);
|
||||
|
||||
$addedFilePathsWithContents = $this->removedAndAddedFilesCollector->getAddedFilesWithContent();
|
||||
|
||||
sort($addedFilePathsWithContents);
|
||||
sort($expectedFilePathsWithContents);
|
||||
sort($addedFileWithContents);
|
||||
|
||||
foreach ($addedFilePathsWithContents as $key => $addedFilePathWithContent) {
|
||||
$expectedFilePathWithContent = $expectedFilePathsWithContents[$key];
|
||||
$expectedFilePathWithContent = $addedFileWithContents[$key];
|
||||
|
||||
$this->assertSame(
|
||||
$expectedFilePathWithContent->getFilePath(),
|
||||
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Testing\ValueObject;
|
||||
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class InputFilePathWithExpectedFile
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $inputFilePath;
|
||||
|
||||
/**
|
||||
* @var AddedFileWithContent
|
||||
*/
|
||||
private $addedFileWithContent;
|
||||
|
||||
public function __construct(string $inputFilePath, AddedFileWithContent $addedFileWithContent)
|
||||
{
|
||||
$this->inputFilePath = $inputFilePath;
|
||||
$this->addedFileWithContent = $addedFileWithContent;
|
||||
}
|
||||
|
||||
public function getInputFileInfo(): SmartFileInfo
|
||||
{
|
||||
return new SmartFileInfo($this->inputFilePath);
|
||||
}
|
||||
|
||||
public function getAddedFileWithContent(): AddedFileWithContent
|
||||
{
|
||||
return $this->addedFileWithContent;
|
||||
}
|
||||
}
|
@ -2,25 +2,24 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Rector\FileSystem;
|
||||
namespace Rector\Autodiscovery\Rector\FileNode;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
|
||||
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @sponsor Thanks https://spaceflow.io/ for sponsoring this rule - visit them on https://github.com/SpaceFlow-app
|
||||
*
|
||||
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
|
||||
*
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector\MoveEntitiesToEntityDirectoryRectorTest
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileNode\MoveEntitiesToEntityDirectoryRector\MoveEntitiesToEntityDirectoryRectorTest
|
||||
*/
|
||||
final class MoveEntitiesToEntityDirectoryRector extends AbstractFileSystemRector
|
||||
final class MoveEntitiesToEntityDirectoryRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
@ -63,37 +62,51 @@ CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
|
||||
public function refactor(SmartFileInfo $smartFileInfo): void
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
$nodes = $this->parseFileInfoToNodes($smartFileInfo);
|
||||
if (! $this->areDoctrineEntityNodes($nodes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// is entity in expected directory?
|
||||
if (Strings::match($smartFileInfo->getRealPath(), self::ENTITY_PATH_REGEX)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->create($smartFileInfo, $nodes, 'Entity');
|
||||
if ($movedFileWithNodes === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addMovedFile($movedFileWithNodes);
|
||||
return [FileNode::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @param FileNode $node
|
||||
*/
|
||||
private function areDoctrineEntityNodes(array $nodes): bool
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isDoctrineEntityFileNode($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// is entity in expected directory?
|
||||
$smartFileInfo = $node->getFileInfo();
|
||||
if (Strings::match($smartFileInfo->getRealPath(), self::ENTITY_PATH_REGEX)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
|
||||
$smartFileInfo,
|
||||
$node->stmts,
|
||||
'Entity'
|
||||
);
|
||||
if ($movedFileWithNodes === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->addMovedFile($movedFileWithNodes);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isDoctrineEntityFileNode(FileNode $fileNode): bool
|
||||
{
|
||||
/** @var Class_|null $class */
|
||||
$class = $this->betterNodeFinder->findFirstInstanceOf($nodes, Class_::class);
|
||||
$class = $this->betterNodeFinder->findFirstInstanceOf($fileNode->stmts, Class_::class);
|
||||
if ($class === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->hasPhpDocTagValueNode($class, EntityTagValueNode::class);
|
||||
return $this->isDoctrineEntityClass($class);
|
||||
}
|
||||
}
|
@ -2,24 +2,24 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Rector\FileSystem;
|
||||
namespace Rector\Autodiscovery\Rector\FileNode;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
|
||||
use Rector\NetteToSymfony\Analyzer\NetteControlFactoryInterfaceAnalyzer;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @sponsor Thanks https://spaceflow.io/ for sponsoring this rule - visit them on https://github.com/SpaceFlow-app
|
||||
*
|
||||
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
|
||||
*
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\MoveInterfacesToContractNamespaceDirectoryRectorTest
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\MoveInterfacesToContractNamespaceDirectoryRectorTest
|
||||
*/
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRector extends AbstractFileSystemRector
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var NetteControlFactoryInterfaceAnalyzer
|
||||
@ -56,33 +56,40 @@ CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
|
||||
public function refactor(SmartFileInfo $smartFileInfo): void
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
$nodes = $this->parseFileInfoToNodes($smartFileInfo);
|
||||
|
||||
$this->processInterfacesToContract($smartFileInfo, $nodes);
|
||||
return [FileNode::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @param FileNode $node
|
||||
*/
|
||||
private function processInterfacesToContract(SmartFileInfo $smartFileInfo, array $nodes): void
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
/** @var Interface_|null $interface */
|
||||
$interface = $this->betterNodeFinder->findFirstInstanceOf($nodes, Interface_::class);
|
||||
$interface = $this->betterNodeFinder->findFirstInstanceOf([$node], Interface_::class);
|
||||
if ($interface === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->netteControlFactoryInterfaceAnalyzer->isComponentFactoryInterface($interface)) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->create($smartFileInfo, $nodes, 'Contract');
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
|
||||
$node->getFileInfo(),
|
||||
$node->stmts,
|
||||
'Contract'
|
||||
);
|
||||
if ($movedFileWithNodes === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->addMovedFile($movedFileWithNodes);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -154,7 +154,11 @@ CODE_SAMPLE
|
||||
|
||||
private function moveFileToGroupName(SmartFileInfo $fileInfo, FileNode $fileNode, string $desiredGroupName): void
|
||||
{
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->create($fileInfo, [$fileNode], $desiredGroupName);
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
|
||||
$fileInfo,
|
||||
$fileNode->stmts,
|
||||
$desiredGroupName
|
||||
);
|
||||
if ($movedFileWithNodes === null) {
|
||||
return;
|
||||
}
|
||||
|
@ -2,17 +2,18 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Rector\FileSystem;
|
||||
namespace Rector\Autodiscovery\Rector\FileNode;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Autodiscovery\Analyzer\ClassAnalyzer;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\PhpParser\Node\CustomNode\FileNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
|
||||
@ -20,19 +21,19 @@ use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
*
|
||||
* @sponsor Thanks https://spaceflow.io/ for sponsoring this rule - visit them on https://github.com/SpaceFlow-app
|
||||
*
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\MoveValueObjectsToValueObjectDirectoryRectorTest
|
||||
* @see \Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\MoveValueObjectsToValueObjectDirectoryRectorTest
|
||||
*/
|
||||
final class MoveValueObjectsToValueObjectDirectoryRector extends AbstractFileSystemRector implements ConfigurableRectorInterface
|
||||
final class MoveValueObjectsToValueObjectDirectoryRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const TYPES = '$types';
|
||||
public const TYPES = 'types';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const SUFFIXES = '$suffixes';
|
||||
public const SUFFIXES = 'suffixes';
|
||||
|
||||
/**
|
||||
* @api
|
||||
@ -121,26 +122,44 @@ CODE_SAMPLE
|
||||
]);
|
||||
}
|
||||
|
||||
public function refactor(SmartFileInfo $smartFileInfo): void
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [FileNode::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FileNode $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$nodes = $this->parseFileInfoToNodes($smartFileInfo);
|
||||
|
||||
/** @var Class_|null $class */
|
||||
$class = $this->betterNodeFinder->findFirstInstanceOf($nodes, Class_::class);
|
||||
$class = $this->betterNodeFinder->findFirstInstanceOf([$node], Class_::class);
|
||||
if ($class === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isValueObjectMatch($class)) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->create($smartFileInfo, $nodes, 'ValueObject');
|
||||
$smartFileInfo = $node->getFileInfo();
|
||||
$movedFileWithNodes = $this->movedFileWithNodesFactory->createWithDesiredGroup(
|
||||
$smartFileInfo,
|
||||
$node->stmts,
|
||||
'ValueObject'
|
||||
);
|
||||
|
||||
if ($movedFileWithNodes === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->addMovedFile($movedFileWithNodes);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function configure(array $configuration): void
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector\Source\Entity;
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveEntitiesToEntityDirectoryRector\Source\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
/**
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveEntitiesToEntityDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveEntitiesToEntityDirectoryRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class MoveEntitiesToEntityDirectoryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
AddedFileWithContent $expectedAddedFileWithContent
|
||||
): void {
|
||||
$this->doTestFileInfo($originalFileInfo);
|
||||
$this->assertFileWithContentWasAdded($expectedAddedFileWithContent);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$smartFileSystem = new SmartFileSystem();
|
||||
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Controller/RandomEntity.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/RandomEntity.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ExpectedRandomEntity.php')
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return MoveEntitiesToEntityDirectoryRector::class;
|
||||
}
|
||||
|
||||
protected function getPhpVersion(): string
|
||||
{
|
||||
return PhpVersionFeature::BEFORE_TYPED_PROPERTIES;
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector\Source\Controller;
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveEntitiesToEntityDirectoryRector\Source\Controller;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
/**
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
final class SameClassImplementEntity implements \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
public function __construct(\Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random)
|
||||
{
|
||||
}
|
||||
|
||||
public function returnAnother(): \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
public \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random;
|
||||
|
||||
public function create(): \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Testing\ValueObject\InputFilePathWithExpectedFile;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @requires PHP 7.4
|
||||
* @dataProvider provideData()
|
||||
* @param InputFilePathWithExpectedFile[] $extraFiles
|
||||
*/
|
||||
public function test(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
?AddedFileWithContent $expectedAddedFileWithContent,
|
||||
array $extraFiles = []
|
||||
): void {
|
||||
$this->doTestFileInfo($originalFileInfo, $extraFiles);
|
||||
|
||||
if ($expectedAddedFileWithContent !== null) {
|
||||
$this->assertFileWithContentWasAdded($expectedAddedFileWithContent);
|
||||
} else {
|
||||
$this->assertFileWasNotChanged($this->originalTempFileInfo);
|
||||
}
|
||||
|
||||
$expectedAddedFilesWithContent = [];
|
||||
foreach ($extraFiles as $extraFile) {
|
||||
$expectedAddedFilesWithContent[] = $extraFile->getAddedFileWithContent();
|
||||
}
|
||||
|
||||
$this->assertFilesWereAdded($expectedAddedFilesWithContent);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$smartFileSystem = new SmartFileSystem();
|
||||
|
||||
$extraFiles = [
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/RandomInterfaceUseCase.php',
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/RandomInterfaceUseCase.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ExpectedRandomInterfaceUseCase.php')
|
||||
)
|
||||
),
|
||||
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/ValueObject/SameClassImplementEntity.php',
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/SameClassImplementEntity.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/Entity/ExpectedSameClassImplementEntity.php')
|
||||
)
|
||||
),
|
||||
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/Entity/RandomInterfaceUseCaseInTheSameNamespace.php',
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/RandomInterfaceUseCaseInTheSameNamespace.php',
|
||||
$smartFileSystem->readFile(
|
||||
__DIR__ . '/Expected/Entity/RandomInterfaceUseCaseInTheSameNamespace.php'
|
||||
)
|
||||
)
|
||||
),
|
||||
];
|
||||
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Entity/RandomInterface.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Contract/RandomInterface.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ExpectedRandomInterface.php')
|
||||
),
|
||||
// extra files
|
||||
$extraFiles,
|
||||
];
|
||||
|
||||
// skip nette control factory
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Control/ControlFactory.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Control/ControlFactory.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Source/Control/ControlFactory.php')
|
||||
),
|
||||
];
|
||||
|
||||
// skip form control factory, even in docblock
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Control/FormFactory.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/Control/FormFactory.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Source/Control/FormFactory.php')
|
||||
),
|
||||
];
|
||||
|
||||
// skip already in correct location
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Contract/KeepThisSomeInterface.php'), null];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return MoveInterfacesToContractNamespaceDirectoryRector::class;
|
||||
}
|
||||
|
||||
protected function getPhpVersion(): string
|
||||
{
|
||||
return PhpVersionFeature::BEFORE_TYPED_PROPERTIES;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface KeepThisSomeInterface
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
interface ControlFactory
|
||||
{
|
||||
public function create(): SomeControl;
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
interface FormFactory
|
||||
{
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class SomeControl extends Control
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class SomeForm extends Form
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): RandomInterface;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
/**
|
||||
* @var RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
/**
|
||||
* @var RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\ValueObject;
|
||||
|
||||
final class SameClassImplementEntity implements \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface
|
||||
{
|
||||
public function __construct(\Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface $random)
|
||||
{
|
||||
}
|
||||
public function returnAnother(): \Rector\Autodiscovery\Tests\Rector\FileNode\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -23,10 +23,8 @@ final class MoveServicesBySuffixToDirectoryRectorTest extends AbstractRectorTest
|
||||
if ($expectedAddedFileWithContent === null) {
|
||||
// no change - file should have the original location
|
||||
$this->assertFileWasNotChanged($this->originalTempFileInfo);
|
||||
}
|
||||
|
||||
if ($expectedAddedFileWithContent !== null) {
|
||||
$this->assertFilesWereAdded([$expectedAddedFileWithContent]);
|
||||
} else {
|
||||
$this->assertFileWithContentWasAdded($expectedAddedFileWithContent);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Expected\ValueObject;
|
||||
|
||||
|
||||
final class MeSearch
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\ValueObject;
|
||||
|
||||
final class MeSearch
|
||||
{
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\ValueObject;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\ObviousValueObjectInterface;
|
||||
|
||||
final class SomeName implements ObviousValueObjectInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\ObviousValueObjectInterface;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class MoveValueObjectsToValueObjectDirectoryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fixtureFileInfo, ?AddedFileWithContent $expectedAddedFileWithContent): void
|
||||
{
|
||||
$this->doTestFileInfo($fixtureFileInfo);
|
||||
|
||||
if ($expectedAddedFileWithContent !== null) {
|
||||
$this->assertFileWithContentWasAdded($expectedAddedFileWithContent);
|
||||
} else {
|
||||
$this->assertFileWasNotChanged($this->originalTempFileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$smartFileSystem = new SmartFileSystem();
|
||||
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Repository/PrimitiveValueObject.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/PrimitiveValueObject.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ValueObject/PrimitiveValueObject.php')
|
||||
),
|
||||
];
|
||||
|
||||
// type
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Command/SomeName.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/SomeName.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ValueObject/SomeName.php')
|
||||
),
|
||||
];
|
||||
|
||||
// suffix
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Command/MeSearch.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/MeSearch.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ValueObject/MeSearch.php')
|
||||
),
|
||||
];
|
||||
|
||||
// skip known service types
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Utils/SomeSuffixedTest.php.inc'), null];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
protected function getRectorsWithConfiguration(): array
|
||||
{
|
||||
return [
|
||||
MoveValueObjectsToValueObjectDirectoryRector::class => [
|
||||
MoveValueObjectsToValueObjectDirectoryRector::TYPES => [ObviousValueObjectInterface::class],
|
||||
MoveValueObjectsToValueObjectDirectoryRector::SUFFIXES => ['Search'],
|
||||
MoveValueObjectsToValueObjectDirectoryRector::ENABLE_VALUE_OBJECT_GUESSING => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\Command;
|
||||
|
||||
final class MeSearch
|
||||
{
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\Command;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\ObviousValueObjectInterface;
|
||||
|
||||
final class SomeName implements ObviousValueObjectInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source;
|
||||
|
||||
interface ObviousValueObjectInterface
|
||||
{
|
||||
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\Repository;
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\Repository;
|
||||
|
||||
class PrimitiveValueObject
|
||||
{
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileNode\MoveValueObjectsToValueObjectDirectoryRector\Source\Utils;
|
||||
|
||||
final class SomeSuffixedTest
|
||||
{
|
||||
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector;
|
||||
use Rector\Testing\PHPUnit\AbstractFileSystemRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MoveEntitiesToEntityDirectoryRectorTest extends AbstractFileSystemRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
string $expectedFileLocation,
|
||||
string $expectedFileContent
|
||||
): void {
|
||||
$this->doTestFileInfo($originalFileInfo);
|
||||
|
||||
$this->assertFileExists($expectedFileLocation);
|
||||
$this->assertFileEquals($expectedFileContent, $expectedFileLocation);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Controller/RandomEntity.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/RandomEntity.php',
|
||||
__DIR__ . '/Expected/ExpectedRandomEntity.php',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return MoveEntitiesToEntityDirectoryRector::class;
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
final class SameClassImplementEntity implements \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
public function __construct(\Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random)
|
||||
{
|
||||
}
|
||||
|
||||
public function returnAnother(): \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
public \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random;
|
||||
|
||||
public function create(): \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
public \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random;
|
||||
|
||||
public function create(): \Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\ValueObject\InputFilePathWithExpectedFile;
|
||||
use Rector\Testing\PHPUnit\AbstractFileSystemRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRectorTest extends AbstractFileSystemRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @requires PHP 7.4
|
||||
* @dataProvider provideData()
|
||||
* @param InputFilePathWithExpectedFile[] $InputFilePathWithExpectedFiles
|
||||
*/
|
||||
public function test(
|
||||
SmartFileInfo $originalFileInfo,
|
||||
string $expectedFileLocation,
|
||||
string $expectedFileContent,
|
||||
array $InputFilePathWithExpectedFiles = []
|
||||
): void {
|
||||
$this->doTestFileInfo($originalFileInfo, $InputFilePathWithExpectedFiles);
|
||||
|
||||
$this->assertFileExists($expectedFileLocation);
|
||||
$this->assertFileEquals($expectedFileContent, $expectedFileLocation);
|
||||
|
||||
$this->doTestExtraFileInfos($InputFilePathWithExpectedFiles);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$extraFiles = [
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/RandomInterfaceUseCase.php',
|
||||
$this->getFixtureTempDirectory() . '/Source/RandomInterfaceUseCase.php',
|
||||
__DIR__ . '/Expected/ExpectedRandomInterfaceUseCase.php'
|
||||
),
|
||||
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/Entity/SameClassImplementEntity.php',
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/SameClassImplementEntity.php',
|
||||
__DIR__ . '/Expected/Entity/ExpectedSameClassImplementEntity.php'
|
||||
),
|
||||
|
||||
new InputFilePathWithExpectedFile(
|
||||
__DIR__ . '/Source/Entity/RandomInterfaceUseCaseInTheSameNamespace.php',
|
||||
$this->getFixtureTempDirectory() . '/Source/Entity/RandomInterfaceUseCaseInTheSameNamespace.php',
|
||||
__DIR__ . '/Expected/Entity/RandomInterfaceUseCaseInTheSameNamespace.php'
|
||||
),
|
||||
];
|
||||
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Entity/RandomInterface.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Contract/RandomInterface.php',
|
||||
__DIR__ . '/Expected/ExpectedRandomInterface.php',
|
||||
// extra files
|
||||
$extraFiles,
|
||||
];
|
||||
|
||||
// skip nette control factory
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Control/ControlFactory.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Control/ControlFactory.php',
|
||||
__DIR__ . '/Source/Control/ControlFactory.php',
|
||||
];
|
||||
|
||||
// skip form control factory, even in docblock
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Control/FormFactory.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Control/FormFactory.php',
|
||||
__DIR__ . '/Source/Control/FormFactory.php',
|
||||
];
|
||||
|
||||
// skip already in correct location
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Contract/KeepThisSomeInterface.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Contract/KeepThisSomeInterface.php',
|
||||
// no change
|
||||
__DIR__ . '/Source/Contract/KeepThisSomeInterface.php',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return MoveInterfacesToContractNamespaceDirectoryRector::class;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface KeepThisSomeInterface
|
||||
{
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
interface ControlFactory
|
||||
{
|
||||
public function create(): SomeControl;
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class SomeControl extends Control
|
||||
{
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class SomeForm extends Form
|
||||
{
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): RandomInterface;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
public RandomInterface $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
final class SameClassImplementEntity implements RandomInterface
|
||||
{
|
||||
public function __construct(RandomInterface $random)
|
||||
{
|
||||
}
|
||||
|
||||
public function returnAnother(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
public RandomInterface $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector\ValueObject;
|
||||
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class InputFilePathWithExpectedFile
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $expectedContent;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $inputFilePath;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $expectedFilePath;
|
||||
|
||||
public function __construct(string $inputFilePath, string $expectedFilePath, string $expectedContent)
|
||||
{
|
||||
$this->expectedContent = $expectedContent;
|
||||
$this->inputFilePath = $inputFilePath;
|
||||
$this->expectedFilePath = $expectedFilePath;
|
||||
}
|
||||
|
||||
public function getExpectedContent(): string
|
||||
{
|
||||
return $this->expectedContent;
|
||||
}
|
||||
|
||||
public function getExpectedFilePath(): string
|
||||
{
|
||||
return $this->expectedFilePath;
|
||||
}
|
||||
|
||||
public function getInputFileInfo(): SmartFileInfo
|
||||
{
|
||||
return new SmartFileInfo($this->inputFilePath);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\ObviousValueObjectInterface;
|
||||
use Rector\Testing\PHPUnit\AbstractFileSystemRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MoveValueObjectsToValueObjectDirectoryRectorTest extends AbstractFileSystemRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $originalFileInfo, string $expectedFileLocation): void
|
||||
{
|
||||
$this->doTestFileInfo($originalFileInfo);
|
||||
$this->assertFileExists($expectedFileLocation);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Repository/PrimitiveValueObject.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/PrimitiveValueObject.php',
|
||||
];
|
||||
|
||||
// type
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Command/SomeName.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/SomeName.php',
|
||||
];
|
||||
|
||||
// suffix
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Command/MeSearch.php'),
|
||||
$this->getFixtureTempDirectory() . '/Source/ValueObject/MeSearch.php',
|
||||
];
|
||||
|
||||
// skip known service types
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Utils/SomeSuffixedTest.php.inc'),
|
||||
$this->getFixtureTempDirectory() . '/Source/Utils/SomeSuffixedTest.php.inc',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
protected function getRectorsWithConfiguration(): array
|
||||
{
|
||||
return [
|
||||
MoveValueObjectsToValueObjectDirectoryRector::class => [
|
||||
MoveValueObjectsToValueObjectDirectoryRector::TYPES => [ObviousValueObjectInterface::class],
|
||||
MoveValueObjectsToValueObjectDirectoryRector::SUFFIXES => ['Search'],
|
||||
MoveValueObjectsToValueObjectDirectoryRector::ENABLE_VALUE_OBJECT_GUESSING => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return MoveValueObjectsToValueObjectDirectoryRector::class;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\Command;
|
||||
|
||||
final class MeSearch
|
||||
{
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\Command;
|
||||
|
||||
use Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\ObviousValueObjectInterface;
|
||||
|
||||
final class SomeName implements ObviousValueObjectInterface
|
||||
{
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source;
|
||||
|
||||
interface ObviousValueObjectInterface
|
||||
{
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector\Source\Utils;
|
||||
|
||||
final class SomeSuffixedTest
|
||||
{
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ final class LivingCodeManipulator
|
||||
{
|
||||
/**
|
||||
* @param Node|int|string|null $expr
|
||||
* @return Expr[]
|
||||
* @return Expr[]|mixed[]
|
||||
*/
|
||||
public function keepLivingCodeFromExpr($expr): array
|
||||
{
|
||||
|
@ -24,7 +24,10 @@ use Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEvent
|
||||
|
||||
final class DuplicatedEventParams
|
||||
{
|
||||
private \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher;
|
||||
/**
|
||||
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
public function __construct(\Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
|
@ -24,7 +24,10 @@ use Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEvent
|
||||
|
||||
final class FileManager
|
||||
{
|
||||
private \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher;
|
||||
/**
|
||||
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
public function __construct(\Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NetteKdyby\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
@ -48,6 +49,11 @@ final class ReplaceMagicPropertyEventWithEventClassRectorTest extends AbstractRe
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPhpVersion(): string
|
||||
{
|
||||
return PhpVersionFeature::BEFORE_TYPED_PROPERTIES;
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ReplaceMagicPropertyEventWithEventClassRector::class;
|
||||
|
@ -12,7 +12,10 @@ final class DuplicatedEventParamsUploadEvent extends \Symfony\Contracts\EventDis
|
||||
* @var mixed
|
||||
*/
|
||||
private $userNameValue;
|
||||
private string $someUnderscore;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $someUnderscore;
|
||||
public function __construct($userOwnerId, $userNameValue, string $someUnderscore)
|
||||
{
|
||||
$this->userOwnerId = $userOwnerId;
|
||||
|
@ -4,7 +4,10 @@ namespace Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWit
|
||||
|
||||
final class FileManagerUploadEvent extends \Symfony\Contracts\EventDispatcher\Event
|
||||
{
|
||||
private \Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector\Source\SomeUser $user;
|
||||
/**
|
||||
* @var \Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector\Source\SomeUser
|
||||
*/
|
||||
private $user;
|
||||
public function __construct(\Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector\Source\SomeUser $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@ -68,7 +68,8 @@ CODE_SAMPLE
|
||||
}
|
||||
|
||||
$newPathName = $this->createPathName($oldPathname);
|
||||
$this->addMovedFile(new MovedFileWithContent($fileInfo, $newPathName));
|
||||
$movedFileWithContent = new MovedFileWithContent($fileInfo, $newPathName);
|
||||
$this->addMovedFile($movedFileWithContent);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Rector\PhpSpecToPHPUnit\Tests\Rector\FileNode\RenameSpecFileToTestFile
|
||||
|
||||
use Iterator;
|
||||
use Nette\Utils\Strings;
|
||||
use Rector\FileSystemRector\ValueObject\MovedFileWithContent;
|
||||
use Rector\FileSystemRector\Contract\MovedFileInterface;
|
||||
use Rector\PhpSpecToPHPUnit\Rector\FileNode\RenameSpecFileToTestFileRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
@ -22,7 +22,7 @@ final class RenameSpecFileToTestFileRectorTest extends AbstractRectorTestCase
|
||||
|
||||
// test file is moved
|
||||
$movedFile = $this->matchMovedFile();
|
||||
$this->assertInstanceOf(MovedFileWithContent::class, $movedFile);
|
||||
$this->assertInstanceOf(MovedFileInterface::class, $movedFile);
|
||||
|
||||
$this->assertTrue(Strings::endsWith($movedFile->getNewPathname(), 'Test.php'));
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the Nette Framework (https://nette.org)
|
||||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace NettePostfixedToUniqueAutoload\Utils;
|
||||
|
||||
/**
|
||||
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the Nette Framework (https://nette.org)
|
||||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace NettePostfixedToUniqueAutoload\Utils;
|
||||
|
||||
/**
|
||||
|
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of the Nette Framework (https://nette.org)
|
||||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace NettePostfixedToUniqueAutoload\Utils;
|
||||
|
||||
/**
|
||||
@ -35,13 +35,13 @@ class RegexpException extends \Exception
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of the Nette Framework (https://nette.org)
|
||||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace NettePostfixedToUniqueAutoload\Utils;
|
||||
|
||||
|
||||
|
@ -121,6 +121,7 @@ final class FileProcessor
|
||||
$this->makeSureFileIsParsed($smartFileInfo);
|
||||
|
||||
$parsedStmtsAndTokens = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);
|
||||
|
||||
return $this->formatPerservingPrinter->printParsedStmstAndTokensToString($parsedStmtsAndTokens);
|
||||
}
|
||||
|
||||
@ -132,10 +133,12 @@ final class FileProcessor
|
||||
$this->makeSureFileIsParsed($smartFileInfo);
|
||||
|
||||
$parsedStmtsAndTokens = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);
|
||||
|
||||
$this->currentFileInfoProvider->setCurrentStmts($parsedStmtsAndTokens->getNewStmts());
|
||||
|
||||
// run file node only if
|
||||
$fileNode = new FileNode($smartFileInfo, $parsedStmtsAndTokens->getNewStmts());
|
||||
$this->rectorNodeTraverser->traverseFileNode($fileNode);
|
||||
$result = $this->rectorNodeTraverser->traverseFileNode($fileNode);
|
||||
|
||||
$newStmts = $this->rectorNodeTraverser->traverse($parsedStmtsAndTokens->getNewStmts());
|
||||
|
||||
@ -186,7 +189,7 @@ final class FileProcessor
|
||||
}
|
||||
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
'File %s was not preparsed, so it cannot be printed.%sCheck "%s" method.',
|
||||
'File "%s" was not preparsed, so it cannot be printed.%sCheck "%s" method.',
|
||||
$smartFileInfo->getRealPath(),
|
||||
PHP_EOL,
|
||||
self::class . '::parseFileInfoToLocalCache()'
|
||||
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Application\FileSystem;
|
||||
|
||||
use Rector\FileSystemRector\Contract\AddedFileInterface;
|
||||
use Rector\FileSystemRector\Contract\MovedFileInterface;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\FileSystemRector\ValueObject\MovedFileWithNodes;
|
||||
@ -18,9 +17,9 @@ final class RemovedAndAddedFilesCollector
|
||||
private $removedFiles = [];
|
||||
|
||||
/**
|
||||
* @var AddedFileInterface[]
|
||||
* @var AddedFileWithContent[]
|
||||
*/
|
||||
private $addedFiles = [];
|
||||
private $addedFileWithContents = [];
|
||||
|
||||
/**
|
||||
* @var MovedFileInterface[]
|
||||
@ -79,17 +78,9 @@ final class RemovedAndAddedFilesCollector
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addAddedFile(AddedFileInterface $addedFile): void
|
||||
public function addAddedFile(AddedFileWithContent $addedFileWithContent): void
|
||||
{
|
||||
$this->addedFiles[] = $addedFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AddedFileInterface[]
|
||||
*/
|
||||
public function getAddedFiles(): array
|
||||
{
|
||||
return $this->addedFiles;
|
||||
$this->addedFileWithContents[] = $addedFileWithContent;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,9 +88,7 @@ final class RemovedAndAddedFilesCollector
|
||||
*/
|
||||
public function getAddedFilesWithContent(): array
|
||||
{
|
||||
return array_filter($this->addedFiles, function (AddedFileInterface $addedFile): bool {
|
||||
return $addedFile instanceof AddedFileWithContent;
|
||||
});
|
||||
return $this->addedFileWithContents;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,12 +103,12 @@ final class RemovedAndAddedFilesCollector
|
||||
|
||||
public function getAffectedFilesCount(): int
|
||||
{
|
||||
return count($this->addedFiles) + count($this->movedFiles) + count($this->removedFiles);
|
||||
return count($this->addedFileWithContents) + count($this->movedFiles) + count($this->removedFiles);
|
||||
}
|
||||
|
||||
public function getAddedFileCount(): int
|
||||
{
|
||||
return count($this->addedFiles);
|
||||
return count($this->addedFileWithContents);
|
||||
}
|
||||
|
||||
public function getRemovedFilesCount(): int
|
||||
@ -132,7 +121,7 @@ final class RemovedAndAddedFilesCollector
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->addedFiles = [];
|
||||
$this->addedFileWithContents = [];
|
||||
$this->removedFiles = [];
|
||||
$this->movedFiles = [];
|
||||
}
|
||||
|
@ -146,11 +146,6 @@ final class Option
|
||||
*/
|
||||
public const XDEBUG = 'xdebug';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const OPTION_SET = 'set';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -181,13 +181,6 @@ final class Application extends SymfonyApplication
|
||||
$this->getDefaultConfigPath()
|
||||
));
|
||||
|
||||
$inputDefinition->addOption(new InputOption(
|
||||
Option::OPTION_SET,
|
||||
's',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Finds config by shortcut name'
|
||||
));
|
||||
|
||||
$inputDefinition->addOption(new InputOption(
|
||||
Option::OPTION_DEBUG,
|
||||
null,
|
||||
|
@ -77,9 +77,13 @@ final class RectorNodeTraverser extends NodeTraverser
|
||||
$this->configureEnabledRectorsOnly();
|
||||
}
|
||||
|
||||
if (! $this->hasFileNodeRectorsEnabled()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// here we only traverse file node without children, to prevent duplicatd traversion
|
||||
foreach ($this->allPhpRectors as $allPhpRector) {
|
||||
$allPhpRector->enterNode($fileNode);
|
||||
foreach ($this->visitors as $rector) {
|
||||
$rector->enterNode($fileNode);
|
||||
}
|
||||
|
||||
return [];
|
||||
@ -156,6 +160,23 @@ final class RectorNodeTraverser extends NodeTraverser
|
||||
}
|
||||
}
|
||||
|
||||
private function hasFileNodeRectorsEnabled(): bool
|
||||
{
|
||||
foreach ($this->visitors as $visitor) {
|
||||
if (! $visitor instanceof PhpRectorInterface) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array(FileNode::class, $visitor->getNodeTypes(), true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
*/
|
||||
|
@ -7,7 +7,6 @@ namespace Rector\Core\Rector\AbstractRector;
|
||||
use PhpParser\Node;
|
||||
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\FileSystemRector\Contract\AddedFileInterface;
|
||||
use Rector\FileSystemRector\Contract\MovedFileInterface;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\FileSystemRector\ValueObject\MovedFileWithNodes;
|
||||
@ -70,8 +69,8 @@ trait RemovedAndAddedFilesTrait
|
||||
$this->removedAndAddedFilesCollector->removeFile($smartFileInfo);
|
||||
}
|
||||
|
||||
private function addFile(AddedFileInterface $addedFile): void
|
||||
private function addFile(AddedFileWithContent $addedFileWithContent): void
|
||||
{
|
||||
$this->removedAndAddedFilesCollector->addAddedFile($addedFile);
|
||||
$this->removedAndAddedFilesCollector->addAddedFile($addedFileWithContent);
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ final class StubLoader
|
||||
$robotLoader->addDirectory($stubDirectory);
|
||||
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/_rector_stubs');
|
||||
$robotLoader->register();
|
||||
$robotLoader->refresh();
|
||||
|
||||
$this->areStubsLoaded = true;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user