Updated Rector to commit 0b5d2ed239b45cddcf4597eac75e23bad1e90cbf

0b5d2ed239 Make init part of process command to help new users with creating config (#3326)
This commit is contained in:
Tomas Votruba 2023-01-30 15:32:02 +00:00
parent dde7f41fda
commit 67f4bcd4f3
9 changed files with 136 additions and 148 deletions

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '0f025099cbdd8c4cc6c9a3ffa0775a9250f112de';
public const PACKAGE_VERSION = '0b5d2ed239b45cddcf4597eac75e23bad1e90cbf';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-01-29 23:29:51';
public const RELEASE_DATE = '2023-01-30 15:26:30';
/**
* @var int
*/

View File

@ -0,0 +1,102 @@
<?php
declare (strict_types=1);
namespace Rector\Core\Configuration;
use RectorPrefix202301\Nette\Utils\FileSystem;
use RectorPrefix202301\Nette\Utils\Strings;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\FileSystem\InitFilePathsResolver;
use Rector\Core\Php\PhpVersionProvider;
use Rector\PostRector\Contract\Rector\ComplementaryRectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use RectorPrefix202301\Symfony\Component\Console\Style\SymfonyStyle;
final class ConfigInitializer
{
/**
* @var RectorInterface[]
* @readonly
*/
private $rectors;
/**
* @readonly
* @var \Rector\Core\FileSystem\InitFilePathsResolver
*/
private $initFilePathsResolver;
/**
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $symfonyStyle;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @param RectorInterface[] $rectors
*/
public function __construct(array $rectors, InitFilePathsResolver $initFilePathsResolver, SymfonyStyle $symfonyStyle, PhpVersionProvider $phpVersionProvider)
{
$this->rectors = $rectors;
$this->initFilePathsResolver = $initFilePathsResolver;
$this->symfonyStyle = $symfonyStyle;
$this->phpVersionProvider = $phpVersionProvider;
}
public function createConfig(string $projectDirectory) : void
{
$commonRectorConfigPath = $projectDirectory . '/rector.php';
if (\file_exists($commonRectorConfigPath)) {
$this->symfonyStyle->warning('Register rules or sets in your "rector.php" config');
return;
}
$response = $this->symfonyStyle->ask('No "rector.php" config found. Should we generate it for you?', 'yes');
if ($response !== 'yes') {
// okay, nothing we can do
return;
}
$configContents = FileSystem::read(__DIR__ . '/../../templates/rector.php.dist');
$configContents = $this->replacePhpLevelContents($configContents);
$configContents = $this->replacePathsContents($configContents, $projectDirectory);
FileSystem::write($commonRectorConfigPath, $configContents);
$this->symfonyStyle->success('The config is added now. Re-run command to make Rector do the work!');
}
public function areSomeRectorsLoaded() : bool
{
$activeRectors = $this->filterActiveRectors($this->rectors);
return $activeRectors !== [];
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterActiveRectors(array $rectors) : array
{
return \array_filter($rectors, static function (RectorInterface $rector) : bool {
if ($rector instanceof PostRectorInterface) {
return \false;
}
return !$rector instanceof ComplementaryRectorInterface;
});
}
private function replacePhpLevelContents(string $rectorPhpTemplateContents) : string
{
$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = Strings::substring($fullPHPVersion, 0, 1) . Strings::substring($fullPHPVersion, 2, 1);
return \str_replace('LevelSetList::UP_TO_PHP_XY', 'LevelSetList::UP_TO_PHP_' . $phpVersion, $rectorPhpTemplateContents);
}
private function replacePathsContents(string $rectorPhpTemplateContents, string $projectDirectory) : string
{
$projectPhpDirectories = $this->initFilePathsResolver->resolve($projectDirectory);
// fallback to default 'src' in case of empty one
if ($projectPhpDirectories === []) {
$projectPhpDirectories[] = 'src';
}
$projectPhpDirectoriesContents = '';
foreach ($projectPhpDirectories as $projectPhpDirectory) {
$projectPhpDirectoriesContents .= " __DIR__ . '/" . $projectPhpDirectory . "'," . \PHP_EOL;
}
$projectPhpDirectoriesContents = \rtrim($projectPhpDirectoriesContents);
return \str_replace('__PATHS__', $projectPhpDirectoriesContents, $rectorPhpTemplateContents);
}
}

View File

@ -3,87 +3,33 @@
declare (strict_types=1);
namespace Rector\Core\Console\Command;
use RectorPrefix202301\Nette\Utils\FileSystem;
use RectorPrefix202301\Nette\Utils\Strings;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\FileSystem\InitFilePathsResolver;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Configuration\ConfigInitializer;
use RectorPrefix202301\Symfony\Component\Console\Command\Command;
use RectorPrefix202301\Symfony\Component\Console\Input\InputInterface;
use RectorPrefix202301\Symfony\Component\Console\Output\OutputInterface;
/**
* @deprecated Now part of the "process" command
*/
final class InitCommand extends Command
{
/**
* @var string
*/
private const TEMPLATE_PATH = __DIR__ . '/../../../templates/rector.php.dist';
/**
* @readonly
* @var \Symfony\Component\Filesystem\Filesystem
* @var \Rector\Core\Configuration\ConfigInitializer
*/
private $filesystem;
/**
* @readonly
* @var \Rector\Core\Contract\Console\OutputStyleInterface
*/
private $rectorOutputStyle;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @readonly
* @var \Rector\Core\FileSystem\InitFilePathsResolver
*/
private $initFilePathsResolver;
public function __construct(\RectorPrefix202301\Symfony\Component\Filesystem\Filesystem $filesystem, OutputStyleInterface $rectorOutputStyle, PhpVersionProvider $phpVersionProvider, InitFilePathsResolver $initFilePathsResolver)
private $configInitializer;
public function __construct(ConfigInitializer $configInitializer)
{
$this->filesystem = $filesystem;
$this->rectorOutputStyle = $rectorOutputStyle;
$this->phpVersionProvider = $phpVersionProvider;
$this->initFilePathsResolver = $initFilePathsResolver;
$this->configInitializer = $configInitializer;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$projectDirectory = \getcwd();
$rectorRootFilePath = $projectDirectory . '/rector.php';
$doesFileExist = $this->filesystem->exists($rectorRootFilePath);
if ($doesFileExist) {
$this->rectorOutputStyle->note('Config file "rector.php" already exists');
} else {
$rectorPhpTemplateContents = FileSystem::read(self::TEMPLATE_PATH);
$rectorPhpTemplateContents = $this->replacePhpLevelContents($rectorPhpTemplateContents);
$rectorPhpTemplateContents = $this->replacePathsContents($rectorPhpTemplateContents, $projectDirectory);
$this->filesystem->dumpFile($rectorRootFilePath, $rectorPhpTemplateContents);
$this->rectorOutputStyle->success('"rector.php" config file was added');
}
return Command::SUCCESS;
}
protected function configure() : void
{
$this->setName('init');
$this->setDescription('Generate rector.php configuration file');
}
private function replacePhpLevelContents(string $rectorPhpTemplateContents) : string
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = Strings::substring($fullPHPVersion, 0, 1) . Strings::substring($fullPHPVersion, 2, 1);
return \str_replace('LevelSetList::UP_TO_PHP_XY', 'LevelSetList::UP_TO_PHP_' . $phpVersion, $rectorPhpTemplateContents);
}
private function replacePathsContents(string $rectorPhpTemplateContents, string $projectDirectory) : string
{
$projectPhpDirectories = $this->initFilePathsResolver->resolve($projectDirectory);
// fallback to default 'src' in case of empty one
if ($projectPhpDirectories === []) {
$projectPhpDirectories[] = 'src';
}
$projectPhpDirectoriesContents = '';
foreach ($projectPhpDirectories as $projectPhpDirectory) {
$projectPhpDirectoriesContents .= " __DIR__ . '/" . $projectPhpDirectory . "'," . \PHP_EOL;
}
$projectPhpDirectoriesContents = \rtrim($projectPhpDirectoriesContents);
return \str_replace('__PATHS__', $projectPhpDirectoriesContents, $rectorPhpTemplateContents);
$this->configInitializer->createConfig(\getcwd());
return Command::SUCCESS;
}
}

View File

@ -7,12 +7,12 @@ use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Autoloading\AdditionalAutoloader;
use Rector\Core\Configuration\ConfigInitializer;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\ExitCode;
use Rector\Core\Console\Output\OutputFormatterCollector;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Reporting\MissingRectorRulesReporter;
use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Core\Util\MemoryLimiter;
use Rector\Core\Validation\EmptyConfigurableRectorChecker;
@ -36,9 +36,9 @@ final class ProcessCommand extends \Rector\Core\Console\Command\AbstractProcessC
private $changedFilesDetector;
/**
* @readonly
* @var \Rector\Core\Reporting\MissingRectorRulesReporter
* @var \Rector\Core\Configuration\ConfigInitializer
*/
private $missingRectorRulesReporter;
private $configInitializer;
/**
* @readonly
* @var \Rector\Core\Application\ApplicationFileProcessor
@ -74,11 +74,11 @@ final class ProcessCommand extends \Rector\Core\Console\Command\AbstractProcessC
* @var \Rector\Core\Util\MemoryLimiter
*/
private $memoryLimiter;
public function __construct(AdditionalAutoloader $additionalAutoloader, ChangedFilesDetector $changedFilesDetector, MissingRectorRulesReporter $missingRectorRulesReporter, ApplicationFileProcessor $applicationFileProcessor, ProcessResultFactory $processResultFactory, DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, EmptyConfigurableRectorChecker $emptyConfigurableRectorChecker, OutputFormatterCollector $outputFormatterCollector, OutputStyleInterface $rectorOutputStyle, MemoryLimiter $memoryLimiter)
public function __construct(AdditionalAutoloader $additionalAutoloader, ChangedFilesDetector $changedFilesDetector, ConfigInitializer $configInitializer, ApplicationFileProcessor $applicationFileProcessor, ProcessResultFactory $processResultFactory, DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, EmptyConfigurableRectorChecker $emptyConfigurableRectorChecker, OutputFormatterCollector $outputFormatterCollector, OutputStyleInterface $rectorOutputStyle, MemoryLimiter $memoryLimiter)
{
$this->additionalAutoloader = $additionalAutoloader;
$this->changedFilesDetector = $changedFilesDetector;
$this->missingRectorRulesReporter = $missingRectorRulesReporter;
$this->configInitializer = $configInitializer;
$this->applicationFileProcessor = $applicationFileProcessor;
$this->processResultFactory = $processResultFactory;
$this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator;
@ -96,9 +96,10 @@ final class ProcessCommand extends \Rector\Core\Console\Command\AbstractProcessC
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$exitCode = $this->missingRectorRulesReporter->reportIfMissing();
if ($exitCode !== null) {
return $exitCode;
// missing config? add it :)
if (!$this->configInitializer->areSomeRectorsLoaded()) {
$this->configInitializer->createConfig(\getcwd());
return self::SUCCESS;
}
$configuration = $this->configurationFactory->createFromInput($input);
$this->memoryLimiter->adjust($configuration);

View File

@ -1,61 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Core\Reporting;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\ComplementaryRectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use RectorPrefix202301\Symfony\Component\Console\Command\Command;
final class MissingRectorRulesReporter
{
/**
* @var RectorInterface[]
* @readonly
*/
private $rectors;
/**
* @readonly
* @var \Rector\Core\Contract\Console\OutputStyleInterface
*/
private $rectorOutputStyle;
/**
* @param RectorInterface[] $rectors
*/
public function __construct(array $rectors, OutputStyleInterface $rectorOutputStyle)
{
$this->rectors = $rectors;
$this->rectorOutputStyle = $rectorOutputStyle;
}
public function reportIfMissing() : ?int
{
if ($this->filterActiveRectors($this->rectors) !== []) {
return null;
}
$this->rectorOutputStyle->warning('We could not find any Rector rules to run. You have 2 options to add them:');
$this->rectorOutputStyle->title('1. Add single rule to "rector.php"');
$this->rectorOutputStyle->writeln(' $rectorConfig->rule(...);');
$this->rectorOutputStyle->newLine(1);
$this->rectorOutputStyle->title('2. Add set of rules to "rector.php"');
$this->rectorOutputStyle->writeln(' $rectorConfig->sets([SetList::...]);');
$this->rectorOutputStyle->newLine(1);
$this->rectorOutputStyle->title('Missing "rector.php" in your project? Let Rector create it for you');
$this->rectorOutputStyle->writeln(' vendor/bin/rector init');
$this->rectorOutputStyle->newLine();
return Command::FAILURE;
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterActiveRectors(array $rectors) : array
{
return \array_filter($rectors, static function (RectorInterface $rector) : bool {
if ($rector instanceof PostRectorInterface) {
return \false;
}
return !$rector instanceof ComplementaryRectorInterface;
});
}
}

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit91d4267ba4c606b22aa047e2c0b388c5::getLoader();
return ComposerAutoloaderInit8079f62218567f082555ac55acf69f28::getLoader();

View File

@ -1344,6 +1344,7 @@ return array(
'Rector\\Core\\Bootstrap\\ExtensionConfigResolver' => $baseDir . '/src/Bootstrap/ExtensionConfigResolver.php',
'Rector\\Core\\Bootstrap\\RectorConfigsResolver' => $baseDir . '/src/Bootstrap/RectorConfigsResolver.php',
'Rector\\Core\\Config\\Loader\\ConfigureCallMergingLoaderFactory' => $baseDir . '/src/Config/Loader/ConfigureCallMergingLoaderFactory.php',
'Rector\\Core\\Configuration\\ConfigInitializer' => $baseDir . '/src/Configuration/ConfigInitializer.php',
'Rector\\Core\\Configuration\\ConfigurationFactory' => $baseDir . '/src/Configuration/ConfigurationFactory.php',
'Rector\\Core\\Configuration\\CurrentNodeProvider' => $baseDir . '/src/Configuration/CurrentNodeProvider.php',
'Rector\\Core\\Configuration\\Option' => $baseDir . '/src/Configuration/Option.php',
@ -1496,7 +1497,6 @@ return array(
'Rector\\Core\\Rector\\AbstractRector' => $baseDir . '/src/Rector/AbstractRector.php',
'Rector\\Core\\Rector\\AbstractScopeAwareRector' => $baseDir . '/src/Rector/AbstractScopeAwareRector.php',
'Rector\\Core\\Reflection\\ReflectionResolver' => $baseDir . '/src/Reflection/ReflectionResolver.php',
'Rector\\Core\\Reporting\\MissingRectorRulesReporter' => $baseDir . '/src/Reporting/MissingRectorRulesReporter.php',
'Rector\\Core\\StaticReflection\\DynamicSourceLocatorDecorator' => $baseDir . '/src/StaticReflection/DynamicSourceLocatorDecorator.php',
'Rector\\Core\\StaticReflection\\SourceLocator\\RenamedClassesSourceLocator' => $baseDir . '/src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php',
'Rector\\Core\\Util\\ArrayChecker' => $baseDir . '/src/Util/ArrayChecker.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit91d4267ba4c606b22aa047e2c0b388c5
class ComposerAutoloaderInit8079f62218567f082555ac55acf69f28
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit91d4267ba4c606b22aa047e2c0b388c5
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit91d4267ba4c606b22aa047e2c0b388c5', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit8079f62218567f082555ac55acf69f28', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit91d4267ba4c606b22aa047e2c0b388c5', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit8079f62218567f082555ac55acf69f28', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit8079f62218567f082555ac55acf69f28::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit8079f62218567f082555ac55acf69f28::$files;
$requireFile = static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5
class ComposerStaticInit8079f62218567f082555ac55acf69f28
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1589,6 +1589,7 @@ class ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5
'Rector\\Core\\Bootstrap\\ExtensionConfigResolver' => __DIR__ . '/../..' . '/src/Bootstrap/ExtensionConfigResolver.php',
'Rector\\Core\\Bootstrap\\RectorConfigsResolver' => __DIR__ . '/../..' . '/src/Bootstrap/RectorConfigsResolver.php',
'Rector\\Core\\Config\\Loader\\ConfigureCallMergingLoaderFactory' => __DIR__ . '/../..' . '/src/Config/Loader/ConfigureCallMergingLoaderFactory.php',
'Rector\\Core\\Configuration\\ConfigInitializer' => __DIR__ . '/../..' . '/src/Configuration/ConfigInitializer.php',
'Rector\\Core\\Configuration\\ConfigurationFactory' => __DIR__ . '/../..' . '/src/Configuration/ConfigurationFactory.php',
'Rector\\Core\\Configuration\\CurrentNodeProvider' => __DIR__ . '/../..' . '/src/Configuration/CurrentNodeProvider.php',
'Rector\\Core\\Configuration\\Option' => __DIR__ . '/../..' . '/src/Configuration/Option.php',
@ -1741,7 +1742,6 @@ class ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5
'Rector\\Core\\Rector\\AbstractRector' => __DIR__ . '/../..' . '/src/Rector/AbstractRector.php',
'Rector\\Core\\Rector\\AbstractScopeAwareRector' => __DIR__ . '/../..' . '/src/Rector/AbstractScopeAwareRector.php',
'Rector\\Core\\Reflection\\ReflectionResolver' => __DIR__ . '/../..' . '/src/Reflection/ReflectionResolver.php',
'Rector\\Core\\Reporting\\MissingRectorRulesReporter' => __DIR__ . '/../..' . '/src/Reporting/MissingRectorRulesReporter.php',
'Rector\\Core\\StaticReflection\\DynamicSourceLocatorDecorator' => __DIR__ . '/../..' . '/src/StaticReflection/DynamicSourceLocatorDecorator.php',
'Rector\\Core\\StaticReflection\\SourceLocator\\RenamedClassesSourceLocator' => __DIR__ . '/../..' . '/src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php',
'Rector\\Core\\Util\\ArrayChecker' => __DIR__ . '/../..' . '/src/Util/ArrayChecker.php',
@ -3082,9 +3082,9 @@ class ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit91d4267ba4c606b22aa047e2c0b388c5::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit8079f62218567f082555ac55acf69f28::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8079f62218567f082555ac55acf69f28::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8079f62218567f082555ac55acf69f28::$classMap;
}, null, ClassLoader::class);
}