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;
|
2019-01-02 13:22:25 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2017-10-22 14:31:29 +00:00
|
|
|
|
|
|
|
final class ProcessCommandReporter
|
|
|
|
{
|
|
|
|
/**
|
2018-11-02 17:27:00 +00:00
|
|
|
* @var SymfonyStyle
|
2017-10-22 14:31:29 +00:00
|
|
|
*/
|
2018-11-02 17:27:00 +00:00
|
|
|
private $symfonyStyle;
|
2017-10-22 14:31:29 +00:00
|
|
|
|
2019-05-18 23:02:07 +00:00
|
|
|
public function __construct(SymfonyStyle $symfonyStyle)
|
|
|
|
{
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle = $symfonyStyle;
|
2017-10-22 14:31:29 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 06:03:11 +00:00
|
|
|
/**
|
2018-02-08 15:51:16 +00:00
|
|
|
* @param FileDiff[] $fileDiffs
|
2018-01-02 06:03:11 +00:00
|
|
|
*/
|
2018-02-08 15:51:16 +00:00
|
|
|
public function reportFileDiffs(array $fileDiffs): void
|
2018-01-02 06:03:11 +00:00
|
|
|
{
|
2018-02-08 15:51:16 +00:00
|
|
|
if (count($fileDiffs) <= 0) {
|
2018-01-06 23:09:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:54:24 +00:00
|
|
|
// normalize
|
|
|
|
ksort($fileDiffs);
|
|
|
|
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle->title(
|
2018-09-21 05:12:24 +00:00
|
|
|
sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's')
|
|
|
|
);
|
2018-01-02 06:03:11 +00:00
|
|
|
|
2018-01-06 20:10:35 +00:00
|
|
|
$i = 0;
|
2018-02-08 15:51:16 +00:00
|
|
|
foreach ($fileDiffs as $fileDiff) {
|
2018-11-02 17:27:00 +00:00
|
|
|
$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-01-02 06:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle->error($message);
|
2018-09-17 10:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 14:31:29 +00:00
|
|
|
}
|