Skip to content

Commit cbe736a

Browse files
authored
Type hint RectorStyle instead of SymfonyStyle in services (#9030)
1 parent d8960be commit cbe736a

17 files changed

Lines changed: 123 additions & 107 deletions

‎src/Application/ApplicationFileProcessor.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rector\Caching\Detector\ChangedFilesDetector;
1111
use Rector\Configuration\Option;
1212
use Rector\Configuration\Parameter\SimpleParameterProvider;
13+
use Rector\Console\Style\RectorStyle;
1314
use Rector\FileSystem\FilesFinder;
1415
use Rector\Parallel\Application\ParallelFileProcessor;
1516
use Rector\Parallel\CpuCoreCountProvider;
@@ -27,7 +28,6 @@
2728
use Rector\ValueObject\ProcessResult;
2829
use Rector\ValueObject\Reporting\FileDiff;
2930
use Symfony\Component\Console\Input\InputInterface;
30-
use Symfony\Component\Console\Style\SymfonyStyle;
3131
use Throwable;
3232

3333
final class ApplicationFileProcessor
@@ -40,7 +40,7 @@ final class ApplicationFileProcessor
4040
private array $systemErrors = [];
4141

4242
public function __construct(
43-
private readonly SymfonyStyle $symfonyStyle,
43+
private readonly RectorStyle $rectorStyle,
4444
private readonly FilesFinder $filesFinder,
4545
private readonly ParallelFileProcessor $parallelFileProcessor,
4646
private readonly ScheduleFactory $scheduleFactory,
@@ -76,11 +76,11 @@ public function run(Configuration $configuration, InputInterface $input): Proces
7676
*/
7777
if ($configuration->shouldShowProgressBar()) {
7878
$fileCount = count($filePaths);
79-
$this->symfonyStyle->progressStart($fileCount);
80-
$this->symfonyStyle->progressAdvance(0);
79+
$this->rectorStyle->progressStart($fileCount);
80+
$this->rectorStyle->progressAdvance(0);
8181

8282
$postFileCallback = function (int $stepCount): void {
83-
$this->symfonyStyle->progressAdvance($stepCount);
83+
$this->rectorStyle->progressAdvance($stepCount);
8484
// running in parallel here → nothing else to do
8585
};
8686
} else {
@@ -90,7 +90,7 @@ public function run(Configuration $configuration, InputInterface $input): Proces
9090

9191
if ($configuration->isDebug()) {
9292
$preFileCallback = function (string $filePath): void {
93-
$this->symfonyStyle->writeln('[file] ' . $filePath);
93+
$this->rectorStyle->writeln('[file] ' . $filePath);
9494
};
9595
} else {
9696
$preFileCallback = null;
@@ -212,7 +212,7 @@ private function resolveSystemError(Throwable $throwable, string $filePath): Sys
212212
{
213213
$errorMessage = sprintf('System error: "%s"', $throwable->getMessage()) . PHP_EOL;
214214

215-
if ($this->symfonyStyle->isDebug()) {
215+
if ($this->rectorStyle->isDebug()) {
216216
$errorMessage .= PHP_EOL . 'Stack trace:' . PHP_EOL . $throwable->getTraceAsString();
217217
} else {
218218
$errorMessage .= 'Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new';

‎src/Application/FileProcessor.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
1414
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
1515
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
16+
use Rector\Console\Style\RectorStyle;
1617
use Rector\Exception\ShouldNotHappenException;
1718
use Rector\FileSystem\FilePathHelper;
1819
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
@@ -27,15 +28,14 @@
2728
use Rector\ValueObject\Configuration;
2829
use Rector\ValueObject\Error\SystemError;
2930
use Rector\ValueObject\FileProcessResult;
30-
use Symfony\Component\Console\Style\SymfonyStyle;
3131
use Throwable;
3232

3333
final readonly class FileProcessor
3434
{
3535
public function __construct(
3636
private BetterStandardPrinter $betterStandardPrinter,
3737
private RectorNodeTraverser $rectorNodeTraverser,
38-
private SymfonyStyle $symfonyStyle,
38+
private RectorStyle $rectorStyle,
3939
private FileDiffFactory $fileDiffFactory,
4040
private ChangedFilesDetector $changedFilesDetector,
4141
private ErrorFactory $errorFactory,
@@ -151,7 +151,7 @@ private function parseFileAndDecorateNodes(File $file): ?SystemError
151151

152152
return $this->errorFactory->createAutoloadError($analysedCodeException, $file->getFilePath());
153153
} catch (Throwable $throwable) {
154-
if ($this->symfonyStyle->isVerbose() || StaticPHPUnitEnvironment::isPHPUnitRun()) {
154+
if ($this->rectorStyle->isVerbose() || StaticPHPUnitEnvironment::isPHPUnitRun()) {
155155
throw $throwable;
156156
}
157157

‎src/ChangesReporting/Output/ConsoleOutputFormatter.php‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
99
use Rector\Configuration\Option;
1010
use Rector\Configuration\Parameter\SimpleParameterProvider;
11+
use Rector\Console\Style\RectorStyle;
1112
use Rector\ValueObject\Configuration;
1213
use Rector\ValueObject\Error\SystemError;
1314
use Rector\ValueObject\ProcessResult;
1415
use Rector\ValueObject\Reporting\FileDiff;
1516
use Symfony\Component\Console\Formatter\OutputFormatter;
16-
use Symfony\Component\Console\Style\SymfonyStyle;
1717

1818
final readonly class ConsoleOutputFormatter implements OutputFormatterInterface
1919
{
@@ -25,7 +25,7 @@
2525
private const string ON_LINE_REGEX = '# on line #';
2626

2727
public function __construct(
28-
private SymfonyStyle $symfonyStyle,
28+
private RectorStyle $rectorStyle,
2929
) {
3030
}
3131

@@ -43,15 +43,15 @@ public function report(ProcessResult $processResult, Configuration $configuratio
4343

4444
// to keep space between progress bar and success message
4545
if ($configuration->shouldShowProgressBar() && $processResult->getFileDiffs() === []) {
46-
$this->symfonyStyle->newLine();
46+
$this->rectorStyle->newLine();
4747
}
4848

4949
if ($configuration->shouldShowRulesSummary()) {
5050
$this->reportRulesSummary($processResult, $configuration);
5151
}
5252

5353
$message = $this->createSuccessMessage($processResult, $configuration);
54-
$this->symfonyStyle->success($message);
54+
$this->rectorStyle->success($message);
5555
}
5656

5757
public function getName(): string
@@ -72,7 +72,7 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void
7272
ksort($fileDiffs);
7373
$message = sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's');
7474

75-
$this->symfonyStyle->title($message);
75+
$this->rectorStyle->title($message);
7676

7777
$i = 0;
7878
foreach ($fileDiffs as $fileDiff) {
@@ -95,14 +95,14 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void
9595

9696
$message = sprintf('<options=bold>%d) %s</>', ++$i, $filePathWithUrl);
9797

98-
$this->symfonyStyle->writeln($message);
99-
$this->symfonyStyle->newLine();
100-
$this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted());
98+
$this->rectorStyle->writeln($message);
99+
$this->rectorStyle->newLine();
100+
$this->rectorStyle->writeln($fileDiff->getDiffConsoleFormatted());
101101

102102
if ($fileDiff->getRectorChanges() !== []) {
103-
$this->symfonyStyle->writeln('<options=underscore>Applied rules:</>');
104-
$this->symfonyStyle->listing($fileDiff->getRectorShortClasses());
105-
$this->symfonyStyle->newLine();
103+
$this->rectorStyle->writeln('<options=underscore>Applied rules:</>');
104+
$this->rectorStyle->listing($fileDiff->getRectorShortClasses());
105+
$this->rectorStyle->newLine();
106106
}
107107
}
108108
}
@@ -131,7 +131,7 @@ private function reportErrors(array $errors, bool $absoluteFilePath): void
131131
$message .= ' On line: ' . $error->getLine();
132132
}
133133

134-
$this->symfonyStyle->error($message);
134+
$this->rectorStyle->error($message);
135135
}
136136
}
137137

@@ -144,11 +144,11 @@ private function reportRulesSummary(ProcessResult $processResult, Configuration
144144

145145
$verb = $configuration->isDryRun() ? 'would have been applied' : 'was applied';
146146

147-
$this->symfonyStyle->section('Rules Summary');
147+
$this->rectorStyle->section('Rules Summary');
148148

149149
foreach ($ruleApplicationCounts as $ruleClass => $count) {
150150
$ruleShortClass = (string) Strings::after($ruleClass, '\\', -1);
151-
$this->symfonyStyle->writeln(sprintf(
151+
$this->rectorStyle->writeln(sprintf(
152152
' * <info>%s</info> %s <comment>%d</comment> time%s',
153153
$ruleShortClass,
154154
$verb,
@@ -157,7 +157,7 @@ private function reportRulesSummary(ProcessResult $processResult, Configuration
157157
));
158158
}
159159

160-
$this->symfonyStyle->newLine();
160+
$this->rectorStyle->newLine();
161161
}
162162

163163
private function normalizePathsToRelativeWithLine(string $errorMessage): string

‎src/Configuration/ConfigInitializer.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Nette\Utils\FileSystem;
88
use Rector\Agentic\TerminalDetector;
99
use Rector\Bootstrap\RectorConfigsResolver;
10+
use Rector\Console\Style\RectorStyle;
1011
use Rector\Contract\Rector\RectorInterface;
1112
use Rector\FileSystem\InitFilePathsResolver;
1213
use Rector\PostRector\Contract\Rector\PostRectorInterface;
13-
use Symfony\Component\Console\Style\SymfonyStyle;
1414

1515
final readonly class ConfigInitializer
1616
{
@@ -20,7 +20,7 @@
2020
public function __construct(
2121
private array $rectors,
2222
private InitFilePathsResolver $initFilePathsResolver,
23-
private SymfonyStyle $symfonyStyle,
23+
private RectorStyle $rectorStyle,
2424
private RectorConfigsResolver $rectorConfigsResolver,
2525
) {
2626
}
@@ -31,14 +31,14 @@ public function createConfig(string $projectDirectory): void
3131
$distRectorConfigPath = $projectDirectory . '/' . RectorConfigsResolver::DEFAULT_DIST_CONFIG_FILE;
3232

3333
if (file_exists($commonRectorConfigPath)) {
34-
$this->symfonyStyle->warning(
34+
$this->rectorStyle->warning(
3535
'Register rules or sets in your "' . RectorConfigsResolver::DEFAULT_CONFIG_FILE . '" config'
3636
);
3737
return;
3838
}
3939

4040
if (file_exists($distRectorConfigPath)) {
41-
$this->symfonyStyle->warning(
41+
$this->rectorStyle->warning(
4242
'Register rules or sets in your "' . RectorConfigsResolver::DEFAULT_DIST_CONFIG_FILE . '" config'
4343
);
4444
return;
@@ -47,7 +47,7 @@ public function createConfig(string $projectDirectory): void
4747
$mainConfigFile = $this->rectorConfigsResolver->provide()
4848
->getMainConfigFile();
4949
if ($mainConfigFile !== null && file_exists($mainConfigFile)) {
50-
$this->symfonyStyle->warning(
50+
$this->rectorStyle->warning(
5151
'Register rules or sets in your "' . basename($mainConfigFile) . '" config'
5252
);
5353
return;
@@ -56,14 +56,14 @@ public function createConfig(string $projectDirectory): void
5656
// non-interactive terminal, e.g. piped output, CI or an agent: never prompt or silently write a
5757
// config, just say what to do - Symfony still treats a closed STDIN as interactive here
5858
if (! TerminalDetector::isInputTty()) {
59-
$this->symfonyStyle->warning(sprintf(
59+
$this->rectorStyle->warning(sprintf(
6060
'No "%s" config found. Create one, or pass "--config <path>".',
6161
RectorConfigsResolver::DEFAULT_CONFIG_FILE
6262
));
6363
return;
6464
}
6565

66-
$response = $this->symfonyStyle->ask(
66+
$response = $this->rectorStyle->ask(
6767
'No "' . RectorConfigsResolver::DEFAULT_CONFIG_FILE . '" config found. Should we generate it for you?',
6868
'yes'
6969
);
@@ -77,7 +77,7 @@ public function createConfig(string $projectDirectory): void
7777
$configContents = $this->replacePathsContents($configContents, $projectDirectory);
7878

7979
FileSystem::write($commonRectorConfigPath, $configContents, null);
80-
$this->symfonyStyle->success('The config is added now. Re-run command to make Rector do the work!');
80+
$this->rectorStyle->success('The config is added now. Re-run command to make Rector do the work!');
8181
}
8282

8383
public function areSomeRectorsLoaded(): bool

‎src/Configuration/ConfigurationFactory.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Rector\Agentic\TerminalDetector;
88
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
99
use Rector\Configuration\Parameter\SimpleParameterProvider;
10+
use Rector\Console\Style\RectorStyle;
1011
use Rector\FileSystem\FilePathFilter;
1112
use Rector\ValueObject\Configuration;
1213
use Symfony\Component\Console\Input\InputInterface;
13-
use Symfony\Component\Console\Style\SymfonyStyle;
1414
use Webmozart\Assert\Assert;
1515

1616
/**
@@ -19,7 +19,7 @@
1919
final readonly class ConfigurationFactory
2020
{
2121
public function __construct(
22-
private SymfonyStyle $symfonyStyle,
22+
private RectorStyle $rectorStyle,
2323
private OnlyRuleResolver $onlyRuleResolver,
2424
private FilePathFilter $filePathFilter,
2525
) {
@@ -76,7 +76,7 @@ public function createFromInput(InputInterface $input): Configuration
7676

7777
$onlySuffix = $input->getOption(Option::ONLY_SUFFIX);
7878
if ($onlySuffix !== null) {
79-
$this->symfonyStyle->warning(
79+
$this->rectorStyle->warning(
8080
'The "--only-suffix" option is deprecated and will be removed. Use "--filter" instead, e.g. --filter="*Controller.php"'
8181
);
8282
}
@@ -179,7 +179,7 @@ private function shouldShowProgressBar(InputInterface $input, string $outputForm
179179
return false;
180180
}
181181

182-
if ($this->symfonyStyle->isVerbose()) {
182+
if ($this->rectorStyle->isVerbose()) {
183183
return false;
184184
}
185185

‎src/Console/Command/ComposerBasedCommand.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\Composer\InstalledPackageResolver;
1010
use Rector\Configuration\Option;
1111
use Rector\Configuration\Parameter\SimpleParameterProvider;
12+
use Rector\Console\Style\RectorStyle;
1213
use Rector\Contract\Rector\RectorInterface;
1314
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
1415
use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration;
@@ -18,7 +19,6 @@
1819
use Symfony\Component\Console\Helper\TableSeparator;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\OutputInterface;
21-
use Symfony\Component\Console\Style\SymfonyStyle;
2222

2323
/**
2424
* @see \Rector\Tests\Console\Command\ComposerBasedCommandTest
@@ -29,7 +29,7 @@ final class ComposerBasedCommand extends Command
2929
* @param RectorInterface[] $rectors
3030
*/
3131
public function __construct(
32-
private readonly SymfonyStyle $symfonyStyle,
32+
private readonly RectorStyle $rectorStyle,
3333
private readonly InstalledPackageResolver $installedPackageResolver,
3434
private readonly array $rectors
3535
) {
@@ -48,22 +48,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4848
$configurationTableRows = $this->createConfigurationTableRows();
4949

5050
if ($tableRows === [] && $configurationTableRows === []) {
51-
$this->symfonyStyle->warning('No composer package bound rule is loaded');
51+
$this->rectorStyle->warning('No composer package bound rule is loaded');
5252

5353
return Command::SUCCESS;
5454
}
5555

5656
if ($tableRows !== []) {
57-
$this->symfonyStyle->title('Composer package bound rules');
58-
$this->symfonyStyle->table(['Rule', 'Package', 'Requires', 'Installed', 'Active'], $tableRows);
57+
$this->rectorStyle->title('Composer package bound rules');
58+
$this->rectorStyle->table(['Rule', 'Package', 'Requires', 'Installed', 'Active'], $tableRows);
5959
}
6060

6161
if ($configurationTableRows !== []) {
62-
$this->symfonyStyle->title('Composer package bound rule configuration');
62+
$this->rectorStyle->title('Composer package bound rule configuration');
6363

6464
$headers = ['Rule', 'Package', 'Requires', 'Installed', 'Active'];
6565

66-
$table = $this->symfonyStyle->createTable();
66+
$table = $this->rectorStyle->createTable();
6767
$table->setHeaders($headers);
6868
$table->setRows($this->createConfigurationDisplayRows($configurationTableRows));
6969

@@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8383
$allTableRows = [...$tableRows, ...$configurationTableRows];
8484
$activeCount = count(array_filter($allTableRows, static fn (array $tableRow): bool => $tableRow[4] === 'yes'));
8585

86-
$this->symfonyStyle->note(
86+
$this->rectorStyle->note(
8787
sprintf('%d of %d composer package bound items are active', $activeCount, count($allTableRows))
8888
);
8989

0 commit comments

Comments
 (0)