Skip to content

Commit d8960be

Browse files
authored
Resolve bootstrap CLI options without symfony/console ArgvInput (#9027)
1 parent 02f15c4 commit d8960be

6 files changed

Lines changed: 96 additions & 30 deletions

File tree

‎src/Bootstrap/AutoloadFileParameterResolver.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Rector\Configuration\Option;
88
use Rector\Configuration\Parameter\SimpleParameterProvider;
9-
use Symfony\Component\Console\Input\ArgvInput;
109

1110
/**
1211
* A different extra autoload changes what PHPStan can resolve, so cached results
@@ -27,9 +26,8 @@ public static function resolveFromArgv(array $argv): void
2726
// handles "--autoload-file path", "--autoload-file=path" and "-a path";
2827
// parallel workers receive the space-separated form, so all spellings must
2928
// normalize to the same value or main process and workers would disagree
30-
$autoloadFile = new ArgvInput($argv)
31-
->getParameterOption(['--autoload-file', '-a'], null);
32-
if (! is_string($autoloadFile) || $autoloadFile === '') {
29+
$autoloadFile = CliOptionValueResolver::resolve($argv, ['--autoload-file', '-a']);
30+
if ($autoloadFile === null || $autoloadFile === '') {
3331
return;
3432
}
3533

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Bootstrap;
6+
7+
/**
8+
* Reads an option value from raw argv, used during bootstrap before the console exists.
9+
*
10+
* @see \Rector\Tests\Bootstrap\CliOptionValueResolverTest
11+
*/
12+
final class CliOptionValueResolver
13+
{
14+
private const string END_OF_OPTIONS = '--';
15+
16+
/**
17+
* Resolve the value of "--name value", "--name=value", "-c value" or "-c=value" from raw argv.
18+
*
19+
* @param array<int, mixed> $argv
20+
* @param string[] $optionNames fully dashed, e.g. ['--config', '-c']
21+
*/
22+
public static function resolve(array $argv, array $optionNames): ?string
23+
{
24+
foreach ($argv as $index => $arg) {
25+
if (! is_string($arg)) {
26+
continue;
27+
}
28+
29+
// everything after "--" is a positional argument
30+
if ($arg === self::END_OF_OPTIONS) {
31+
return null;
32+
}
33+
34+
foreach ($optionNames as $optionName) {
35+
if ($arg === $optionName) {
36+
$nextArg = $argv[$index + 1] ?? null;
37+
return is_string($nextArg) ? $nextArg : null;
38+
}
39+
40+
if (str_starts_with($arg, $optionName . '=')) {
41+
return substr($arg, strlen($optionName) + 1);
42+
}
43+
}
44+
}
45+
46+
return null;
47+
}
48+
}

‎src/Bootstrap/RectorConfigsResolver.php‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Rector\Bootstrap;
66

77
use Rector\ValueObject\Bootstrap\BootstrapConfigs;
8-
use Symfony\Component\Console\Input\ArgvInput;
98
use Webmozart\Assert\Assert;
109

1110
final class RectorConfigsResolver
@@ -16,15 +15,14 @@ final class RectorConfigsResolver
1615

1716
public function provide(): BootstrapConfigs
1817
{
19-
$argvInput = new ArgvInput();
20-
$mainConfigFile = $this->resolveFromInputWithFallback($argvInput);
18+
$mainConfigFile = $this->resolveFromInputWithFallback();
2119

2220
return new BootstrapConfigs($mainConfigFile, []);
2321
}
2422

25-
private function resolveFromInput(ArgvInput $argvInput): ?string
23+
private function resolveFromInput(): ?string
2624
{
27-
$configFile = $this->getOptionValue($argvInput, ['--config', '-c']);
25+
$configFile = CliOptionValueResolver::resolve($_SERVER['argv'] ?? [], ['--config', '-c']);
2826
if ($configFile === null) {
2927
return null;
3028
}
@@ -34,9 +32,9 @@ private function resolveFromInput(ArgvInput $argvInput): ?string
3432
return realpath($configFile);
3533
}
3634

37-
private function resolveFromInputWithFallback(ArgvInput $argvInput): ?string
35+
private function resolveFromInputWithFallback(): ?string
3836
{
39-
$configFile = $this->resolveFromInput($argvInput);
37+
$configFile = $this->resolveFromInput();
4038
if ($configFile !== null) {
4139
return $configFile;
4240
}
@@ -60,17 +58,4 @@ private function createFallbackFileInfoIfFound(string $fallbackFile): ?string
6058
return $rootFallbackFile;
6159
}
6260

63-
/**
64-
* @param string[] $optionNames
65-
*/
66-
private function getOptionValue(ArgvInput $argvInput, array $optionNames): ?string
67-
{
68-
foreach ($optionNames as $optionName) {
69-
if ($argvInput->hasParameterOption($optionName, true)) {
70-
return $argvInput->getParameterOption($optionName, null, true);
71-
}
72-
}
73-
74-
return null;
75-
}
7661
}

