Skip to content

Commit

Permalink
[DX] Merge RectorOutputStyle, OutputStyleInterface to RectorStyle (#4711
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TomasVotruba committed Aug 7, 2023
1 parent acce9fa commit 6e9378e
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 252 deletions.
8 changes: 4 additions & 4 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Rector\Core\Bootstrap\RectorConfigsResolver;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\Console\Style\RectorConsoleOutputStyleFactory;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\DependencyInjection\RectorContainerFactory;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Util\Reflection\PrivatesAccessor;
Expand Down Expand Up @@ -151,9 +151,9 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void
]);
} else {
// report fatal errors in console format
$rectorConsoleOutputStyleFactory = new RectorConsoleOutputStyleFactory(new PrivatesAccessor());
$rectorConsoleOutputStyle = $rectorConsoleOutputStyleFactory->create();
$rectorConsoleOutputStyle->error($throwable->getMessage());
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor());
$symfonyStyle = $symfonyStyleFactory->create();
$symfonyStyle->error($throwable->getMessage());
}

exit(Command::FAILURE);
Expand Down
8 changes: 3 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
use Rector\Core\Console\Command\ListRulesCommand;
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\Console\Output\OutputFormatterCollector;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Console\Style\RectorConsoleOutputStyleFactory;
use Rector\Core\Console\Style\RectorStyle;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
Expand Down Expand Up @@ -187,9 +186,6 @@
$services->set(CloningVisitor::class);
$services->set(NodeFinder::class);

$services->set(RectorConsoleOutputStyle::class)
->factory([service(RectorConsoleOutputStyleFactory::class), 'create']);

$services->set(Parser::class)
->factory([service(PHPStanServicesFactory::class), 'createPHPStanParser']);

Expand All @@ -204,6 +200,8 @@

// console
$services->set(SymfonyStyleFactory::class);

$services->alias(RectorStyle::class, SymfonyStyle::class);
$services->set(SymfonyStyle::class)
->factory([service(SymfonyStyleFactory::class), 'create']);

Expand Down
2 changes: 0 additions & 2 deletions easy-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
Expand Down Expand Up @@ -43,7 +42,6 @@
RectorBetterReflectionSourceLocatorFactory::class,
AbstractTestCase::class,
PHPStanServicesFactory::class,
OutputStyleInterface::class,
// fix later - rector-symfony
// used in tests
FileInfoParser::class,
Expand Down
24 changes: 12 additions & 12 deletions packages/ChangesReporting/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Nette\Utils\Strings;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\ProcessResult;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Style\SymfonyStyle;

final class ConsoleOutputFormatter implements OutputFormatterInterface
{
Expand All @@ -27,7 +27,7 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface
private const ON_LINE_REGEX = '# on line #';

public function __construct(
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly SymfonyStyle $symfonyStyle,
private readonly RectorsChangelogResolver $rectorsChangelogResolver,
) {
}
Expand All @@ -46,11 +46,11 @@ public function report(ProcessResult $processResult, Configuration $configuratio

// to keep space between progress bar and success message
if ($configuration->shouldShowProgressBar() && $processResult->getFileDiffs() === []) {
$this->rectorOutputStyle->newLine();
$this->symfonyStyle->newLine();
}

$message = $this->createSuccessMessage($processResult, $configuration);
$this->rectorOutputStyle->success($message);
$this->symfonyStyle->success($message);
}

public function getName(): string
Expand All @@ -71,7 +71,7 @@ private function reportFileDiffs(array $fileDiffs): void
ksort($fileDiffs);
$message = sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's');

$this->rectorOutputStyle->title($message);
$this->symfonyStyle->title($message);

$i = 0;
foreach ($fileDiffs as $fileDiff) {
Expand All @@ -85,16 +85,16 @@ private function reportFileDiffs(array $fileDiffs): void

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

$this->rectorOutputStyle->writeln($message);
$this->rectorOutputStyle->newLine();
$this->rectorOutputStyle->writeln($fileDiff->getDiffConsoleFormatted());
$this->symfonyStyle->writeln($message);
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted());

$rectorsChangelogsLines = $this->createRectorChangelogLines($fileDiff);

