rector/src/Console/Output/ProcessCommandReporter.php

99 lines
2.6 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;
2017-12-28 17:32:02 +00:00
use Rector\Contract\Rector\RectorInterface;
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
2018-02-08 15:51:16 +00:00
use Rector\Reporting\FileDiff;
2018-06-27 23:25:07 +00:00
use Rector\YamlRector\YamlFileProcessor;
use Symfony\Component\Console\Style\SymfonyStyle;
2018-09-21 05:12:24 +00:00
use function Safe\sprintf;
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
/**
* @var RectorNodeTraverser
2017-10-22 14:31:29 +00:00
*/
private $rectorNodeTraverser;
2017-10-22 14:31:29 +00:00
2018-06-27 23:25:07 +00:00
/**
* @var YamlFileProcessor
*/
private $yamlFileProcessor;
public function __construct(
RectorNodeTraverser $rectorNodeTraverser,
SymfonyStyle $symfonyStyle,
2018-06-27 23:25:07 +00:00
YamlFileProcessor $yamlFileProcessor
) {
$this->symfonyStyle = $symfonyStyle;
$this->rectorNodeTraverser = $rectorNodeTraverser;
2018-06-27 23:25:07 +00:00
$this->yamlFileProcessor = $yamlFileProcessor;
2017-10-22 14:31:29 +00:00
}
/**
* @param string[] $changedFiles
*/
public function reportChangedFiles(array $changedFiles): void
{
if (count($changedFiles) <= 0) {
return;
}
$this->symfonyStyle->title(
2018-09-21 05:12:24 +00:00
sprintf('%d Changed file%s', count($changedFiles), count($changedFiles) === 1 ? '' : 's')
);
$this->symfonyStyle->listing($changedFiles);
2017-10-22 14:31:29 +00:00
}
2017-12-20 01:12:15 +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;
}
$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-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
}