mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-14 12:32:00 +02:00
add JsonOutputFormatter
This commit is contained in:
parent
1b6c3134fa
commit
8ca511a8e2
@ -23,13 +23,22 @@ final class DifferAndFormatter
|
||||
$this->diffConsoleFormatter = $diffConsoleFormatter;
|
||||
}
|
||||
|
||||
public function diff(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->differ->diff($old, $new);
|
||||
}
|
||||
|
||||
public function diffAndFormat(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$diff = $this->differ->diff($old, $new);
|
||||
$diff = $this->diff($old, $new);
|
||||
|
||||
return $this->diffConsoleFormatter->format($diff);
|
||||
}
|
||||
|
@ -17,4 +17,4 @@ parameters:
|
||||
php_version_features: '7.1'
|
||||
|
||||
services:
|
||||
# Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
|
||||
Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
|
||||
|
@ -83,6 +83,7 @@ final class ErrorAndDiffCollector
|
||||
// always keep the most recent diff
|
||||
$this->fileDiffs[$smartFileInfo->getRealPath()] = new FileDiff(
|
||||
$smartFileInfo->getRealPath(),
|
||||
$this->differAndFormatter->diff($oldContent, $newContent),
|
||||
$this->differAndFormatter->diffAndFormat($oldContent, $newContent),
|
||||
$appliedRectors
|
||||
);
|
||||
|
@ -49,7 +49,7 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface
|
||||
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->writeln($fileDiff->getDiffConsoleFormatted());
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
if ($fileDiff->getAppliedRectorClasses() !== []) {
|
||||
|
80
src/Console/Output/JsonOutputFormatter.php
Normal file
80
src/Console/Output/JsonOutputFormatter.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Console\Output;
|
||||
|
||||
use Nette\Utils\Json;
|
||||
use Rector\Application\Error;
|
||||
use Rector\Contract\Console\Output\OutputFormatterInterface;
|
||||
use Rector\Reporting\FileDiff;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class JsonOutputFormatter implements OutputFormatterInterface
|
||||
{
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
public function __construct(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'json';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FileDiff[] $fileDiffs
|
||||
*/
|
||||
public function reportFileDiffs(array $fileDiffs): void
|
||||
{
|
||||
$errorsArray = [];
|
||||
$errorsArray['totals']['changed_files'] = count($fileDiffs);
|
||||
|
||||
ksort($fileDiffs);
|
||||
|
||||
foreach ($fileDiffs as $fileDiff) {
|
||||
$errorsArray['file_diffs'][] = [
|
||||
'file' => $fileDiff->getFile(),
|
||||
'diff' => $fileDiff->getDiff(),
|
||||
'applied_rectors' => $fileDiff->getAppliedRectorClasses(),
|
||||
];
|
||||
}
|
||||
|
||||
$json = Json::encode($errorsArray, Json::PRETTY);
|
||||
|
||||
$this->symfonyStyle->writeln($json);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Error[] $errors
|
||||
*/
|
||||
public function reportErrors(array $errors): void
|
||||
{
|
||||
$errorsArray = [];
|
||||
$errorsArray['totals']['errors'] = count($errors);
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$errorData = [
|
||||
'message' => $error->getMessage(),
|
||||
'file' => $error->getFileInfo()->getPathname(),
|
||||
];
|
||||
|
||||
if ($error->getRectorClass()) {
|
||||
$errorData['caused_by'] = $error->getRectorClass();
|
||||
}
|
||||
|
||||
if ($error->getLine() !== null) {
|
||||
$errorData['line'] = $error->getLine();
|
||||
}
|
||||
|
||||
$errorsArray['errors'][] = $errorData;
|
||||
}
|
||||
|
||||
$json = Json::encode($errorsArray, Json::PRETTY);
|
||||
|
||||
$this->symfonyStyle->writeln($json);
|
||||
}
|
||||
}
|
@ -19,14 +19,24 @@ final class FileDiff
|
||||
*/
|
||||
private $appliedRectorClasses = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $diffConsoleFormatted;
|
||||
|
||||
/**
|
||||
* @param string[] $appliedRectorClasses
|
||||
*/
|
||||
public function __construct(string $file, string $diff, array $appliedRectorClasses = [])
|
||||
{
|
||||
public function __construct(
|
||||
string $file,
|
||||
string $diff,
|
||||
string $diffConsoleFormatted,
|
||||
array $appliedRectorClasses = []
|
||||
) {
|
||||
$this->file = $file;
|
||||
$this->diff = $diff;
|
||||
$this->appliedRectorClasses = $appliedRectorClasses;
|
||||
$this->diffConsoleFormatted = $diffConsoleFormatted;
|
||||
}
|
||||
|
||||
public function getDiff(): string
|
||||
@ -34,6 +44,11 @@ final class FileDiff
|
||||
return $this->diff;
|
||||
}
|
||||
|
||||
public function getDiffConsoleFormatted(): string
|
||||
{
|
||||
return $this->diffConsoleFormatted;
|
||||
}
|
||||
|
||||
public function getFile(): string
|
||||
{
|
||||
return $this->file;
|
||||
|
Loading…
x
Reference in New Issue
Block a user