From 801cc1b62472ddc55937e6a967076f0a5e2388cd Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 Jan 2024 22:29:22 +0100 Subject: [PATCH 1/4] tidy up --- composer.json | 18 ++++++------------ src/Provider/RectorRecipeProvider.php | 4 ---- src/TemplateVariablesFactory.php | 1 - src/ValueObject/Option.php | 5 ----- src/ValueObject/Placeholder.php | 5 ----- templates/rector-recipe.php | 5 ----- 6 files changed, 6 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index f854eb9..fce8bdd 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,9 @@ "homepage": "https://getrector.com", "license": "MIT", "require": { - "php": ">=8.1", + "php": ">=8.2", "nette/utils": "^3.2", - "nikic/php-parser": "^4.17", + "nikic/php-parser": "^4.18", "symfony/console": "^6.3", "symfony/finder": "^6.3", "webmozart/assert": "^1.11", @@ -16,22 +16,16 @@ }, "require-dev": { "rector/rector-src": "dev-main", - "php-parallel-lint/php-parallel-lint": "^1.3", - "phpunit/phpunit": "^10.3", - "symplify/easy-coding-standard": "^12.0", + "phpunit/phpunit": "^10.5", + "symplify/easy-coding-standard": "^12.1", "symplify/phpstan-extensions": "^11.0", "symplify/phpstan-rules": "^12.0", "phpstan/extension-installer": "^1.3", "symplify/vendor-patches": "^11.0", "phpstan/phpstan-webmozart-assert": "^1.2", "tracy/tracy": "^2.10", - "symplify/easy-testing": "^11.1", - "tomasvotruba/class-leak": "^0.1.3", - "tomasvotruba/unused-public": "0.3.2.72", - "tomasvotruba/type-coverage": "^0.2.1", - "rector/rector": "*", - "phpstan/phpstan": "^1.10", - "tomasvotruba/cognitive-complexity": "^0.1.1" + "tomasvotruba/class-leak": "^0.2", + "phpstan/phpstan": "^1.10.55" }, "autoload": { "psr-4": { diff --git a/src/Provider/RectorRecipeProvider.php b/src/Provider/RectorRecipeProvider.php index fd32f29..35945a3 100644 --- a/src/Provider/RectorRecipeProvider.php +++ b/src/Provider/RectorRecipeProvider.php @@ -38,10 +38,6 @@ public function __construct(array $rectorRecipeConfiguration = []) $rectorRecipe->setConfiguration($rectorRecipeConfiguration[Option::CONFIGURATION]); } - if (isset($rectorRecipeConfiguration[Option::RESOURCES])) { - $rectorRecipe->setResources($rectorRecipeConfiguration[Option::RESOURCES]); - } - if (isset($rectorRecipeConfiguration[Option::SET_FILE_PATH])) { $rectorRecipe->setSetFilePath($rectorRecipeConfiguration[Option::SET_FILE_PATH]); } diff --git a/src/TemplateVariablesFactory.php b/src/TemplateVariablesFactory.php index 7febb92..a9b5fa4 100644 --- a/src/TemplateVariablesFactory.php +++ b/src/TemplateVariablesFactory.php @@ -38,7 +38,6 @@ public function createFromRectorRecipe(RectorRecipe $rectorRecipe): array Placeholder::CODE_BEFORE_EXAMPLE => $this->createCodeForDefinition($rectorRecipe->getCodeBefore()), Placeholder::CODE_AFTER => trim($rectorRecipe->getCodeAfter()) . PHP_EOL, Placeholder::CODE_AFTER_EXAMPLE => $this->createCodeForDefinition($rectorRecipe->getCodeAfter()), - Placeholder::RESOURCES => $this->createSourceDocBlock($rectorRecipe->getResources()), ]; if ($rectorRecipe->getConfiguration() !== []) { diff --git a/src/ValueObject/Option.php b/src/ValueObject/Option.php index 04b780f..8c6e1ca 100644 --- a/src/ValueObject/Option.php +++ b/src/ValueObject/Option.php @@ -44,11 +44,6 @@ final class Option */ public const CONFIGURATION = 'configuration'; - /** - * @var string - */ - public const RESOURCES = 'resources'; - /** * @var string */ diff --git a/src/ValueObject/Placeholder.php b/src/ValueObject/Placeholder.php index c8c0de6..e7d8589 100644 --- a/src/ValueObject/Placeholder.php +++ b/src/ValueObject/Placeholder.php @@ -45,9 +45,4 @@ final class Placeholder * @var string */ public const CODE_AFTER_EXAMPLE = '__CodeAfterExample__'; - - /** - * @var string - */ - public const RESOURCES = '__Resources__'; } diff --git a/templates/rector-recipe.php b/templates/rector-recipe.php index 0496864..1fb3ef4 100644 --- a/templates/rector-recipe.php +++ b/templates/rector-recipe.php @@ -51,11 +51,6 @@ public function run() // [OPTIONAL - UNCOMMENT TO USE] - // links to useful websites, that explain why the change is needed - // Option::RESOURCES => [ - // 'https://github.com/symfony/symfony/blob/6.1/UPGRADE-6.0.md' - // ], - // is the rule configurable? // Option::CONFIGURATION => [ // 'privatePropertyName' => [ From fbf6e766175a213630e7f3a3d4520c6777e2a63d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 Jan 2024 22:30:55 +0100 Subject: [PATCH 2/4] tidy up --- ecs.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/ecs.php b/ecs.php index fd95ad8..6faba1b 100644 --- a/ecs.php +++ b/ecs.php @@ -3,19 +3,14 @@ declare(strict_types=1); use Symplify\EasyCodingStandard\Config\ECSConfig; -use Symplify\EasyCodingStandard\ValueObject\Set\SetList; -return static function (ECSConfig $ecsConfig): void { - $ecsConfig->sets([SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE, SetList::SYMPLIFY]); - - $ecsConfig->paths([ +return ECSConfig::configure() + ->withPreparedSets(psr12: true, common: true, strict: true, symplify: true) + ->withPaths([ __DIR__ . '/src', __DIR__ . '/config', __DIR__ . '/tests', - __DIR__ . '/ecs.php', - __DIR__ . '/rector.php', __DIR__ . '/templates/rector-recipe.php', - ]); - - $ecsConfig->skip([__DIR__ . '/tests/RectorGenerator/Fixture', __DIR__ . '/tests/RectorGenerator/Source']); -}; + ]) + ->withRootFiles() + ->withSkip([__DIR__ . '/tests/RectorGenerator/Fixture', __DIR__ . '/tests/RectorGenerator/Source']); From f9645db448de1d775cf969850799bb42ddc20989 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 Jan 2024 22:31:20 +0100 Subject: [PATCH 3/4] remove assert trait, make it easy --- tests/PHPUnit/DirectoryAssertableTrait.php | 116 ------------------ tests/RectorGenerator/RectorGeneratorTest.php | 2 - 2 files changed, 118 deletions(-) delete mode 100644 tests/PHPUnit/DirectoryAssertableTrait.php diff --git a/tests/PHPUnit/DirectoryAssertableTrait.php b/tests/PHPUnit/DirectoryAssertableTrait.php deleted file mode 100644 index 7186613..0000000 --- a/tests/PHPUnit/DirectoryAssertableTrait.php +++ /dev/null @@ -1,116 +0,0 @@ -findFileInfosInDirectory($expectedDirectory); - $outputFileInfos = $this->findFileInfosInDirectory($outputDirectory); - - $fileInfosByRelativeFilePath = $this->groupFileInfosByRelativeFilePath( - $expectedFileInfos, - $expectedDirectory, - $outputFileInfos, - $outputDirectory - ); - - foreach ($fileInfosByRelativeFilePath as $relativeFilePath => $expectedAndOutputFileInfoPair) { - // output file exists - $this->assertFileExists($outputDirectory . '/' . $relativeFilePath); - - /** @var ExpectedAndOutputFileInfoPair $expectedAndOutputFileInfoPair */ - if (! $expectedAndOutputFileInfoPair->doesOutputFileExist()) { - continue; - } - - // they have the same content - $this->assertSame( - $expectedAndOutputFileInfoPair->getExpectedFileContent(), - $expectedAndOutputFileInfoPair->getOutputFileContent(), - $relativeFilePath - ); - } - } - - /** - * @return SmartFileInfo[] - */ - private function findFileInfosInDirectory(string $directory): array - { - $firstDirectoryFinder = new Finder(); - $firstDirectoryFinder->files() - ->in($directory); - - $finderSanitizer = new FinderSanitizer(); - return $finderSanitizer->sanitize($firstDirectoryFinder); - } - - /** - * @param SmartFileInfo[] $expectedFileInfos - * @param SmartFileInfo[] $outputFileInfos - * @return array - */ - private function groupFileInfosByRelativeFilePath( - array $expectedFileInfos, - string $expectedDirectory, - array $outputFileInfos, - string $outputDirectory - ): array { - $fileInfosByRelativeFilePath = []; - - foreach ($expectedFileInfos as $expectedFileInfo) { - $relativeFilePath = $expectedFileInfo->getRelativeFilePathFromDirectory($expectedDirectory); - - // match output file info - $outputFileInfo = $this->resolveFileInfoByRelativeFilePath( - $outputFileInfos, - $outputDirectory, - $relativeFilePath - ); - - $fileInfosByRelativeFilePath[$relativeFilePath] = new ExpectedAndOutputFileInfoPair( - $expectedFileInfo, - $outputFileInfo - ); - } - - return $fileInfosByRelativeFilePath; - } - - /** - * @param SmartFileInfo[] $fileInfos - */ - private function resolveFileInfoByRelativeFilePath( - array $fileInfos, - string $directory, - string $desiredRelativeFilePath - ): ?SmartFileInfo { - foreach ($fileInfos as $fileInfo) { - $relativeFilePath = $fileInfo->getRelativeFilePathFromDirectory($directory); - if ($desiredRelativeFilePath !== $relativeFilePath) { - continue; - } - - return $fileInfo; - } - - return null; - } -} diff --git a/tests/RectorGenerator/RectorGeneratorTest.php b/tests/RectorGenerator/RectorGeneratorTest.php index 232edd1..821cabd 100644 --- a/tests/RectorGenerator/RectorGeneratorTest.php +++ b/tests/RectorGenerator/RectorGeneratorTest.php @@ -12,8 +12,6 @@ final class RectorGeneratorTest extends AbstractLazyTestCase { - use \Rector\RectorGenerator\Tests\PHPUnit\DirectoryAssertableTrait; - /** * @var string */ From d2c22f4670c0213306e6582a761db1bb217ccabd Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 Jan 2024 22:39:18 +0100 Subject: [PATCH 4/4] cleanup optional parameters, to make one easy path --- .github/workflows/code_analysis.yaml | 3 +- .github/workflows/php_linter.yaml | 29 ----- .github/workflows/tests.yaml | 10 +- composer.json | 29 ++--- src/Command/GenerateCommand.php | 47 +++----- src/Command/InitRecipeCommand.php | 27 +++-- src/Enum/Packages.php | 2 +- src/FileSystem/ConfigFilesystem.php | 72 ------------ src/FileSystem/PathHelper.php | 6 +- src/FileSystem/TemplateFileSystem.php | 9 -- src/Finder/TemplateFinder.php | 49 ++------ src/Generator/RectorGenerator.php | 19 +--- src/Guard/OverrideGuard.php | 65 ----------- src/NodeFactory/ConfigurationNodeFactory.php | 33 ------ .../ConfigureClassMethodFactory.php | 53 --------- src/Provider/RectorRecipeProvider.php | 13 +-- src/TemplateInitializer.php | 35 ------ src/TemplateVariablesFactory.php | 105 ------------------ src/ValueObject/NamePattern.php | 13 --- src/ValueObject/Option.php | 10 -- src/ValueObject/RectorGeneratorConfig.php | 16 --- src/ValueObject/RectorRecipe.php | 65 +---------- templates/rector-recipe.php | 12 -- .../config/__Configuredconfigured_rule.php | 11 -- .../__Category__/__Configured__Name__.php | 51 --------- .../Rector/__Category__/__Name__.php | 1 - tests/Config/ConfigFilesystemTest.php | 51 --------- tests/Config/Fixture/some_set.php.inc | 24 ---- .../WhateverRector/config/configured_rule.php | 4 +- .../Rector/MethodCall/WhateverRector.php | 27 +---- .../src/Rector/MethodCall/WhateverRector.php | 27 +---- .../WhateverRector/config/configured_rule.php | 4 +- tests/RectorGenerator/RectorGeneratorTest.php | 38 ++++++- .../Source/StaticRectorRecipeFactory.php | 8 -- tests/Source/Set/DummySetList.php | 16 --- tests/project/rules/symfony/some.php | 5 - 36 files changed, 123 insertions(+), 866 deletions(-) delete mode 100644 .github/workflows/php_linter.yaml delete mode 100644 src/FileSystem/ConfigFilesystem.php delete mode 100644 src/Guard/OverrideGuard.php delete mode 100644 src/NodeFactory/ConfigurationNodeFactory.php delete mode 100644 src/NodeFactory/ConfigureClassMethodFactory.php delete mode 100644 src/TemplateInitializer.php delete mode 100644 src/ValueObject/NamePattern.php delete mode 100644 src/ValueObject/RectorGeneratorConfig.php delete mode 100644 templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/__Configuredconfigured_rule.php delete mode 100644 templates/rules/__Package__/Rector/__Category__/__Configured__Name__.php delete mode 100644 tests/Config/ConfigFilesystemTest.php delete mode 100644 tests/Config/Fixture/some_set.php.inc delete mode 100644 tests/Source/Set/DummySetList.php delete mode 100644 tests/project/rules/symfony/some.php diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index c0da9ae..d4d893c 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -22,7 +22,6 @@ jobs: name: ${{ matrix.actions.name }} runs-on: ubuntu-latest - timeout-minutes: 10 steps: - uses: actions/checkout@v4 @@ -30,7 +29,7 @@ jobs: - uses: shivammathur/setup-php@v2 with: - php-version: 8.1 + php-version: 8.2 coverage: none - uses: "ramsey/composer-install@v2" diff --git a/.github/workflows/php_linter.yaml b/.github/workflows/php_linter.yaml deleted file mode 100644 index 0e86f86..0000000 --- a/.github/workflows/php_linter.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: PHP Linter - -on: - pull_request: null - push: - branches: - - main - -env: - # see https://github.com/composer/composer/issues/9368#issuecomment-718112361 - COMPOSER_ROOT_VERSION: "dev-main" - -jobs: - php_linter: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - # see https://github.com/shivammathur/setup-php - - - uses: shivammathur/setup-php@v2 - with: - php-version: '8.1' - coverage: none - - - uses: "ramsey/composer-install@v1" - - - run: vendor/bin/parallel-lint src tests diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 300ea11..cc01980 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -6,22 +6,18 @@ on: branches: - main -env: - # see https://github.com/composer/composer/issues/9368#issuecomment-718112361 - COMPOSER_ROOT_VERSION: "dev-main" - jobs: tests: runs-on: ubuntu-latest - name: PHP 8.1 tests steps: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: - php-version: 8.1 - coverage: none # disable xdebug, pcov + php-version: 8.2 + coverage: none - uses: "ramsey/composer-install@v1" + - run: vendor/bin/phpunit diff --git a/composer.json b/composer.json index fce8bdd..a30408e 100644 --- a/composer.json +++ b/composer.json @@ -8,23 +8,23 @@ "php": ">=8.2", "nette/utils": "^3.2", "nikic/php-parser": "^4.18", - "symfony/console": "^6.3", - "symfony/finder": "^6.3", + "symfony/console": "^6.4", + "symfony/finder": "^6.4", "webmozart/assert": "^1.11", - "symfony/filesystem": "^6.3", - "illuminate/container": "^10.20" + "symfony/filesystem": "^6.4", + "illuminate/container": "^10.42" }, "require-dev": { - "rector/rector-src": "dev-main", + "rector/rector": "dev-main", "phpunit/phpunit": "^10.5", "symplify/easy-coding-standard": "^12.1", - "symplify/phpstan-extensions": "^11.0", - "symplify/phpstan-rules": "^12.0", + "symplify/phpstan-extensions": "^11.4", + "symplify/phpstan-rules": "^12.4", "phpstan/extension-installer": "^1.3", - "symplify/vendor-patches": "^11.0", + "symplify/vendor-patches": "^11.3", "phpstan/phpstan-webmozart-assert": "^1.2", - "tracy/tracy": "^2.10", - "tomasvotruba/class-leak": "^0.2", + "tracy/tracy": "^2.10.5", + "tomasvotruba/class-leak": "^0.2.6", "phpstan/phpstan": "^1.10.55" }, "autoload": { @@ -48,6 +48,11 @@ "includes": [ "config/config.php" ] + }, + "patches": { + "package-name": [ + "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/illuminate-container-container-php.patch" + ] } }, "config": { @@ -56,7 +61,5 @@ "rector/extension-installer": true, "phpstan/extension-installer": true } - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/src/Command/GenerateCommand.php b/src/Command/GenerateCommand.php index 056b244..806c50d 100644 --- a/src/Command/GenerateCommand.php +++ b/src/Command/GenerateCommand.php @@ -5,12 +5,9 @@ namespace Rector\RectorGenerator\Command; use Rector\RectorGenerator\Exception\ShouldNotHappenException; -use Rector\RectorGenerator\FileSystem\ConfigFilesystem; use Rector\RectorGenerator\FileSystem\PathHelper; use Rector\RectorGenerator\Generator\RectorGenerator; use Rector\RectorGenerator\Provider\RectorRecipeProvider; -use Rector\RectorGenerator\TemplateVariablesFactory; -use Rector\RectorGenerator\ValueObject\NamePattern; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -22,9 +19,6 @@ final class GenerateCommand extends Command { public function __construct( - private readonly ConfigFilesystem $configFilesystem, - private readonly SymfonyStyle $symfonyStyle, - private readonly TemplateVariablesFactory $templateVariablesFactory, private readonly RectorRecipeProvider $rectorRecipeProvider, private readonly RectorGenerator $rectorGenerator, ) { @@ -39,29 +33,14 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $rectorRecipe = $this->rectorRecipeProvider->provide(); - $targetDirectory = getcwd(); - - $generatedFilePaths = $this->rectorGenerator->generate($rectorRecipe, $targetDirectory); - - // nothing new - if ($generatedFilePaths === []) { - return self::SUCCESS; - } + $symfonyStyle = new SymfonyStyle($input, $output); - $setFilePath = $rectorRecipe->getSetFilePath(); - if ($setFilePath !== null) { - $templateVariables = $this->templateVariablesFactory->createFromRectorRecipe($rectorRecipe); + $rectorRecipe = $this->rectorRecipeProvider->provide(); - $this->configFilesystem->appendRectorServiceToSet( - $setFilePath, - $templateVariables, - NamePattern::RECTOR_FQN_NAME_PATTERN - ); - } + $generatedFilePaths = $this->rectorGenerator->generate($rectorRecipe, getcwd()); $testCaseDirectoryPath = $this->resolveTestCaseDirectoryPath($generatedFilePaths); - $this->printSuccess($rectorRecipe->getName(), $generatedFilePaths, $testCaseDirectoryPath); + $this->printSuccess($rectorRecipe->getName(), $generatedFilePaths, $testCaseDirectoryPath, $symfonyStyle); return self::SUCCESS; } @@ -76,7 +55,7 @@ private function resolveTestCaseDirectoryPath(array $generatedFilePaths): string continue; } - $relativeFilePath = PathHelper::getRelativePathFromDirector($generatedFilePath, getcwd()); + $relativeFilePath = PathHelper::getRelativePathFromDirectory($generatedFilePath, getcwd()); return dirname($relativeFilePath); } @@ -86,21 +65,25 @@ private function resolveTestCaseDirectoryPath(array $generatedFilePaths): string /** * @param string[] $generatedFilePaths */ - private function printSuccess(string $name, array $generatedFilePaths, string $testCaseFilePath): void - { + private function printSuccess( + string $name, + array $generatedFilePaths, + string $testCaseFilePath, + SymfonyStyle $symfonyStyle + ): void { $message = sprintf('New files generated for "%s":', $name); - $this->symfonyStyle->title($message); + $symfonyStyle->title($message); sort($generatedFilePaths); foreach ($generatedFilePaths as $generatedFilePath) { - $relativeFilePath = PathHelper::getRelativePathFromDirector($generatedFilePath, getcwd()); - $this->symfonyStyle->writeln(' * ' . $relativeFilePath); + $relativeFilePath = PathHelper::getRelativePathFromDirectory($generatedFilePath, getcwd()); + $symfonyStyle->writeln(' * ' . $relativeFilePath); } $message = sprintf('Make tests green again:%svendor/bin/phpunit %s', PHP_EOL . PHP_EOL, $testCaseFilePath); - $this->symfonyStyle->success($message); + $symfonyStyle->success($message); } private function isGeneratedFilePathTestCase(string $generatedFilePath): bool diff --git a/src/Command/InitRecipeCommand.php b/src/Command/InitRecipeCommand.php index 94128cd..fd1c3b0 100644 --- a/src/Command/InitRecipeCommand.php +++ b/src/Command/InitRecipeCommand.php @@ -4,18 +4,18 @@ namespace Rector\RectorGenerator\Command; -use Rector\RectorGenerator\TemplateInitializer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Filesystem\Filesystem; final class InitRecipeCommand extends Command { - public function __construct( - private readonly TemplateInitializer $templateInitializer - ) { - parent::__construct(); - } + /** + * @var string + */ + private const RECIPE_FILE_NAME = 'rector-recipe.php'; protected function configure(): void { @@ -25,7 +25,20 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $this->templateInitializer->initialize(__DIR__ . '/../../templates/rector-recipe.php', 'rector-recipe.php'); + $symfonyStyle = new SymfonyStyle($input, $output); + + $templateFilePath = __DIR__ . '/../../templates/rector-recipe.php'; + $targetFilePath = getcwd() . '/' . self::RECIPE_FILE_NAME; + + if (file_exists($targetFilePath)) { + $symfonyStyle->warning(sprintf('Config file "%s" already exists', self::RECIPE_FILE_NAME)); + return self::SUCCESS; + } + + $filesystem = new Filesystem(); + $filesystem->copy($templateFilePath, $targetFilePath); + + $symfonyStyle->success(sprintf('"%s" config file was added', self::RECIPE_FILE_NAME)); return self::SUCCESS; } diff --git a/src/Enum/Packages.php b/src/Enum/Packages.php index 9ffe792..24e79ea 100644 --- a/src/Enum/Packages.php +++ b/src/Enum/Packages.php @@ -9,5 +9,5 @@ final class Packages /** * @var string[] */ - public const RECTOR_CORE = ['Symfony', 'Nette', 'Doctrine', 'Laravel', 'PHPUnit', 'CakePHP', 'PHPOffice']; + public const RECTOR_CORE = ['Symfony', 'Doctrine', 'PHPUnit']; } diff --git a/src/FileSystem/ConfigFilesystem.php b/src/FileSystem/ConfigFilesystem.php deleted file mode 100644 index 6bfb0a4..0000000 --- a/src/FileSystem/ConfigFilesystem.php +++ /dev/null @@ -1,72 +0,0 @@ - $templateVariables - */ - public function appendRectorServiceToSet( - string $setFilePath, - array $templateVariables, - string $rectorFqnNamePattern - ): void { - $setFileContents = FileSystem::read($setFilePath); - $this->ensureRequiredKeysAreSet($templateVariables); - - // already added? - $servicesFullyQualifiedName = $this->templateFactory->create($rectorFqnNamePattern, $templateVariables); - if (\str_contains($setFileContents, $servicesFullyQualifiedName)) { - return; - } - - $registerServiceLine = sprintf( - ';' . PHP_EOL . ' $rectorConfig->rule(\\%s::class);' . PHP_EOL . '};', - $servicesFullyQualifiedName - ); - $setFileContents = Strings::replace($setFileContents, self::LAST_ITEM_REGEX, $registerServiceLine); - - // 3. print the content back to file - - $this->filesystem->dumpFile($setFilePath, $setFileContents); - } - - /** - * @param array $templateVariables - */ - private function ensureRequiredKeysAreSet(array $templateVariables): void - { - $missingKeys = array_diff(self::REQUIRED_KEYS, array_keys($templateVariables)); - if ($missingKeys === []) { - return; - } - - $message = sprintf('Template variables for "%s" keys are missing', implode('", "', $missingKeys)); - throw new ShouldNotHappenException($message); - } -} diff --git a/src/FileSystem/PathHelper.php b/src/FileSystem/PathHelper.php index 47cd9a4..49a509c 100644 --- a/src/FileSystem/PathHelper.php +++ b/src/FileSystem/PathHelper.php @@ -8,10 +8,10 @@ final class PathHelper { - public static function getRelativePathFromDirector(string $filePath, string $directory): string + public static function getRelativePathFromDirectory(string $filePath, string $directory): string { - $smartFileSystem = new Filesystem(); - $relativeFilePath = $smartFileSystem->makePathRelative($filePath, (string) realpath($directory)); + $filesystem = new Filesystem(); + $relativeFilePath = $filesystem->makePathRelative($filePath, (string) realpath($directory)); return rtrim($relativeFilePath, '/'); } diff --git a/src/FileSystem/TemplateFileSystem.php b/src/FileSystem/TemplateFileSystem.php index d73c902..b294f45 100644 --- a/src/FileSystem/TemplateFileSystem.php +++ b/src/FileSystem/TemplateFileSystem.php @@ -31,12 +31,6 @@ final class TemplateFileSystem */ private const PACKAGE_RULES_TESTS_PATH_REGEX = '#(rules-tests)\/__Package__#i'; - /** - * @var string - * @see https://regex101.com/r/tOidWU/1 - */ - private const CONFIGURED_OR_EXTRA_REGEX = '#(__Configured|__Extra)#'; - public function __construct( private readonly TemplateFactory $templateFactory, private readonly Filesystem $filesystem, @@ -62,11 +56,8 @@ public function resolveDestination( $destination = Strings::replace($destination, self::PACKAGE_RULES_TESTS_PATH_REGEX, 'utils/rector/tests'); } - // remove _Configured|_Extra prefix $destination = $this->templateFactory->create($destination, $templateVariables); - $destination = Strings::replace($destination, self::CONFIGURED_OR_EXTRA_REGEX, ''); - // remove ".inc" protection from PHPUnit if not a test case if ($this->isNonFixtureFileWithIncSuffix($destination)) { $destination = Strings::before($destination, '.inc'); diff --git a/src/Finder/TemplateFinder.php b/src/Finder/TemplateFinder.php index 9e12a93..a579424 100644 --- a/src/Finder/TemplateFinder.php +++ b/src/Finder/TemplateFinder.php @@ -4,7 +4,6 @@ namespace Rector\RectorGenerator\Finder; -use Rector\RectorGenerator\ValueObject\RectorRecipe; use Webmozart\Assert\Assert; final class TemplateFinder @@ -17,48 +16,18 @@ final class TemplateFinder /** * @return string[] */ - public function find(RectorRecipe $rectorRecipe): array + public function find(): array { - $filePaths = []; + $filePaths = [ + __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/configured_rule.php', + // the ".inc" suffix is needed, so PHPUnit doesn't load it as a test case + __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/__Name__Test.php.inc', + __DIR__ . '/../../templates/rules/__Package__/Rector/__Category__/__Name__.php', + __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/Fixture/some_class.php.inc', + ]; - $filePaths = $this->addRuleAndTestCase($rectorRecipe, $filePaths); - $filePaths[] = __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/Fixture/some_class.php.inc'; - - $this->ensureFilePathsExists($filePaths); - - return $filePaths; - } - - /** - * @param string[] $filePaths - * @return string[] - * - * @note the ".inc" suffix is needed, so PHPUnit doesn't load it as a test case - */ - private function addRuleAndTestCase(RectorRecipe $rectorRecipe, array $filePaths): array - { - if ($rectorRecipe->getConfiguration() !== []) { - $filePaths[] = __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/__Configuredconfigured_rule.php'; - } else { - $filePaths[] = __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/configured_rule.php'; - } - - $filePaths[] = __DIR__ . '/../../templates/rules-tests/__Package__/Rector/__Category__/__Name__/__Name__Test.php.inc'; - - if ($rectorRecipe->getConfiguration() !== []) { - $filePaths[] = __DIR__ . '/../../templates/rules/__Package__/Rector/__Category__/__Configured__Name__.php'; - } else { - $filePaths[] = __DIR__ . '/../../templates/rules/__Package__/Rector/__Category__/__Name__.php'; - } + Assert::allFileExists($filePaths); return $filePaths; } - - /** - * @param string[] $filePaths - */ - private function ensureFilePathsExists(array $filePaths): void - { - Assert::allFileExists($filePaths, __METHOD__); - } } diff --git a/src/Generator/RectorGenerator.php b/src/Generator/RectorGenerator.php index 14ed0a8..e3e5d88 100644 --- a/src/Generator/RectorGenerator.php +++ b/src/Generator/RectorGenerator.php @@ -5,10 +5,8 @@ namespace Rector\RectorGenerator\Generator; use Rector\RectorGenerator\Finder\TemplateFinder; -use Rector\RectorGenerator\Guard\OverrideGuard; use Rector\RectorGenerator\TemplateVariablesFactory; use Rector\RectorGenerator\ValueObject\RectorRecipe; -use Symfony\Component\Console\Style\SymfonyStyle; /** * @see \Rector\RectorGenerator\Tests\RectorGenerator\RectorGeneratorTest @@ -19,8 +17,6 @@ public function __construct( private readonly TemplateFinder $templateFinder, private readonly TemplateVariablesFactory $templateVariablesFactory, private readonly FileGenerator $fileGenerator, - private readonly OverrideGuard $overrideGuard, - private readonly SymfonyStyle $symfonyStyle, ) { } @@ -30,22 +26,9 @@ public function __construct( public function generate(RectorRecipe $rectorRecipe, string $destinationDirectory): array { // generate and compare - $templateFilePaths = $this->templateFinder->find($rectorRecipe); - + $templateFilePaths = $this->templateFinder->find(); $templateVariables = $this->templateVariablesFactory->createFromRectorRecipe($rectorRecipe); - $isUnwantedOverride = $this->overrideGuard->isUnwantedOverride( - $templateFilePaths, - $templateVariables, - $rectorRecipe, - $destinationDirectory - ); - - if ($isUnwantedOverride) { - $this->symfonyStyle->warning('No files were changed'); - return []; - } - return $this->fileGenerator->generateFiles( $templateFilePaths, $templateVariables, diff --git a/src/Guard/OverrideGuard.php b/src/Guard/OverrideGuard.php deleted file mode 100644 index 646c1df..0000000 --- a/src/Guard/OverrideGuard.php +++ /dev/null @@ -1,65 +0,0 @@ - $templateVariables - * @param string[] $templateFilePaths - */ - public function isUnwantedOverride( - array $templateFilePaths, - array $templateVariables, - RectorRecipe $rectorRecipe, - string $targetDirectory - ): bool { - $message = sprintf('Files for "%s" rule already exist. Should we override them?', $rectorRecipe->getName()); - - foreach ($templateFilePaths as $templateFilePath) { - if (! $this->doesFileInfoAlreadyExist( - $templateVariables, - $rectorRecipe, - $templateFilePath, - $targetDirectory - )) { - continue; - } - - return ! $this->symfonyStyle->confirm($message); - } - - return false; - } - - /** - * @param array $templateVariables - */ - private function doesFileInfoAlreadyExist( - array $templateVariables, - RectorRecipe $rectorRecipe, - string $templateFilePath, - string $targetDirectory - ): bool { - $destination = $this->templateFileSystem->resolveDestination( - $templateFilePath, - $templateVariables, - $rectorRecipe, - $targetDirectory - ); - - return file_exists($destination); - } -} diff --git a/src/NodeFactory/ConfigurationNodeFactory.php b/src/NodeFactory/ConfigurationNodeFactory.php deleted file mode 100644 index c48cea4..0000000 --- a/src/NodeFactory/ConfigurationNodeFactory.php +++ /dev/null @@ -1,33 +0,0 @@ - $ruleConfiguration - * @return Property[] - */ - public function createProperties(array $ruleConfiguration): array - { - $properties = []; - - foreach (array_keys($ruleConfiguration) as $privatePropertyName) { - $property = $this->nodeFactory->createPrivateArrayProperty($privatePropertyName); - $property->props[0]->default = new Array_([]); - $properties[] = $property; - } - - return $properties; - } -} diff --git a/src/NodeFactory/ConfigureClassMethodFactory.php b/src/NodeFactory/ConfigureClassMethodFactory.php deleted file mode 100644 index 018aba1..0000000 --- a/src/NodeFactory/ConfigureClassMethodFactory.php +++ /dev/null @@ -1,53 +0,0 @@ - $ruleConfiguration - */ - public function create(array $ruleConfiguration): ClassMethod - { - $classMethod = $this->nodeFactory->createPublicMethod('configure'); - $classMethod->returnType = new Identifier('void'); - - $configurationVariable = new Variable('configuration'); - $configurationParam = new Param($configurationVariable); - $configurationParam->type = new Identifier('array'); - $classMethod->params[] = $configurationParam; - - $assigns = []; - - foreach (array_keys($ruleConfiguration) as $propertyName) { - $assign = $this->nodeFactory->createPropertyAssign($propertyName, $configurationVariable); - $assigns[] = new Expression($assign); - } - - $classMethod->stmts = $assigns; - - $paramDoc = <<<'CODE_SAMPLE' -/** - * @param mixed[] $configuration - */ -CODE_SAMPLE; - - $classMethod->setDocComment(new Doc($paramDoc)); - - return $classMethod; - } -} diff --git a/src/Provider/RectorRecipeProvider.php b/src/Provider/RectorRecipeProvider.php index 35945a3..edf8b4c 100644 --- a/src/Provider/RectorRecipeProvider.php +++ b/src/Provider/RectorRecipeProvider.php @@ -24,7 +24,7 @@ public function __construct(array $rectorRecipeConfiguration = []) return; } - $rectorRecipe = new RectorRecipe( + $this->rectorRecipe = new RectorRecipe( $rectorRecipeConfiguration[Option::PACKAGE], $rectorRecipeConfiguration[Option::NAME], $rectorRecipeConfiguration[Option::NODE_TYPES], @@ -32,17 +32,6 @@ public function __construct(array $rectorRecipeConfiguration = []) $rectorRecipeConfiguration[Option::CODE_BEFORE], $rectorRecipeConfiguration[Option::CODE_AFTER], ); - - // optional parameters - if (isset($rectorRecipeConfiguration[Option::CONFIGURATION])) { - $rectorRecipe->setConfiguration($rectorRecipeConfiguration[Option::CONFIGURATION]); - } - - if (isset($rectorRecipeConfiguration[Option::SET_FILE_PATH])) { - $rectorRecipe->setSetFilePath($rectorRecipeConfiguration[Option::SET_FILE_PATH]); - } - - $this->rectorRecipe = $rectorRecipe; } public function provide(): RectorRecipe diff --git a/src/TemplateInitializer.php b/src/TemplateInitializer.php deleted file mode 100644 index fb164b0..0000000 --- a/src/TemplateInitializer.php +++ /dev/null @@ -1,35 +0,0 @@ -filesystem->exists($targetFilePath); - if ($doesFileExist) { - $message = sprintf('Config file "%s" already exists', $rootFileName); - $this->symfonyStyle->warning($message); - } else { - $this->filesystem->copy($templateFilePath, $targetFilePath); - $message = sprintf('"%s" config file was added', $rootFileName); - $this->symfonyStyle->success($message); - } - } -} diff --git a/src/TemplateVariablesFactory.php b/src/TemplateVariablesFactory.php index a9b5fa4..c7ccce6 100644 --- a/src/TemplateVariablesFactory.php +++ b/src/TemplateVariablesFactory.php @@ -4,12 +4,7 @@ namespace Rector\RectorGenerator; -use PhpParser\BuilderHelpers; -use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\PrettyPrinter\Standard; -use Rector\RectorGenerator\NodeFactory\ConfigurationNodeFactory; -use Rector\RectorGenerator\NodeFactory\ConfigureClassMethodFactory; use Rector\RectorGenerator\NodeFactory\NodeFactory; use Rector\RectorGenerator\ValueObject\Placeholder; use Rector\RectorGenerator\ValueObject\RectorRecipe; @@ -18,8 +13,6 @@ final class TemplateVariablesFactory { public function __construct( private readonly Standard $standard, - private readonly ConfigurationNodeFactory $configurationNodeFactory, - private readonly ConfigureClassMethodFactory $configureClassMethodFactory, private readonly NodeFactory $nodeFactory, ) { } @@ -40,11 +33,6 @@ public function createFromRectorRecipe(RectorRecipe $rectorRecipe): array Placeholder::CODE_AFTER_EXAMPLE => $this->createCodeForDefinition($rectorRecipe->getCodeAfter()), ]; - if ($rectorRecipe->getConfiguration() !== []) { - $configurationData = $this->createConfigurationData($rectorRecipe); - $data = array_merge($data, $configurationData); - } - $data['__NodeTypesPhp__'] = $this->createNodeTypePhp($rectorRecipe); $data['__NodeTypesDoc__'] = '\\' . implode('|\\', $rectorRecipe->getNodeTypes()); @@ -62,58 +50,6 @@ private function createCodeForDefinition(string $code): string return "'" . str_replace("'", '"', $code) . "'"; } - /** - * @param string[] $source - */ - private function createSourceDocBlock(array $source): string - { - if ($source === []) { - return ''; - } - - $sourceAsString = ''; - foreach ($source as $singleSource) { - $sourceAsString .= ' * @changelog ' . $singleSource . PHP_EOL; - } - - $sourceAsString .= ' *'; - - return rtrim($sourceAsString); - } - - /** - * @param array $configuration - */ - private function createRuleConfiguration(array $configuration): string - { - $arrayItems = []; - foreach ($configuration as $singleConfiguration) { - $singleConfiguration = BuilderHelpers::normalizeValue($singleConfiguration); - $arrayItems[] = new ArrayItem($singleConfiguration); - } - - $array = new Array_($arrayItems); - return $this->standard->prettyPrintExpr($array); - } - - /** - * @param array $ruleConfiguration - */ - private function createConfigurationProperty(array $ruleConfiguration): string - { - $properties = $this->configurationNodeFactory->createProperties($ruleConfiguration); - return $this->standard->prettyPrint($properties); - } - - /** - * @param array $ruleConfiguration - */ - private function createConfigureClassMethod(array $ruleConfiguration): string - { - $classMethod = $this->configureClassMethodFactory->create($ruleConfiguration); - return $this->standard->prettyPrint([$classMethod]); - } - private function createNodeTypePhp(RectorRecipe $rectorRecipe): string { $referencingClassConsts = []; @@ -124,45 +60,4 @@ private function createNodeTypePhp(RectorRecipe $rectorRecipe): string $array = $this->nodeFactory->createArray($referencingClassConsts); return $this->standard->prettyPrintExpr($array); } - - /** - * @return array - */ - private function createConfigurationData(RectorRecipe $rectorRecipe): array - { - $configurationData = []; - - $configurationData['__TestRuleConfiguration__'] = $this->createRuleConfiguration( - $rectorRecipe->getConfiguration() - ); - - $configurationData['__RuleConfiguration__'] = $this->createRuleConfiguration( - $rectorRecipe->getConfiguration() - ); - - $configurationData['__ConfigurationProperties__'] = $this->createConfigurationProperty( - $rectorRecipe->getConfiguration() - ); - - $configurationData['__ConfigureClassMethod__'] = $this->createConfigureClassMethod( - $rectorRecipe->getConfiguration() - ); - - $configurationData['__MainConfiguration__'] = $this->createMainConfiguration( - $rectorRecipe->getConfiguration() - ); - - return $configurationData; - } - - /** - * @param array $ruleConfiguration - */ - private function createMainConfiguration(array $ruleConfiguration): string - { - $firstItem = array_pop($ruleConfiguration); - - $valueExpr = BuilderHelpers::normalizeValue($firstItem); - return $this->standard->prettyPrintExpr($valueExpr); - } } diff --git a/src/ValueObject/NamePattern.php b/src/ValueObject/NamePattern.php deleted file mode 100644 index 10b63d6..0000000 --- a/src/ValueObject/NamePattern.php +++ /dev/null @@ -1,13 +0,0 @@ - - */ - private array $configuration = []; - /** * Use default package name, if not overriden manually */ private string $package = self::PACKAGE_UTILS; - private ?string $setFilePath = null; - /** * @param array> $nodeTypes */ @@ -92,7 +77,7 @@ public function getName(): string } /** - * @return class-string[] + * @return string[] */ public function getNodeTypes(): array { @@ -114,32 +99,6 @@ public function getCodeAfter(): string return $this->codeAfter; } - /** - * @return string[] - */ - public function getResources(): array - { - return $this->resources; - } - - public function setSetFilePath(string $setFilePath): void - { - $this->setFilePath = $setFilePath; - } - - public function getSetFilePath(): ?string - { - return $this->setFilePath; - } - - /** - * @return array - */ - public function getConfiguration(): array - { - return $this->configuration; - } - public function isRectorRepository(): bool { return $this->isRectorRepository; @@ -150,24 +109,6 @@ public function getCategory(): string return $this->category; } - /** - * @api - * @param array $configuration - */ - public function setConfiguration(array $configuration): void - { - $this->configuration = $configuration; - } - - /** - * @api - * @param string[] $resources - */ - public function setResources(array $resources): void - { - $this->resources = array_filter($resources); - } - /** * For testing purposes * @@ -215,7 +156,7 @@ private function setNodeTypes(array $nodeTypes): void { foreach ($nodeTypes as $nodeType) { // avoid phpstan class method that is never traversed - Assert::isNotA($nodeType, ClassMethod::class); + Assert::isNotA($nodeType, 'PHPStan\Node\ClassMethod'); if (is_a($nodeType, Node::class, true)) { continue; diff --git a/templates/rector-recipe.php b/templates/rector-recipe.php index 1fb3ef4..386a0b3 100644 --- a/templates/rector-recipe.php +++ b/templates/rector-recipe.php @@ -48,18 +48,6 @@ public function run() } CODE_SAMPLE , - - // [OPTIONAL - UNCOMMENT TO USE] - - // is the rule configurable? - // Option::CONFIGURATION => [ - // 'privatePropertyName' => [ - // 'old_value' => 'newValue', - // ], - // ], - - // set the rule belongs to; is optional, because e.g. generic rules don't need a specific set to belong to - // Option::SET_FILE_PATH => \Rector\Set\ValueObject\SetList::NAMING, ]; $rectorConfig->singleton(RectorRecipeProvider::class, function () use ( diff --git a/templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/__Configuredconfigured_rule.php b/templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/__Configuredconfigured_rule.php deleted file mode 100644 index 103da34..0000000 --- a/templates/rules-tests/__Package__/Rector/__Category__/__Name__/config/__Configuredconfigured_rule.php +++ /dev/null @@ -1,11 +0,0 @@ -ruleWithConfiguration(\Rector\__Package__\Rector\__Category__\__Name__::class, - __MainConfiguration__ - ); -}; diff --git a/templates/rules/__Package__/Rector/__Category__/__Configured__Name__.php b/templates/rules/__Package__/Rector/__Category__/__Configured__Name__.php deleted file mode 100644 index a91912f..0000000 --- a/templates/rules/__Package__/Rector/__Category__/__Configured__Name__.php +++ /dev/null @@ -1,51 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return __NodeTypesPhp__; - } - - /** - * @param __NodeTypesDoc__ $node - */ - public function refactor(Node $node): ?Node - { - // change the node - - return $node; - } - - __ConfigureClassMethod__ -} diff --git a/templates/rules/__Package__/Rector/__Category__/__Name__.php b/templates/rules/__Package__/Rector/__Category__/__Name__.php index 124cda7..c9919f6 100644 --- a/templates/rules/__Package__/Rector/__Category__/__Name__.php +++ b/templates/rules/__Package__/Rector/__Category__/__Name__.php @@ -10,7 +10,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** -__Resources__ * @see \Rector\Tests\__Package__\Rector\__Category__\__Name__\__Name__Test */ final class __Name__ extends AbstractRector diff --git a/tests/Config/ConfigFilesystemTest.php b/tests/Config/ConfigFilesystemTest.php deleted file mode 100644 index 60f94a9..0000000 --- a/tests/Config/ConfigFilesystemTest.php +++ /dev/null @@ -1,51 +0,0 @@ -configFilesystem = $this->make(ConfigFilesystem::class); - } - - #[DataProvider('provideData')] - public function test(string $filePath): void - { - [$inputContents, $expectedContents] = FixtureSplitter::split($filePath); - - $setTempFilePath = sys_get_temp_dir() . '/temp-set-path.php'; - \Nette\Utils\FileSystem::write($setTempFilePath, $inputContents); - - $rectorRecipe = StaticRectorRecipeFactory::createRectorRecipe($setTempFilePath, false); - - $setTempFilePath = $rectorRecipe->getSetFilePath(); - $this->assertNotNull($setTempFilePath); - - /** @var string $setTempFilePath */ - $this->configFilesystem->appendRectorServiceToSet($setTempFilePath, [ - '__Package__' => 'SomePackage', - '__Category__' => 'String_', - '__Name__' => 'SomeRector', - ], NamePattern::RECTOR_FQN_NAME_PATTERN); - - $this->assertSame($expectedContents, \Nette\Utils\FileSystem::read($setTempFilePath)); - } - - public static function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/some_set.php.inc']; - } -} diff --git a/tests/Config/Fixture/some_set.php.inc b/tests/Config/Fixture/some_set.php.inc deleted file mode 100644 index ad4a335..0000000 --- a/tests/Config/Fixture/some_set.php.inc +++ /dev/null @@ -1,24 +0,0 @@ -rule('SomeDummyRector'); -}; - -?> ------ -rule('SomeDummyRector'); - $rectorConfig->rule(\Rector\SomePackage\Rector\String_\SomeRector::class); -}; - -?> diff --git a/tests/RectorGenerator/Fixture/expected/rules-tests/ModeratePackage/Rector/MethodCall/WhateverRector/config/configured_rule.php b/tests/RectorGenerator/Fixture/expected/rules-tests/ModeratePackage/Rector/MethodCall/WhateverRector/config/configured_rule.php index cb41fe1..c889d4e 100644 --- a/tests/RectorGenerator/Fixture/expected/rules-tests/ModeratePackage/Rector/MethodCall/WhateverRector/config/configured_rule.php +++ b/tests/RectorGenerator/Fixture/expected/rules-tests/ModeratePackage/Rector/MethodCall/WhateverRector/config/configured_rule.php @@ -5,7 +5,5 @@ use Rector\Config\RectorConfig; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->ruleWithConfiguration(\Rector\ModeratePackage\Rector\MethodCall\WhateverRector::class, - ['old_package_name' => 'new_package_name'] - ); + $rectorConfig->rule(\Rector\ModeratePackage\Rector\MethodCall\WhateverRector::class); }; diff --git a/tests/RectorGenerator/Fixture/expected/rules/ModeratePackage/Rector/MethodCall/WhateverRector.php b/tests/RectorGenerator/Fixture/expected/rules/ModeratePackage/Rector/MethodCall/WhateverRector.php index 0b559af..69a6ba9 100644 --- a/tests/RectorGenerator/Fixture/expected/rules/ModeratePackage/Rector/MethodCall/WhateverRector.php +++ b/tests/RectorGenerator/Fixture/expected/rules/ModeratePackage/Rector/MethodCall/WhateverRector.php @@ -5,37 +5,30 @@ namespace Rector\ModeratePackage\Rector\MethodCall; use PhpParser\Node; -use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; -use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\ModeratePackage\Rector\MethodCall\WhateverRector\WhateverRectorTest */ -final class WhateverRector extends AbstractRector implements ConfigurableRectorInterface +final class WhateverRector extends AbstractRector { - /** - * @var mixed[] - */ -private $renamedPackages = []; - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change $service->arg(...) to $service->call(...)', [ - new ConfiguredCodeSample( + new CodeSample( <<<'CODE_SAMPLE' $result = []; echo 'code before'; CODE_SAMPLE -, + + , <<<'CODE_SAMPLE' $result = []; echo 'code after'; CODE_SAMPLE -, - [['old_package_name' => 'new_package_name']] + ) ]); } @@ -57,12 +50,4 @@ public function refactor(Node $node): ?Node return $node; } - - /** - * @param mixed[] $configuration - */ -public function configure(array $configuration) : void -{ - $this->renamedPackages = $configuration; -} } 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 index 4f9b239..c744ec3 100644 --- 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 @@ -5,37 +5,30 @@ namespace Utils\Rector\Rector\MethodCall; use PhpParser\Node; -use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; -use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Utils\Rector\Tests\Rector\MethodCall\WhateverRector\WhateverRectorTest */ -final class WhateverRector extends AbstractRector implements ConfigurableRectorInterface +final class WhateverRector extends AbstractRector { - /** - * @var mixed[] - */ -private $renamedPackages = []; - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change $service->arg(...) to $service->call(...)', [ - new ConfiguredCodeSample( + new CodeSample( <<<'CODE_SAMPLE' $result = []; echo 'code before'; CODE_SAMPLE -, + + , <<<'CODE_SAMPLE' $result = []; echo 'code after'; CODE_SAMPLE -, - [['old_package_name' => 'new_package_name']] + ) ]); } @@ -57,12 +50,4 @@ public function refactor(Node $node): ?Node return $node; } - - /** - * @param mixed[] $configuration - */ -public function configure(array $configuration) : void -{ - $this->renamedPackages = $configuration; -} } 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 index 66feb3f..13e2031 100644 --- 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 @@ -5,7 +5,5 @@ use Rector\Config\RectorConfig; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->ruleWithConfiguration(\Utils\Rector\Rector\MethodCall\WhateverRector::class, - ['old_package_name' => 'new_package_name'] - ); + $rectorConfig->rule(\Utils\Rector\Rector\MethodCall\WhateverRector::class); }; diff --git a/tests/RectorGenerator/RectorGeneratorTest.php b/tests/RectorGenerator/RectorGeneratorTest.php index 821cabd..a4b2a22 100644 --- a/tests/RectorGenerator/RectorGeneratorTest.php +++ b/tests/RectorGenerator/RectorGeneratorTest.php @@ -9,6 +9,8 @@ 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; final class RectorGeneratorTest extends AbstractLazyTestCase { @@ -38,7 +40,16 @@ public function test(): void $rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', true); $this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY); - $this->assertDirectoryEquals(__DIR__ . '/Fixture/expected', self::DESTINATION_DIRECTORY); + $fileInfos = $this->findFileInfos(__DIR__ . '/Fixture/expected/'); + + foreach ($fileInfos as $fileInfo) { + $this->assertFileExists(self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname()); + + $this->assertFileEquals( + $fileInfo->getRealPath(), + self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname() + ); + } } public function test3rdParty(): void @@ -46,11 +57,34 @@ public function test3rdParty(): void $rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', false); $this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY); - $this->assertDirectoryEquals(__DIR__ . '/Fixture/expected_3rd_party', 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[] + */ + private function findFileInfos(string $directory): array + { + $fileInfos = Finder::create() + ->in($directory) + ->files() + ->sortByName() + ->getIterator(); + + return iterator_to_array($fileInfos); + } } diff --git a/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php b/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php index 61c3528..d8bad51 100644 --- a/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php +++ b/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php @@ -40,19 +40,11 @@ public static function createRectorRecipe(string $setFilePath, bool $isRectorRep CODE_SAMPLE ); - $rectorRecipe->setConfiguration([ - 'renamedPackages' => [ - 'old_package_name' => 'new_package_name' - ] - ]); - $rectorRecipe->setIsRectorRepository($isRectorRepository); if ($isRectorRepository) { $rectorRecipe->setPackage('ModeratePackage'); } - $rectorRecipe->setSetFilePath($setFilePath); - return $rectorRecipe; } } diff --git a/tests/Source/Set/DummySetList.php b/tests/Source/Set/DummySetList.php deleted file mode 100644 index 9d06bca..0000000 --- a/tests/Source/Set/DummySetList.php +++ /dev/null @@ -1,16 +0,0 @@ -