[Autodiscovery] Do not nest already correct name

This commit is contained in:
TomasVotruba 2020-06-10 12:05:27 +02:00
parent 630d17494b
commit aff10cf014
19 changed files with 152 additions and 133 deletions

View File

@ -312,3 +312,4 @@ parameters:
- '#Cognitive complexity for "Rector\\NetteKdyby\\NodeResolver\\ListeningMethodsCollector\:\:collectFromClassAndGetSubscribedEventClassMethod\(\)" is 11, keep it under 9#'
- '#Cognitive complexity for "Rector\\NetteKdyby\\ContributeEventClassResolver\:\:resolveGetterMethodByEventClassAndParam\(\)" is \d+, keep it under 9#'
- '#Parameter \#1 \$type of class PhpParser\\Node\\NullableType constructor expects PhpParser\\Node\\Identifier\|PhpParser\\Node\\Name\|string, PhpParser\\Node\\Identifier\|PhpParser\\Node\\Name\|PhpParser\\Node\\NullableType\|PhpParser\\Node\\UnionType given#'
- '#Parameter \#1 \$oldMethod of class Rector\\PHPOffice\\ValueObject\\ConditionalSetValue constructor expects string, bool\|int\|string given#'

View File

@ -99,10 +99,16 @@ PHP
preg_quote($groupName, '#'),
preg_quote($suffixPattern, '#')
);
if (Strings::match($smartFileInfo->getRealPath(), $expectedLocationFilePattern)) {
continue;
}
// file is already in the group
if (Strings::match($smartFileInfo->getPath(), '#' . $groupName . '$#')) {
continue;
}
$this->moveFileToGroupName($smartFileInfo, $nodes, $groupName);
return;
}

View File

