rector/src/Console/Output/ProcessCommandReporter.php

75 lines
2.0 KiB
PHP
Raw Normal View History

2017-10-22 14:31:29 +00:00
<?php declare(strict_types=1);
namespace Rector\Console\Output;
2018-09-17 10:38:11 +00:00
use Rector\Application\Error;
2018-02-08 15:51:16 +00:00
use Rector\Reporting\FileDiff;
use Symfony\Component\Console\Style\SymfonyStyle;
2017-10-22 14:31:29 +00:00
final class ProcessCommandReporter
{
/**
* @var SymfonyStyle
2017-10-22 14:31:29 +00:00
*/
private $symfonyStyle;
2017-10-22 14:31:29 +00:00
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
2017-10-22 14:31:29 +00:00
}
/**
2018-02-08 15:51:16 +00:00
* @param FileDiff[] $fileDiffs
*/
2018-02-08 15:51:16 +00:00
public function reportFileDiffs(array $fileDiffs): void
{
2018-02-08 15:51:16 +00:00
if (count($fileDiffs) <= 0) {
return;
}
// normalize
ksort($fileDiffs);
$this->symfonyStyle->title(
2018-09-21 05:12:24 +00:00
sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's')
);
$i = 0;
2018-02-08 15:51:16 +00:00
foreach ($fileDiffs as $fileDiff) {
$this->symfonyStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $fileDiff->getFile()));
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln($fileDiff->getDiff());
$this->symfonyStyle->newLine();
2018-11-05 01:27:48 +00:00
2019-02-17 14:12:47 +00:00
if ($fileDiff->getAppliedRectorClasses() !== []) {
2018-11-05 01:27:48 +00:00
$this->symfonyStyle->writeln('Applied rectors:');
$this->symfonyStyle->newLine();
$this->symfonyStyle->listing($fileDiff->getAppliedRectorClasses());
$this->symfonyStyle->newLine();
}
}
}
2018-09-17 10:38:11 +00:00
/**
* @param Error[] $errors
*/
public function reportErrors(array $errors): void
{
foreach ($errors as $error) {
$message = sprintf(
2018-11-04 23:36:12 +00:00
'Could not process "%s" file%s, due to: %s"%s".',
2018-09-17 10:38:11 +00:00
$error->getFileInfo()->getPathname(),
2018-11-04 23:36:12 +00:00
$error->getRectorClass() ? ' by "' . $error->getRectorClass() . '"' : '',
2018-09-17 10:38:11 +00:00
PHP_EOL,
$error->getMessage()
);
if ($error->getLine()) {
$message .= ' On line: ' . $error->getLine();
}
$this->symfonyStyle->error($message);
2018-09-17 10:38:11 +00:00
}
}
2017-10-22 14:31:29 +00:00
}