if ($fileDiff->getRectorChanges() !== []) {
$this->rectorOutputStyle->writeln('<options=underscore>Applied rules:</>');
$this->rectorOutputStyle->listing($rectorsChangelogsLines);
$this->rectorOutputStyle->newLine();
$this->symfonyStyle->writeln('<options=underscore>Applied rules:</>');
$this->symfonyStyle->listing($rectorsChangelogsLines);
$this->symfonyStyle->newLine();
}
}
}
Expand All @@ -120,7 +120,7 @@ private function reportErrors(array $errors): void
$message .= ' On line: ' . $error->getLine();
}

$this->rectorOutputStyle->error($message);
$this->symfonyStyle->error($message);
}
}

Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,8 @@ parameters:
- '#Parameter \#1 \$commands of method Symfony\\Component\\Console\\Application\:\:addCommands\(\) expects array<Symfony\\Component\\Console\\Command\\Command>, iterable<Symfony\\Component\\Console\\Command\\Command> given#'

- '#Property Rector\\Core\\Console\\Command\\ListRulesCommand\:\:\$rectors \(array<Rector\\Core\\Contract\\Rector\\RectorInterface>\) does not accept array\|iterable<Rector\\Core\\Contract\\Rector\\RectorInterface>#'

# reflection property change
-
message: '#\$this as argument is not allowed\. Refactor method to service composition#'
path: src/Console/ConsoleApplication.php
32 changes: 16 additions & 16 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Util\ArrayParametersMerger;
Expand All @@ -22,6 +21,7 @@
use Rector\Parallel\ValueObject\Bridge;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyParallel\CpuCoreCountProvider;
use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyParallel\ScheduleFactory;
Expand All @@ -43,16 +43,16 @@ final class ApplicationFileProcessor
* @param FileProcessorInterface[] $fileProcessors
*/
public function __construct(
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileFactory $fileFactory,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly SymfonyStyle $symfonyStyle,
private readonly FileFactory $fileFactory,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly ParallelFileProcessor $parallelFileProcessor,
private readonly ScheduleFactory $scheduleFactory,
private readonly CpuCoreCountProvider $cpuCoreCountProvider,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly CurrentFileProvider $currentFileProvider,
private readonly iterable $fileProcessors,
private readonly ScheduleFactory $scheduleFactory,
private readonly CpuCoreCountProvider $cpuCoreCountProvider,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly CurrentFileProvider $currentFileProvider,
private readonly iterable $fileProcessors,
) {
}

