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;
|
2018-11-04 01:17:02 +00:00
|
|
|
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;
|
2018-11-02 17:27:00 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
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
|
|
|
|
|
|
|
/**
|
2018-02-24 21:24:57 +00:00
|
|
|
* @var RectorNodeTraverser
|
2017-10-22 14:31:29 +00:00
|
|
|
*/
|
2018-02-24 21:24:57 +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,
|
2018-11-02 17:27:00 +00:00
|
|
|
SymfonyStyle $symfonyStyle,
|
2018-06-27 23:25:07 +00:00
|
|
|
YamlFileProcessor $yamlFileProcessor
|
|
|
|
) {
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle = $symfonyStyle;
|
2018-02-24 21:24:57 +00:00
|
|
|
$this->rectorNodeTraverser = $rectorNodeTraverser;
|
2018-06-27 23:25:07 +00:00
|
|
|
$this->yamlFileProcessor = $yamlFileProcessor;
|
2017-10-22 14:31:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 04:10:28 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $changedFiles
|
|
|
|
*/
|
|
|
|
public function reportChangedFiles(array $changedFiles): void
|
|
|
|
{
|
2018-01-06 23:09:22 +00:00
|
|
|
if (count($changedFiles) <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle->title(
|
2018-09-21 05:12:24 +00:00
|
|
|
sprintf('%d Changed file%s', count($changedFiles), count($changedFiles) === 1 ? '' : 's')
|
|
|
|
);
|
2018-11-02 17:27:00 +00:00
|
|
|
$this->symfonyStyle->listing($changedFiles);
|
2017-10-22 14:31:29 +00:00
|
|
|
}
|
2017-12-20 01:12:15 +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-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-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
|
|
|
}
|