‎src/Console/ExitCode.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Rector\Console;
66

7-
use Symfony\Component\Console\Command\Command;
8-
97
final class ExitCode
108
{
11-
public const int SUCCESS = Command::SUCCESS;
9+
public const int SUCCESS = 0;
1210

13-
public const int FAILURE = Command::FAILURE;
11+
public const int FAILURE = 1;
1412

1513
public const int CHANGED_CODE = 2;
1614
}

‎src/Parallel/Application/ParallelFileProcessor.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\Configuration\Option;
1414
use Rector\Configuration\Parameter\SimpleParameterProvider;
1515
use Rector\Console\Command\ProcessCommand;
16+
use Rector\Console\ExitCode;
1617
use Rector\Parallel\Command\WorkerCommandLineFactory;
1718
use Rector\Parallel\Enum\Action;
1819
use Rector\Parallel\Enum\Content;
@@ -25,7 +26,6 @@
2526
use Rector\Parallel\ValueObject\Schedule;
2627
use Rector\ValueObject\Error\SystemError;
2728
use Rector\ValueObject\ProcessResult;
28-
use Symfony\Component\Console\Command\Command;
2929
use Symfony\Component\Console\Input\InputInterface;
3030
use Throwable;
3131

@@ -245,7 +245,7 @@ function (array $json) use (
245245
// 3. callable on exit
246246
function ($exitCode, string $stdErr) use ($parallelResultCollector, $processIdentifier): void {
247247
$this->processPool->tryQuitProcess($processIdentifier);
248-
if ($exitCode === Command::SUCCESS) {
248+
if ($exitCode === ExitCode::SUCCESS) {
249249
return;
250250
}
251251

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Bootstrap;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
use Rector\Bootstrap\CliOptionValueResolver;
10+
11+
final class CliOptionValueResolverTest extends TestCase
12+
{
13+
/**
14+
* @param array<int, mixed> $argv
15+
*/
16+
#[DataProvider('provideData')]
17+
public function test(array $argv, ?string $expectedValue): void
18+
{
19+
$this->assertSame($expectedValue, CliOptionValueResolver::resolve($argv, ['--config', '-c']));
20+
}
21+
22+
/**
23+
* @return iterable<string, array{array<int, mixed>, string|null}>
24+
*/
25+
public static function provideData(): iterable
26+
{
27+
yield 'long with space' => [['bin/rector', 'process', '--config', 'rector.php'], 'rector.php'];
28+
yield 'long with equals' => [['bin/rector', 'process', '--config=rector.php'], 'rector.php'];
29+
yield 'short with space' => [['bin/rector', 'process', '-c', 'rector.php'], 'rector.php'];
30+
yield 'short with equals' => [['bin/rector', 'process', '-c=rector.php'], 'rector.php'];
31+
yield 'first occurrence wins' => [['bin/rector', '-c', 'first.php', '--config', 'second.php'], 'first.php'];
32+
yield 'missing value' => [['bin/rector', 'process', '--config'], null];
33+
yield 'missing option' => [['bin/rector', 'process', '--dry-run'], null];
34+
yield 'similar prefix is not matched' => [['bin/rector', 'process', '--configuration', 'rector.php'], null];
35+
yield 'after end of options' => [['bin/rector', 'process', '--', '--config', 'rector.php'], null];
36+
}
37+
}

0 commit comments

Comments
 (0)