2021-06-22 19:34:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2024-01-02 02:40:38 +00:00
|
|
|
namespace Rector\Configuration;
|
2021-06-22 19:34:44 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Configuration\Parameter\SimpleParameterProvider;
|
|
|
|
use Rector\ValueObject\Configuration;
|
2024-06-01 09:25:16 +00:00
|
|
|
use RectorPrefix202406\Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use RectorPrefix202406\Symfony\Component\Console\Style\SymfonyStyle;
|
2023-09-11 18:33:42 +00:00
|
|
|
/**
|
2024-01-01 09:05:02 +00:00
|
|
|
* @see \Rector\Tests\Configuration\ConfigurationFactoryTest
|
2023-09-11 18:33:42 +00:00
|
|
|
*/
|
2021-06-22 19:34:44 +00:00
|
|
|
final class ConfigurationFactory
|
|
|
|
{
|
2021-07-02 20:55:14 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-08-07 21:49:15 +00:00
|
|
|
* @var \Symfony\Component\Console\Style\SymfonyStyle
|
2021-07-02 20:55:14 +00:00
|
|
|
*/
|
2023-08-07 21:49:15 +00:00
|
|
|
private $symfonyStyle;
|
|
|
|
public function __construct(SymfonyStyle $symfonyStyle)
|
2021-06-22 19:34:44 +00:00
|
|
|
{
|
2023-08-07 21:49:15 +00:00
|
|
|
$this->symfonyStyle = $symfonyStyle;
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|
2021-12-28 12:08:43 +00:00
|
|
|
/**
|
2022-12-23 17:10:25 +00:00
|
|
|
* @api used in tests
|
2021-12-28 12:08:43 +00:00
|
|
|
* @param string[] $paths
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createForTests(array $paths) : Configuration
|
2021-06-23 14:44:39 +00:00
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$fileExtensions = SimpleParameterProvider::provideArrayParameter(\Rector\Configuration\Option::FILE_EXTENSIONS);
|
2024-01-15 22:10:40 +00:00
|
|
|
return new Configuration(\false, \true, \false, ConsoleOutputFormatter::NAME, $fileExtensions, $paths, \true, null, null, \false, null, \false);
|
2021-06-23 14:44:39 +00:00
|
|
|
}
|
2021-06-22 19:34:44 +00:00
|
|
|
/**
|
|
|
|
* Needs to run in the start of the life cycle, since the rest of workflow uses it.
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createFromInput(InputInterface $input) : Configuration
|
2021-06-22 19:34:44 +00:00
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$isDryRun = (bool) $input->getOption(\Rector\Configuration\Option::DRY_RUN);
|
|
|
|
$shouldClearCache = (bool) $input->getOption(\Rector\Configuration\Option::CLEAR_CACHE);
|
|
|
|
$outputFormat = (string) $input->getOption(\Rector\Configuration\Option::OUTPUT_FORMAT);
|
2021-06-22 19:34:44 +00:00
|
|
|
$showProgressBar = $this->shouldShowProgressBar($input, $outputFormat);
|
2023-04-10 13:55:06 +00:00
|
|
|
$showDiffs = $this->shouldShowDiffs($input);
|
2021-06-22 19:34:44 +00:00
|
|
|
$paths = $this->resolvePaths($input);
|
2024-01-02 02:40:38 +00:00
|
|
|
$fileExtensions = SimpleParameterProvider::provideArrayParameter(\Rector\Configuration\Option::FILE_EXTENSIONS);
|
|
|
|
$isParallel = SimpleParameterProvider::provideBoolParameter(\Rector\Configuration\Option::PARALLEL);
|
|
|
|
$parallelPort = (string) $input->getOption(\Rector\Configuration\Option::PARALLEL_PORT);
|
|
|
|
$parallelIdentifier = (string) $input->getOption(\Rector\Configuration\Option::PARALLEL_IDENTIFIER);
|
|
|
|
$isDebug = (bool) $input->getOption(\Rector\Configuration\Option::DEBUG);
|
2024-05-20 06:49:34 +00:00
|
|
|
// using debug disables parallel, so emitting exception is straightforward and easier to debug
|
|
|
|
if ($isDebug) {
|
|
|
|
$isParallel = \false;
|
|
|
|
}
|
2023-04-10 14:35:48 +00:00
|
|
|
$memoryLimit = $this->resolveMemoryLimit($input);
|
2024-01-15 22:10:40 +00:00
|
|
|
return new Configuration($isDryRun, $showProgressBar, $shouldClearCache, $outputFormat, $fileExtensions, $paths, $showDiffs, $parallelPort, $parallelIdentifier, $isParallel, $memoryLimit, $isDebug);
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldShowProgressBar(InputInterface $input, string $outputFormat) : bool
|
2021-06-22 19:34:44 +00:00
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$noProgressBar = (bool) $input->getOption(\Rector\Configuration\Option::NO_PROGRESS_BAR);
|
2021-06-22 19:34:44 +00:00
|
|
|
if ($noProgressBar) {
|
|
|
|
return \false;
|
|
|
|
}
|
2023-08-07 21:49:15 +00:00
|
|
|
if ($this->symfonyStyle->isVerbose()) {
|
2021-07-02 20:55:14 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $outputFormat === ConsoleOutputFormatter::NAME;
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|
2023-04-10 13:55:06 +00:00
|
|
|
private function shouldShowDiffs(InputInterface $input) : bool
|
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$noDiffs = (bool) $input->getOption(\Rector\Configuration\Option::NO_DIFFS);
|
2023-04-10 13:55:06 +00:00
|
|
|
if ($noDiffs) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
// fallback to parameter
|
2024-01-02 02:40:38 +00:00
|
|
|
return !SimpleParameterProvider::provideBoolParameter(\Rector\Configuration\Option::NO_DIFFS, \false);
|
2023-04-10 13:55:06 +00:00
|
|
|
}
|
2021-06-22 19:34:44 +00:00
|
|
|
/**
|
|
|
|
* @return string[]|mixed[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolvePaths(InputInterface $input) : array
|
2021-06-22 19:34:44 +00:00
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$commandLinePaths = (array) $input->getArgument(\Rector\Configuration\Option::SOURCE);
|
2023-07-20 15:05:47 +00:00
|
|
|
// give priority to command line
|
2021-06-22 19:34:44 +00:00
|
|
|
if ($commandLinePaths !== []) {
|
2023-07-20 15:05:47 +00:00
|
|
|
return $commandLinePaths;
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|
|
|
|
// fallback to parameter
|
2024-01-02 02:40:38 +00:00
|
|
|
return SimpleParameterProvider::provideArrayParameter(\Rector\Configuration\Option::PATHS);
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|
2023-04-10 14:35:48 +00:00
|
|
|
private function resolveMemoryLimit(InputInterface $input) : ?string
|
|
|
|
{
|
2024-01-02 02:40:38 +00:00
|
|
|
$memoryLimit = $input->getOption(\Rector\Configuration\Option::MEMORY_LIMIT);
|
2023-04-10 14:35:48 +00:00
|
|
|
if ($memoryLimit !== null) {
|
|
|
|
return (string) $memoryLimit;
|
|
|
|
}
|
2024-01-02 02:40:38 +00:00
|
|
|
if (!SimpleParameterProvider::hasParameter(\Rector\Configuration\Option::MEMORY_LIMIT)) {
|
2023-04-10 14:35:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-01-02 02:40:38 +00:00
|
|
|
return SimpleParameterProvider::provideStringParameter(\Rector\Configuration\Option::MEMORY_LIMIT);
|
2023-04-10 14:35:48 +00:00
|
|
|
}
|
2021-06-22 19:34:44 +00:00
|
|
|
}
|