@ -25,7 +25,7 @@ final class MoveInterfacesToContractNamespaceDirectoryRectorTest extends Abstrac
{
yield [
__DIR__ . '/Source/Entity/RandomInterface.php',
__DIR__ . '/Source/Fixture/Contract/RandomInterface.php',
$this->getFixtureTempDirectory() . '/Source/Contract/RandomInterface.php',
__DIR__ . '/Expected/ExpectedRandomInterface.php',
];
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector\Source\Command;

View File

@ -25,22 +25,29 @@ final class MoveServicesBySuffixToDirectoryRectorTest extends AbstractFileSystem
{
yield [
__DIR__ . '/Source/Entity/AppleRepository.php',
__DIR__ . '/Source/Fixture/Repository/AppleRepository.php',
$this->getFixtureTempDirectory() . '/Source/Repository/AppleRepository.php',
__DIR__ . '/Expected/Repository/ExpectedAppleRepository.php',
];
yield 'prefix_same_namespace' => [
__DIR__ . '/Source/Controller/BananaCommand.php',
__DIR__ . '/Source/Command/Fixture/BananaCommand.php',
$this->getFixtureTempDirectory() . '/Source/Command/BananaCommand.php',
__DIR__ . '/Expected/Command/ExpectedBananaCommand.php',
];
yield [
__DIR__ . '/Source/Mapper/CorrectMapper.php',
$this->getFixtureTempDirectory() . '/Source/Mapper/CorrectMapper.php',
// same content, no change
__DIR__ . '/Source/Mapper/CorrectMapper.php',
];
}
protected function getRectorsWithConfiguration(): array
{
return [
MoveServicesBySuffixToDirectoryRector::class => [
'$groupNamesBySuffix' => ['Repository', 'Command'],
'$groupNamesBySuffix' => ['Repository', 'Command', 'Mapper'],
],
];
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Autodiscovery\Tests\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector\SkipAlreadyMoved\Source\Mapper;
final class CorrectMapper
{
}

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class ClassMethodDeclaration
{
function __doSomething() {}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class ClassMethodDeclaration
{
function doSomething() {}
}
?>

View File

@ -1,57 +0,0 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class MyClass
{
function __doSomething() {}
function __construct(){}
function __destruct(){}
function __call($name, $value){}
static function __callStatic($name, $value){}
function __get($value){}
function __set($key, $value){}
function __isset($key){}
function __unset($key){}
function __sleep(){}
function __wakeup(){}
function __serialize(){}
function __unserialize(){}
function __toString(){}
function __invoke(){}
function __set_state(){}
function __clone(){}
function __debugInfo(){}
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class MyClass
{
function doSomething() {}
function __construct(){}
function __destruct(){}
function __call($name, $value){}
static function __callStatic($name, $value){}
function __get($value){}
function __set($key, $value){}
function __isset($key){}
function __unset($key){}
function __sleep(){}
function __wakeup(){}
function __serialize(){}
function __unserialize(){}
function __toString(){}
function __invoke(){}
function __set_state(){}
function __clone(){}
function __debugInfo(){}
}
?>

View File

@ -0,0 +1,29 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector\Fixture;
class SkipNativePhpMethods
{
function __construct(){}
function __destruct(){}
function __call($name, $value){}
static function __callStatic($name, $value){}
function __get($value){}
function __set($key, $value){}
function __isset($key){}
function __unset($key){}
function __sleep(){}
function __wakeup(){}
function __serialize(){}
function __unserialize(){}
function __toString()
{
return 'something to keep contract';
}
function __invoke(){}
function __set_state(){}
function __clone(){}
function __debugInfo(){}
}

View File

@ -13,10 +13,10 @@ final class FunctionToStaticMethodRectorTest extends AbstractFileSystemRectorTes
{
$this->doTestFile(__DIR__ . '/Source/static_functions.php');
$this->assertFileExists(__DIR__ . '/Fixture/StaticFunctions.php');
$this->assertFileExists($this->getFixtureTempDirectory() . '/Source/StaticFunctions.php');
$this->assertFileEquals(
__DIR__ . '/Source/ExpectedStaticFunctions.php',
__DIR__ . '/Fixture/StaticFunctions.php'
$this->getFixtureTempDirectory() . '/Source/StaticFunctions.php'
);
}

View File

@ -34,13 +34,13 @@ final class PSR4NamespaceMatcher implements PSR4AutoloadNamespaceMatcherInterfac
/** @var string[] $paths */
$paths = is_array($path) ? $path : [$path];
foreach ($paths as $path) {
$path = rtrim($path, '/');
if (! Strings::startsWith($smartFileInfo->getRelativeDirectoryPath(), $path)) {
foreach ($paths as $singlePath) {
$singlePath = rtrim($singlePath, '/');
if (! Strings::startsWith($smartFileInfo->getRelativeDirectoryPath(), $singlePath)) {
continue;
}
$expectedNamespace = $namespace . $this->resolveExtraNamespace($smartFileInfo, $path);
$expectedNamespace = $namespace . $this->resolveExtraNamespace($smartFileInfo, $singlePath);
return rtrim($expectedNamespace, '\\');
}

View File

@ -11,15 +11,20 @@ final class NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRectorTest extends
{
public function test(): void
{
$this->doTestFile(__DIR__ . '/Source/namespace_less_class.php.inc');
$this->doTestFile(__DIR__ . '/Fixture/namespace_less_class.php.inc');
$this->assertFileExists($this->getFixtureTempDirectory() . '/Fixture/namespace_less_class.php.inc');
$this->assertFileExists(__DIR__ . '/Fixture/namespace_less_class.php.inc');
$this->assertFileEquals(
__DIR__ . '/Source/ExpectedNamespaceLessClass.php',
__DIR__ . '/Fixture/namespace_less_class.php.inc'
$this->getFixtureTempDirectory() . '/Fixture/namespace_less_class.php.inc'
);
}
protected function provideConfig(): string
{
return __DIR__ . '/config/normalize_namespace_without_namespace_config.yaml';
}
protected function getRectorClass(): string
{
return NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector::class;

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector\Source;
use PhpParser\Node;
use Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface;
final class DummyPSR4AutoloadWithoutNamespaceMatcher implements PSR4AutoloadNamespaceMatcherInterface
{
public function getExpectedNamespace(Node $node): ?string
{
return 'Rector\PSR4\Tests\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector\Fixture';
}
}

View File

@ -0,0 +1,8 @@
parameters:
auto_import_names: true
services:
Rector\PSR4\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector: null
Rector\PSR4\Tests\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector\Source\DummyPSR4AutoloadWithoutNamespaceMatcher: null
Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface: '@Rector\PSR4\Tests\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector\Source\DummyPSR4AutoloadWithoutNamespaceMatcher'

View File

@ -43,10 +43,10 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/nette-exceptions.php',
[
__DIR__ . '/Fixture/ArgumentOutOfRangeException.php' => __DIR__ . '/Expected/ArgumentOutOfRangeException.php',
__DIR__ . '/Fixture/InvalidStateException.php' => __DIR__ . '/Expected/InvalidStateException.php',
__DIR__ . '/Fixture/RegexpException.php' => __DIR__ . '/Expected/RegexpException.php',
__DIR__ . '/Fixture/UnknownImageFileException.php' => __DIR__ . '/Expected/UnknownImageFileException.php',
$this->getFixtureTempDirectory() . '/Source/ArgumentOutOfRangeException.php' => __DIR__ . '/Expected/ArgumentOutOfRangeException.php',
$this->getFixtureTempDirectory() . '/Source/InvalidStateException.php' => __DIR__ . '/Expected/InvalidStateException.php',
$this->getFixtureTempDirectory() . '/Source/RegexpException.php' => __DIR__ . '/Expected/RegexpException.php',
$this->getFixtureTempDirectory() . '/Source/UnknownImageFileException.php' => __DIR__ . '/Expected/UnknownImageFileException.php',
],
true,
];
@ -54,8 +54,8 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/exceptions.php',
[
__DIR__ . '/Fixture/FirstException.php' => __DIR__ . '/Expected/FirstException.php',
__DIR__ . '/Fixture/SecondException.php' => __DIR__ . '/Expected/SecondException.php',
$this->getFixtureTempDirectory() . '/Source/FirstException.php' => __DIR__ . '/Expected/FirstException.php',
$this->getFixtureTempDirectory() . '/Source/SecondException.php' => __DIR__ . '/Expected/SecondException.php',
],
true,
];
@ -63,8 +63,8 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/exceptions-without-namespace.php',
[
__DIR__ . '/Fixture/JustOneExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustOneExceptionWithoutNamespace.php',
__DIR__ . '/Fixture/JustTwoExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustTwoExceptionWithoutNamespace.php',
$this->getFixtureTempDirectory() . '/Source/JustOneExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustOneExceptionWithoutNamespace.php',
$this->getFixtureTempDirectory() . '/Source/JustTwoExceptionWithoutNamespace.php' => __DIR__ . '/Expected/JustTwoExceptionWithoutNamespace.php',
],
true,
];
@ -72,8 +72,8 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/MissNamed.php',
[
__DIR__ . '/Fixture/Miss.php' => __DIR__ . '/Expected/Miss.php',
__DIR__ . '/Fixture/Named.php' => __DIR__ . '/Expected/Named.php',
$this->getFixtureTempDirectory() . '/Source/Miss.php' => __DIR__ . '/Expected/Miss.php',
$this->getFixtureTempDirectory() . '/Source/Named.php' => __DIR__ . '/Expected/Named.php',
],
true,
];
@ -81,9 +81,9 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/ClassLike.php',
[
__DIR__ . '/Fixture/MyTrait.php' => __DIR__ . '/Expected/MyTrait.php',
__DIR__ . '/Fixture/MyClass.php' => __DIR__ . '/Expected/MyClass.php',
__DIR__ . '/Fixture/MyInterface.php' => __DIR__ . '/Expected/MyInterface.php',
$this->getFixtureTempDirectory() . '/Source/MyTrait.php' => __DIR__ . '/Expected/MyTrait.php',
$this->getFixtureTempDirectory() . '/Source/MyClass.php' => __DIR__ . '/Expected/MyClass.php',
$this->getFixtureTempDirectory() . '/Source/MyInterface.php' => __DIR__ . '/Expected/MyInterface.php',
],
true,
];
@ -91,8 +91,8 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
yield [
__DIR__ . '/Source/SomeClass.php',
[
__DIR__ . '/Fixture/SomeClass.php' => __DIR__ . '/Expected/SomeClass.php',
__DIR__ . '/Fixture/SomeClass_Exception.php' => __DIR__ . '/Expected/SomeClass_Exception.php',
$this->getFixtureTempDirectory() . '/Source/SomeClass.php' => __DIR__ . '/Expected/SomeClass.php',
$this->getFixtureTempDirectory() . '/Source/SomeClass_Exception.php' => __DIR__ . '/Expected/SomeClass_Exception.php',
],
false,
];

View File

@ -5,8 +5,9 @@ declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\Namespace_\NormalizeNamespaceByPSR4ComposerAutoloadRector\Source;
use PhpParser\Node;
use Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface;
final class DummyPSR4AutoloadNamespaceMatcher implements \Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface
final class DummyPSR4AutoloadNamespaceMatcher implements PSR4AutoloadNamespaceMatcherInterface
{
public function getExpectedNamespace(Node $node): ?string
{

View File

@ -8,7 +8,6 @@ use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
use Rector\FileSystemRector\FileSystemFileProcessor;
@ -40,32 +39,10 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes
$this->removedAndAddedFilesProcessor = self::$container->get(RemovedAndAddedFilesProcessor::class);
}
protected function tearDown(): void
{
$testDirectory = $this->resolveTestDirectory();
if (file_exists($testDirectory . '/Source/Fixture')) {
FileSystem::delete($testDirectory . '/Source/Fixture');
}
if (file_exists($testDirectory . '/Fixture')) {
FileSystem::delete($testDirectory . '/Fixture');
}
}
protected function doTestFileWithoutAutoload(string $file): string
{
$temporaryFilePath = $this->createTemporaryFilePathFromFilePath($file);
if ($temporaryFilePath === $file) {
$message = sprintf(
'File %s is about to be copied to itself. Move it out of "/Fixture" directory to "/Source"',
$file
);
throw new ShouldNotHappenException($message);
}
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($temporaryFilePath));
$this->removedAndAddedFilesProcessor->run();
@ -89,6 +66,11 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes
return FileSystemRectorInterface::class;
}
protected function getFixtureTempDirectory(): string
{
return sys_get_temp_dir() . '/rector_temp_tests';
}
private function createContainerWithProvidedRector(): void
{
$configFileTempPath = $this->createConfigFileTempPath();
@ -113,38 +95,27 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes
$this->bootKernelWithConfigs(RectorKernel::class, $configFilePaths);
}
private function resolveTestDirectory(): string
{
$testReflectionClass = new ReflectionClass(static::class);
return (string) dirname((string) $testReflectionClass->getFileName());
}
private function createTemporaryFilePathFromFilePath(string $file): string
{
$fileInfo = new SmartFileInfo($file);
return $this->createTemporaryFilePath($fileInfo, $file);
// 1. get test case directory
$reflectionClass = new ReflectionClass(static::class);
$testCaseDirectory = dirname((string) $reflectionClass->getFileName());
// 2. relative test case file path
$relativeFilePath = $fileInfo->getRelativeFilePathFromDirectory($testCaseDirectory);
$temporaryFilePath = $this->getFixtureTempDirectory() . '/' . $relativeFilePath;
FileSystem::copy($file, $temporaryFilePath, true);
return $temporaryFilePath;
}
private function createConfigFileTempPath(): string
{
$thisClass = Strings::after(Strings::webalize(static::class), '-', -1);
return sprintf(sys_get_temp_dir() . '/rector_temp_tests/' . $thisClass . 'file_system_rector.yaml');
}
private function createTemporaryFilePath(SmartFileInfo $fileInfo, string $file): string
{
$temporaryFilePath = sprintf(
'%s%sFixture%s%s',
dirname($fileInfo->getPath()),
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
$fileInfo->getBasename()
);
FileSystem::copy($file, $temporaryFilePath, true);
return $temporaryFilePath;
return sprintf($this->getFixtureTempDirectory() . '/' . $thisClass . 'file_system_rector.yaml');
}
}