Expand Down Expand Up @@ -103,8 +103,8 @@ public function processFiles(array $filePaths, Configuration $configuration, boo
// progress bar on parallel handled on runParallel()
if (! $isParallel && $shouldShowProgressBar) {
$fileCount = count($filePaths);
$this->rectorOutputStyle->progressStart($fileCount);
$this->rectorOutputStyle->progressAdvance(0);
$this->symfonyStyle->progressStart($fileCount);
$this->symfonyStyle->progressAdvance(0);
}

$systemErrorsAndFileDiffs = [
Expand All @@ -124,7 +124,7 @@ public function processFiles(array $filePaths, Configuration $configuration, boo
// progress bar +1,
// progress bar on parallel handled on runParallel()
if (! $isParallel && $shouldShowProgressBar) {
$this->rectorOutputStyle->progressAdvance();
$this->symfonyStyle->progressAdvance();
}
} catch (Throwable $throwable) {
$this->invalidateFile($file);
Expand Down Expand Up @@ -173,7 +173,7 @@ private function resolveSystemError(Throwable $throwable, string|File $filePath)
? $filePath->getFilePath()
: $filePath;

if ($this->rectorOutputStyle->isDebug()) {
if ($this->symfonyStyle->isDebug()) {
return new SystemError(
$errorMessage . PHP_EOL . 'Stack trace:' . PHP_EOL . $throwable->getTraceAsString(),
$filePath,
Expand Down Expand Up @@ -242,11 +242,11 @@ private function runParallel(array $filePaths, Configuration $configuration, Inp

if ($configuration->shouldShowProgressBar()) {
$fileCount = count($filePaths);
$this->rectorOutputStyle->progressStart($fileCount);
$this->rectorOutputStyle->progressAdvance(0);
$this->symfonyStyle->progressStart($fileCount);
$this->symfonyStyle->progressAdvance(0);

$postFileCallback = function (int $stepCount): void {
$this->rectorOutputStyle->progressAdvance($stepCount);
$this->symfonyStyle->progressAdvance($stepCount);
// running in parallel here → nothing else to do
};
}
Expand Down
22 changes: 11 additions & 11 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileProcessor;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\FileSystem\FilePathHelper;
Expand All @@ -22,6 +21,7 @@
use Rector\Parallel\ValueObject\Bridge;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

final class PhpFileProcessor implements FileProcessorInterface
Expand All @@ -34,13 +34,13 @@ final class PhpFileProcessor implements FileProcessorInterface

public function __construct(
private readonly FormatPerservingPrinter $formatPerservingPrinter,
private readonly FileProcessor $fileProcessor,
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileDiffFactory $fileDiffFactory,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly PostFileProcessor $postFileProcessor,
private readonly ErrorFactory $errorFactory,
private readonly FilePathHelper $filePathHelper
private readonly FileProcessor $fileProcessor,
private readonly SymfonyStyle $symfonyStyle,
private readonly FileDiffFactory $fileDiffFactory,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly PostFileProcessor $postFileProcessor,
private readonly ErrorFactory $errorFactory,
private readonly FilePathHelper $filePathHelper
) {
}

Expand Down Expand Up @@ -153,7 +153,7 @@ private function parseFileAndDecorateNodes(File $file): array
);
return [$autoloadSystemError];
} catch (Throwable $throwable) {
if ($this->rectorOutputStyle->isVerbose() || StaticPHPUnitEnvironment::isPHPUnitRun()) {
if ($this->symfonyStyle->isVerbose() || StaticPHPUnitEnvironment::isPHPUnitRun()) {
throw $throwable;
}

Expand Down Expand Up @@ -209,11 +209,11 @@ private function printFile(File $file, Configuration $configuration): void

private function notifyFile(File $file): void
{
if (! $this->rectorOutputStyle->isVerbose()) {
if (! $this->symfonyStyle->isVerbose()) {
return;
}

$relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath());
$this->rectorOutputStyle->writeln($relativeFilePath);
$this->symfonyStyle->writeln($relativeFilePath);
}
}
6 changes: 3 additions & 3 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\ValueObject\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class ConfigurationFactory
{
public function __construct(
private readonly OutputStyleInterface $rectorOutputStyle
private readonly SymfonyStyle $symfonyStyle
) {
}

Expand Down Expand Up @@ -73,7 +73,7 @@ private function shouldShowProgressBar(InputInterface $input, string $outputForm
return false;
}

if ($this->rectorOutputStyle->isVerbose()) {
if ($this->symfonyStyle->isVerbose()) {
return false;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Console/Command/ListRulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nette\Utils\Json;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\Output\RectorOutputStyle;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\ComplementaryRectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
Expand All @@ -16,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

final class ListRulesCommand extends Command
Expand All @@ -29,11 +29,12 @@ final class ListRulesCommand extends Command
* @param RewindableGenerator<RectorInterface>|RectorInterface[] $rectors
*/
public function __construct(
private readonly RectorOutputStyle $rectorOutputStyle,
private readonly SymfonyStyle $symfonyStyle,
private readonly SkippedClassResolver $skippedClassResolver,
iterable $rectors
) {
parent::__construct();

if ($rectors instanceof RewindableGenerator) {
$rectors = iterator_to_array($rectors->getIterator());
}
Expand Down Expand Up @@ -72,12 +73,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

$this->rectorOutputStyle->title('Loaded Rector rules');
$this->rectorOutputStyle->listing($rectorClasses);
$this->symfonyStyle->title('Loaded Rector rules');
$this->symfonyStyle->listing($rectorClasses);

if ($skippedClasses !== []) {
$this->rectorOutputStyle->title('Skipped Rector rules');
$this->rectorOutputStyle->listing($skippedClasses);
$this->symfonyStyle->title('Skipped Rector rules');
$this->symfonyStyle->listing($skippedClasses);
}

return Command::SUCCESS;
Expand Down
Loading

0 comments on commit 6e9378e

Please sign in to comment.