diff --git a/.github/workflows/code_analysis_no_dev.yaml b/.github/workflows/code_analysis_no_dev.yaml index 0ea4562e73f..53a417699e5 100644 --- a/.github/workflows/code_analysis_no_dev.yaml +++ b/.github/workflows/code_analysis_no_dev.yaml @@ -12,7 +12,7 @@ jobs: - name: 'Rector Recipe' run: | - cp rector-recipe.php.dist rector-recipe.php + bin/rector init-recipe --ansi bin/rector generate --ansi name: ${{ matrix.actions.name }} diff --git a/docs/rector_recipe.md b/docs/rector_recipe.md index 64d46fa9fe7..7435d7e3ead 100644 --- a/docs/rector_recipe.md +++ b/docs/rector_recipe.md @@ -11,8 +11,14 @@ Don't worry, also generates a test case, which is required to contribute. ## How to Generate Rector rule in 3 steps? -1. Copy [`rector-recipe.php.dist`](/rector-recipe.php.dist) to `rector-recipe.php` -2. Change parameters in `rector-recipe.php` to meet you need +1. Initialize `rector-recipe.php` config + + ```bash + vendor/bin/rector init-recipe + ``` + +2. Complete parameters in `rector-recipe.php` to design your new rule + 3. Run command ```bash diff --git a/packages/rector-generator/src/Command/InitRecipeCommand.php b/packages/rector-generator/src/Command/InitRecipeCommand.php new file mode 100644 index 00000000000..0fdc7dbd533 --- /dev/null +++ b/packages/rector-generator/src/Command/InitRecipeCommand.php @@ -0,0 +1,41 @@ +templateInitializer = $templateInitializer; + } + + protected function configure(): void + { + $this->setDescription('[DEV] Initialize "rector-recipe.php" config'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->templateInitializer->initialize( + __DIR__ . '/../../../../templates/rector-recipe.php.dist', + 'rector-recipe.php' + ); + + return ShellCode::SUCCESS; + } +} diff --git a/packages/rector-generator/src/TemplateInitializer.php b/packages/rector-generator/src/TemplateInitializer.php new file mode 100644 index 00000000000..7a27ce6b2f0 --- /dev/null +++ b/packages/rector-generator/src/TemplateInitializer.php @@ -0,0 +1,54 @@ +symfonyStyle = $symfonyStyle; + $this->smartFileSystem = $smartFileSystem; + $this->fileSystemGuard = $fileSystemGuard; + } + + public function initialize(string $templateFilePath, string $rootFileName): void + { + $this->fileSystemGuard->ensureFileExists($templateFilePath, __METHOD__); + + $targetFilePath = getcwd() . '/' . $rootFileName; + + $doesFileExist = $this->smartFileSystem->exists($targetFilePath); + if ($doesFileExist) { + $message = sprintf('Config file "%s" already exists', $rootFileName); + $this->symfonyStyle->warning($message); + } else { + $this->smartFileSystem->copy($templateFilePath, $targetFilePath); + $message = sprintf('"%s" config file was added', $rootFileName); + $this->symfonyStyle->success($message); + } + } +} diff --git a/rules/downgrade-php80/src/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php b/rules/downgrade-php80/src/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php index 0b905a0eae8..7649edbf408 100644 --- a/rules/downgrade-php80/src/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php +++ b/rules/downgrade-php80/src/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php @@ -12,7 +12,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Param; use Rector\Core\Rector\AbstractRector; -use Rector\Core\ValueObject\PhpVersionFeature; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; diff --git a/src/Console/Command/InitCommand.php b/src/Console/Command/InitCommand.php index fb7f8f868f6..da9e96fff1c 100644 --- a/src/Console/Command/InitCommand.php +++ b/src/Console/Command/InitCommand.php @@ -4,30 +4,23 @@ declare(strict_types=1); namespace Rector\Core\Console\Command; +use Rector\RectorGenerator\TemplateInitializer; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; use Symplify\PackageBuilder\Console\ShellCode; -use Symplify\SmartFileSystem\SmartFileSystem; final class InitCommand extends AbstractCommand { /** - * @var SmartFileSystem + * @var TemplateInitializer */ - private $smartFileSystem; + private $templateInitializer; - /** - * @var SymfonyStyle - */ - private $symfonyStyle; - - public function __construct(SmartFileSystem $smartFileSystem, SymfonyStyle $symfonyStyle) + public function __construct(TemplateInitializer $templateInitializer) { parent::__construct(); - $this->smartFileSystem = $smartFileSystem; - $this->symfonyStyle = $symfonyStyle; + $this->templateInitializer = $templateInitializer; } protected function configure(): void @@ -37,14 +30,7 @@ final class InitCommand extends AbstractCommand protected function execute(InputInterface $input, OutputInterface $output): int { - $rectorConfigFiles = $this->smartFileSystem->exists(getcwd() . '/rector.php'); - - if (! $rectorConfigFiles) { - $this->smartFileSystem->copy(__DIR__ . '/../../../templates/rector.php.dist', getcwd() . '/rector.php'); - $this->symfonyStyle->success('"rector.php" config file has been generated successfully!'); - } else { - $this->symfonyStyle->error('Config file not generated. A "rector.php" configuration file already exists'); - } + $this->templateInitializer->initialize(__DIR__ . '/../../../templates/rector.php.dist', 'rector.php'); return ShellCode::SUCCESS; } diff --git a/rector-recipe.php.dist b/templates/rector-recipe.php.dist similarity index 100% rename from rector-recipe.php.dist rename to templates/rector-recipe.php.dist diff --git a/utils/rule-doc-generator/src/Category/RectorCategoryInferer.php b/utils/rule-doc-generator/src/Category/RectorCategoryInferer.php index 4014dea838d..2338877b04a 100644 --- a/utils/rule-doc-generator/src/Category/RectorCategoryInferer.php +++ b/utils/rule-doc-generator/src/Category/RectorCategoryInferer.php @@ -1,4 +1,5 @@ \w+)\\\\#'; + private const RECTOR_CATEGORY_REGEX = '#Rector\\\\(?<' . self::CATEGORY . '>\w+)\\\\#'; + + /** + * @var string + */ + private const CATEGORY = 'category'; public function infer(RuleDefinition $ruleDefinition): ?string { $matches = Strings::match($ruleDefinition->getRuleClass(), self::RECTOR_CATEGORY_REGEX); - if (! isset($matches['category'])) { + if (! isset($matches[self::CATEGORY])) { $message = sprintf('Category for "%s" could not be resolved', $ruleDefinition->getRuleClass()); throw new ShouldNotHappenException($message); } - return $matches['category']; + return $matches[self::CATEGORY]; } }