From dd875cd0c94c05afa3b54e0439a29fe6af6edb4e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 25 Jan 2024 00:06:40 +0000 Subject: [PATCH] Updated Rector to commit d09ae7400a75f7694a11c2d8353c2fd14b6419e3 https://github.com/rectorphp/rector-src/commit/d09ae7400a75f7694a11c2d8353c2fd14b6419e3 Add "custom-rule" command to make creating rules easy (#5498) --- src/Application/VersionResolver.php | 4 +- src/Config/RectorConfig.php | 17 ----- src/Console/Command/CustomRuleCommand.php | 75 +++++++++++++++++++ .../LazyContainerFactory.php | 2 + src/FileSystem/JsonFileSystem.php | 8 ++ .../utils/rector/src/Rector/__Name__.php | 45 +++++++++++ .../__Name__/Fixture/some_class.php.inc | 15 ++++ .../Rector/__Name__/__Name__Test.php.inc | 27 +++++++ .../__Name__/config/configured_rule.php | 9 +++ vendor/autoload.php | 2 +- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_real.php | 10 +-- vendor/composer/autoload_static.php | 9 ++- vendor/scoper-autoload.php | 2 +- 14 files changed, 196 insertions(+), 30 deletions(-) create mode 100644 src/Console/Command/CustomRuleCommand.php create mode 100644 templates/custom-rule/utils/rector/src/Rector/__Name__.php create mode 100644 templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc create mode 100644 templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php.inc create mode 100644 templates/custom-rule/utils/rector/tests/Rector/__Name__/config/configured_rule.php diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 99fd7d063be..8ce4fa9555d 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '955a5de414501f77b80b321a90ee35d4fe49e9e2'; + public const PACKAGE_VERSION = 'd09ae7400a75f7694a11c2d8353c2fd14b6419e3'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-01-24 21:24:18'; + public const RELEASE_DATE = '2024-01-25 00:04:31'; /** * @var int */ diff --git a/src/Config/RectorConfig.php b/src/Config/RectorConfig.php index 8996ab90da2..46ebc55adf8 100644 --- a/src/Config/RectorConfig.php +++ b/src/Config/RectorConfig.php @@ -4,7 +4,6 @@ declare (strict_types=1); namespace Rector\Config; use RectorPrefix202401\Illuminate\Container\Container; -use PHPStan\Collectors\Collector; use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; @@ -168,22 +167,6 @@ final class RectorConfig extends Container // for cache invalidation in case of change SimpleParameterProvider::addParameter(Option::REGISTERED_RECTOR_RULES, $rectorClass); } - /** - * @param array> $collectorClasses - */ - public function collectors(array $collectorClasses) : void - { - foreach ($collectorClasses as $collectorClass) { - $this->collector($collectorClass); - } - } - /** - * @param class-string $collectorClass - */ - public function collector(string $collectorClass) : void - { - \trigger_error('collector have been deprecated as performance costly and not valuable'); - } /** * @param class-string $commandClass */ diff --git a/src/Console/Command/CustomRuleCommand.php b/src/Console/Command/CustomRuleCommand.php new file mode 100644 index 00000000000..353775f14c8 --- /dev/null +++ b/src/Console/Command/CustomRuleCommand.php @@ -0,0 +1,75 @@ +symfonyStyle = $symfonyStyle; + parent::__construct(); + } + protected function configure() : void + { + $this->setName('custom-rule'); + $this->setDescription('Create base of local custom rule with tests'); + } + protected function execute(InputInterface $input, OutputInterface $output) : int + { + // ask for rule name + $rectorName = $this->symfonyStyle->ask('What is the name of the rule class (e.g. "LegacyCallToDbalMethodCall")?', null, static function (string $answer) : string { + if ($answer === '') { + throw new ShouldNotHappenException('Rector name cannot be empty'); + } + return $answer; + }); + // suffix with Rector by convention + if (\substr_compare((string) $rectorName, 'Rector', -\strlen('Rector')) !== 0) { + $rectorName .= 'Rector'; + } + $rectorName = \ucfirst((string) $rectorName); + // find all files in templates directory + $fileInfos = Finder::create()->files()->in(__DIR__ . '/../../../templates/custom-rule')->getIterator(); + $generatedFilePaths = []; + foreach ($fileInfos as $fileInfo) { + // replace __Name__ with $rectorName + $newContent = \str_replace('__Name__', $rectorName, $fileInfo->getContents()); + $newFilePath = \str_replace('__Name__', $rectorName, $fileInfo->getRelativePathname()); + FileSystem::write(\getcwd() . '/' . $newFilePath, $newContent); + $generatedFilePaths[] = $newFilePath; + } + $this->symfonyStyle->title('Generated files'); + $this->symfonyStyle->listing($generatedFilePaths); + $this->symfonyStyle->success(\sprintf('Base for the "%s" rule was created. Now you can fill the missing parts', $rectorName)); + // 2. update autoload-dev in composer.json + $composerJsonFilePath = \getcwd() . '/composer.json'; + if (\file_exists($composerJsonFilePath)) { + $hasChanged = \false; + $composerJson = JsonFileSystem::readFilePath($composerJsonFilePath); + if (!isset($composerJson['autoload-dev']['psr-4']['Utils\\Rector\\'])) { + $composerJson['autoload-dev']['psr-4']['Utils\\Rector\\'] = 'utils/rector/src'; + $composerJson['autoload-dev']['psr-4']['Utils\\Rector\\Tests\\'] = 'utils/rector/tests'; + $hasChanged = \true; + } + if ($hasChanged) { + $this->symfonyStyle->success('We also update composer.json autoload-dev, to load Rector rules. Now run "composer dump-autoload" to update paths'); + JsonFileSystem::writeFile($composerJsonFilePath, $composerJson); + } + } + return Command::SUCCESS; + } +} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 7fe3ead5899..c8bbbb04621 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -48,6 +48,7 @@ use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterfac use Rector\Config\RectorConfig; use Rector\Configuration\ConfigInitializer; use Rector\Configuration\RenamedClassesDataCollector; +use Rector\Console\Command\CustomRuleCommand; use Rector\Console\Command\ListRulesCommand; use Rector\Console\Command\ProcessCommand; use Rector\Console\Command\SetupCICommand; @@ -257,6 +258,7 @@ final class LazyContainerFactory $rectorConfig->singleton(WorkerCommand::class); $rectorConfig->singleton(SetupCICommand::class); $rectorConfig->singleton(ListRulesCommand::class); + $rectorConfig->singleton(CustomRuleCommand::class); $rectorConfig->when(ListRulesCommand::class)->needs('$rectors')->giveTagged(RectorInterface::class); // dev if (\class_exists(MissingInSetCommand::class)) { diff --git a/src/FileSystem/JsonFileSystem.php b/src/FileSystem/JsonFileSystem.php index 5ecd0e7bde5..e22b97d088e 100644 --- a/src/FileSystem/JsonFileSystem.php +++ b/src/FileSystem/JsonFileSystem.php @@ -15,4 +15,12 @@ final class JsonFileSystem $fileContents = FileSystem::read($filePath); return Json::decode($fileContents, Json::FORCE_ARRAY); } + /** + * @param array $data + */ + public static function writeFile(string $filePath, array $data) : void + { + $json = Json::encode($data, Json::PRETTY); + FileSystem::write($filePath, $json); + } } diff --git a/templates/custom-rule/utils/rector/src/Rector/__Name__.php b/templates/custom-rule/utils/rector/src/Rector/__Name__.php new file mode 100644 index 00000000000..e64c780d767 --- /dev/null +++ b/templates/custom-rule/utils/rector/src/Rector/__Name__.php @@ -0,0 +1,45 @@ +> + */ + public function getNodeTypes(): array + { + // @todo select node type + return [\PhpParser\Node\Stmt\Class_::class]; + } + + /** + * @param \PhpParser\Node\Stmt\Class_ $node + */ + public function refactor(Node $node): ?Node + { + // @todo change the node + + return $node; + } +} diff --git a/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc b/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc new file mode 100644 index 00000000000..7fb1cf3967e --- /dev/null +++ b/templates/custom-rule/utils/rector/tests/Rector/__Name__/Fixture/some_class.php.inc @@ -0,0 +1,15 @@ + +----- + diff --git a/templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php.inc b/templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php.inc new file mode 100644 index 00000000000..75be5573365 --- /dev/null +++ b/templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php.inc @@ -0,0 +1,27 @@ +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/templates/custom-rule/utils/rector/tests/Rector/__Name__/config/configured_rule.php b/templates/custom-rule/utils/rector/tests/Rector/__Name__/config/configured_rule.php new file mode 100644 index 00000000000..25ee3a80e6d --- /dev/null +++ b/templates/custom-rule/utils/rector/tests/Rector/__Name__/config/configured_rule.php @@ -0,0 +1,9 @@ +rule(\Utils\Rector\Rector\__Name__::class); +}; diff --git a/vendor/autoload.php b/vendor/autoload.php index 9ef84cfb35b..d2d108fd09a 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit0408599c96f445821212bfb9798860ca::getLoader(); +return ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index f3d2a878a9c..b4552ce07e1 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -1176,6 +1176,7 @@ return array( 'Rector\\Configuration\\Option' => $baseDir . '/src/Configuration/Option.php', 'Rector\\Configuration\\Parameter\\SimpleParameterProvider' => $baseDir . '/src/Configuration/Parameter/SimpleParameterProvider.php', 'Rector\\Configuration\\RenamedClassesDataCollector' => $baseDir . '/src/Configuration/RenamedClassesDataCollector.php', + 'Rector\\Console\\Command\\CustomRuleCommand' => $baseDir . '/src/Console/Command/CustomRuleCommand.php', 'Rector\\Console\\Command\\ListRulesCommand' => $baseDir . '/src/Console/Command/ListRulesCommand.php', 'Rector\\Console\\Command\\ProcessCommand' => $baseDir . '/src/Console/Command/ProcessCommand.php', 'Rector\\Console\\Command\\SetupCICommand' => $baseDir . '/src/Console/Command/SetupCICommand.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 84adbb5fe30..d921b0e588e 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit0408599c96f445821212bfb9798860ca +class ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit0408599c96f445821212bfb9798860ca return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit0408599c96f445821212bfb9798860ca', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit0408599c96f445821212bfb9798860ca', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit0408599c96f445821212bfb9798860ca::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit0408599c96f445821212bfb9798860ca::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index eedbc3765df..98d0d0ad008 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit0408599c96f445821212bfb9798860ca +class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -1390,6 +1390,7 @@ class ComposerStaticInit0408599c96f445821212bfb9798860ca 'Rector\\Configuration\\Option' => __DIR__ . '/../..' . '/src/Configuration/Option.php', 'Rector\\Configuration\\Parameter\\SimpleParameterProvider' => __DIR__ . '/../..' . '/src/Configuration/Parameter/SimpleParameterProvider.php', 'Rector\\Configuration\\RenamedClassesDataCollector' => __DIR__ . '/../..' . '/src/Configuration/RenamedClassesDataCollector.php', + 'Rector\\Console\\Command\\CustomRuleCommand' => __DIR__ . '/../..' . '/src/Console/Command/CustomRuleCommand.php', 'Rector\\Console\\Command\\ListRulesCommand' => __DIR__ . '/../..' . '/src/Console/Command/ListRulesCommand.php', 'Rector\\Console\\Command\\ProcessCommand' => __DIR__ . '/../..' . '/src/Console/Command/ProcessCommand.php', 'Rector\\Console\\Command\\SetupCICommand' => __DIR__ . '/../..' . '/src/Console/Command/SetupCICommand.php', @@ -2649,9 +2650,9 @@ class ComposerStaticInit0408599c96f445821212bfb9798860ca public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit0408599c96f445821212bfb9798860ca::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit0408599c96f445821212bfb9798860ca::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit0408599c96f445821212bfb9798860ca::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/scoper-autoload.php b/vendor/scoper-autoload.php index 700c8aabb3e..7e6eeab05ab 100644 --- a/vendor/scoper-autoload.php +++ b/vendor/scoper-autoload.php @@ -30,7 +30,7 @@ if (!function_exists('humbug_phpscoper_expose_class')) { } } humbug_phpscoper_expose_class('AutoloadIncluder', 'RectorPrefix202401\AutoloadIncluder'); -humbug_phpscoper_expose_class('ComposerAutoloaderInit0408599c96f445821212bfb9798860ca', 'RectorPrefix202401\ComposerAutoloaderInit0408599c96f445821212bfb9798860ca'); +humbug_phpscoper_expose_class('ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3', 'RectorPrefix202401\ComposerAutoloaderInitf637847380e2ddf55dcae18dded1d2b3'); humbug_phpscoper_expose_class('Product', 'RectorPrefix202401\Product'); // Function aliases. For more information see: