From cd5d2873f6a2ca98297bf5b48c09b473f646e12e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 25 Jan 2024 00:52:58 +0100 Subject: [PATCH] remove 3rd party support, as will be handled in rector core (#46) --- src/FileSystem/TemplateFileSystem.php | 19 ------- src/Generator/FileGenerator.php | 18 ------- src/ValueObject/RectorRecipe.php | 50 ----------------- .../src/Rector/MethodCall/WhateverRector.php | 53 ------------------- .../WhateverRector/Fixture/some_class.php.inc | 17 ------ .../WhateverRector/WhateverRectorTest.php.inc | 26 --------- .../WhateverRector/config/configured_rule.php | 9 ---- tests/RectorGenerator/RectorGeneratorTest.php | 25 +-------- .../Source/StaticRectorRecipeFactory.php | 12 +---- .../Source/config/some_set.php | 9 ---- 10 files changed, 3 insertions(+), 235 deletions(-) delete mode 100644 tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/src/Rector/MethodCall/WhateverRector.php delete mode 100644 tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/Fixture/some_class.php.inc delete mode 100644 tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/WhateverRectorTest.php.inc delete mode 100644 tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/config/configured_rule.php delete mode 100644 tests/RectorGenerator/Source/config/some_set.php diff --git a/src/FileSystem/TemplateFileSystem.php b/src/FileSystem/TemplateFileSystem.php index b294f45..549ffd4 100644 --- a/src/FileSystem/TemplateFileSystem.php +++ b/src/FileSystem/TemplateFileSystem.php @@ -19,18 +19,6 @@ final class TemplateFileSystem */ private const FIXTURE_SHORT_REGEX = '#/Fixture/#'; - /** - * @var string - * @see https://regex101.com/r/HBcfXd/1 - */ - private const PACKAGE_RULES_PATH_REGEX = '#(rules)\/__Package__#i'; - - /** - * @var string - * @see https://regex101.com/r/HBcfXd/1 - */ - private const PACKAGE_RULES_TESTS_PATH_REGEX = '#(rules-tests)\/__Package__#i'; - public function __construct( private readonly TemplateFactory $templateFactory, private readonly Filesystem $filesystem, @@ -49,13 +37,6 @@ public function resolveDestination( $destination = $this->resolveRelativeFilepath($filePath); $destination = $this->changeRootPathForRootPackage($rectorRecipe, $destination); - // normalize core package - if (! $rectorRecipe->isRectorRepository()) { - // special keyword for 3rd party Rectors, not for core Github contribution - $destination = Strings::replace($destination, self::PACKAGE_RULES_PATH_REGEX, 'utils/rector/src'); - $destination = Strings::replace($destination, self::PACKAGE_RULES_TESTS_PATH_REGEX, 'utils/rector/tests'); - } - $destination = $this->templateFactory->create($destination, $templateVariables); // remove ".inc" protection from PHPUnit if not a test case diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php index 78ff120..4415dc7 100644 --- a/src/Generator/FileGenerator.php +++ b/src/Generator/FileGenerator.php @@ -13,18 +13,6 @@ final class FileGenerator { - /** - * @var string - * @see https://regex101.com/r/RVbPEX/1 - */ - private const RECTOR_UTILS_REGEX = '#Rector\\\\Utils#'; - - /** - * @var string - * @see https://regex101.com/r/RVbPEX/1 - */ - private const RECTOR_UTILS_TESTS_REGEX = '#Rector\\\\Tests\\\\Utils#'; - public function __construct( private readonly Filesystem $filesystem, private readonly TemplateFactory $templateFactory, @@ -77,12 +65,6 @@ private function generateFileInfoWithTemplateVariables( $content = $this->templateFactory->create($templateFileContents, $templateVariables); - // replace "Rector\Utils\" with "Utils\Rector\" for 3rd party packages - if (! $rectorRecipe->isRectorRepository()) { - $content = Strings::replace($content, self::RECTOR_UTILS_REGEX, 'Utils\Rector'); - $content = Strings::replace($content, self::RECTOR_UTILS_TESTS_REGEX, 'Utils\Rector\Tests'); - } - // correct tests PSR-4 namespace for core rector packages if (in_array($rectorRecipe->getPackage(), Packages::RECTOR_CORE, true)) { $content = Strings::replace( diff --git a/src/ValueObject/RectorRecipe.php b/src/ValueObject/RectorRecipe.php index 663c408..7ec61c0 100644 --- a/src/ValueObject/RectorRecipe.php +++ b/src/ValueObject/RectorRecipe.php @@ -4,7 +4,6 @@ namespace Rector\RectorGenerator\ValueObject; -use Nette\Utils\Json; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; @@ -25,8 +24,6 @@ final class RectorRecipe private ?string $codeAfter = null; - private bool $isRectorRepository; - private ?string $category = null; /** @@ -50,8 +47,6 @@ public function __construct( string $codeBefore, string $codeAfter ) { - $this->isRectorRepository = $this->detectRectorRepository(); - $this->setPackage($package); $this->setName($name); $this->setNodeTypes($nodeTypes); @@ -99,26 +94,11 @@ public function getCodeAfter(): string return $this->codeAfter; } - public function isRectorRepository(): bool - { - return $this->isRectorRepository; - } - public function getCategory(): string { return $this->category; } - /** - * For testing purposes - * - * @api - */ - public function setIsRectorRepository(bool $isRectorRepository): void - { - $this->isRectorRepository = $isRectorRepository; - } - /** * For tests * @@ -204,34 +184,4 @@ private function normalizeCode(string $code): string return trim($code); } - - private function detectRectorRepository(): bool - { - $possibleComposerJsonFilePaths = [ - __DIR__ . '/../../../../../composer.json', - __DIR__ . '/../../composer.json', - ]; - - foreach ($possibleComposerJsonFilePaths as $possibleComposerJsonFilePath) { - if (! file_exists($possibleComposerJsonFilePath)) { - continue; - } - - $composerJsonContent = file_get_contents($possibleComposerJsonFilePath); - if ($composerJsonContent === false) { - continue; - } - - $composerJson = Json::decode($composerJsonContent, Json::FORCE_ARRAY); - if (! isset($composerJson['name'])) { - continue; - } - - if (\str_starts_with((string) $composerJson['name'], 'rector/')) { - return true; - } - } - - return false; - } } diff --git a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/src/Rector/MethodCall/WhateverRector.php b/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/src/Rector/MethodCall/WhateverRector.php deleted file mode 100644 index c744ec3..0000000 --- a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/src/Rector/MethodCall/WhateverRector.php +++ /dev/null @@ -1,53 +0,0 @@ -arg(...) to $service->call(...)', [ - new CodeSample( - <<<'CODE_SAMPLE' -$result = []; -echo 'code before'; -CODE_SAMPLE - - , - <<<'CODE_SAMPLE' -$result = []; -echo 'code after'; -CODE_SAMPLE - - ) - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [\PhpParser\Node\Expr\MethodCall::class]; - } - - /** - * @param \PhpParser\Node\Expr\MethodCall $node - */ - public function refactor(Node $node): ?Node - { - // change the node - - return $node; - } -} diff --git a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/Fixture/some_class.php.inc b/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/Fixture/some_class.php.inc deleted file mode 100644 index be9795b..0000000 --- a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - ------ - diff --git a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/WhateverRectorTest.php.inc b/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/WhateverRectorTest.php.inc deleted file mode 100644 index 1022f45..0000000 --- a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/WhateverRectorTest.php.inc +++ /dev/null @@ -1,26 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): \Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/config/configured_rule.php b/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/config/configured_rule.php deleted file mode 100644 index 13e2031..0000000 --- a/tests/RectorGenerator/Fixture/expected_3rd_party/utils/rector/tests/Rector/MethodCall/WhateverRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -rule(\Utils\Rector\Rector\MethodCall\WhateverRector::class); -}; diff --git a/tests/RectorGenerator/RectorGeneratorTest.php b/tests/RectorGenerator/RectorGeneratorTest.php index a4b2a22..4c57655 100644 --- a/tests/RectorGenerator/RectorGeneratorTest.php +++ b/tests/RectorGenerator/RectorGeneratorTest.php @@ -7,7 +7,6 @@ use Nette\Utils\FileSystem; use Rector\RectorGenerator\Generator\RectorGenerator; use Rector\RectorGenerator\Tests\RectorGenerator\Source\StaticRectorRecipeFactory; -use Rector\RectorGenerator\ValueObject\RectorRecipe; use Rector\Testing\PHPUnit\AbstractLazyTestCase; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; @@ -37,7 +36,7 @@ protected function tearDown(): void public function test(): void { - $rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', true); + $rectorRecipe = StaticRectorRecipeFactory::createRectorRecipe(); $this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY); $fileInfos = $this->findFileInfos(__DIR__ . '/Fixture/expected/'); @@ -52,28 +51,6 @@ public function test(): void } } - public function test3rdParty(): void - { - $rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', false); - $this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY); - - $fileInfos = $this->findFileInfos(__DIR__ . '/Fixture/expected_3rd_party/'); - - foreach ($fileInfos as $fileInfo) { - $this->assertFileExists(self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname()); - - $this->assertFileEquals( - $fileInfo->getRealPath(), - self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname() - ); - } - } - - private function createConfiguration(string $setFilePath, bool $isRectorRepository): RectorRecipe - { - return StaticRectorRecipeFactory::createRectorRecipe($setFilePath, $isRectorRepository); - } - /** * @return SplFileInfo[] */ diff --git a/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php b/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php index d8bad51..7c4da6f 100644 --- a/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php +++ b/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php @@ -14,13 +14,8 @@ */ final class StaticRectorRecipeFactory { - public static function createRectorRecipe(string $setFilePath, bool $isRectorRepository): RectorRecipe + public static function createRectorRecipe(): RectorRecipe { - if (! file_exists($setFilePath)) { - $message = sprintf('Set file path "%s" was not found', $setFilePath); - throw new ShouldNotHappenException($message); - } - $rectorRecipe = new RectorRecipe( 'Utils', 'WhateverRector', @@ -40,10 +35,7 @@ public static function createRectorRecipe(string $setFilePath, bool $isRectorRep CODE_SAMPLE ); - $rectorRecipe->setIsRectorRepository($isRectorRepository); - if ($isRectorRepository) { - $rectorRecipe->setPackage('ModeratePackage'); - } + $rectorRecipe->setPackage('ModeratePackage'); return $rectorRecipe; } diff --git a/tests/RectorGenerator/Source/config/some_set.php b/tests/RectorGenerator/Source/config/some_set.php deleted file mode 100644 index bc8a4b4..0000000 --- a/tests/RectorGenerator/Source/config/some_set.php +++ /dev/null @@ -1,9 +0,0 @@ -