2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-05-28 14:18:05 +00:00
|
|
|
|
2020-03-19 00:07:07 +00:00
|
|
|
namespace Rector\ChangesReporting\Output;
|
2019-05-28 14:18:05 +00:00
|
|
|
|
|
|
|
use Nette\Utils\Json;
|
2021-04-10 14:54:24 +00:00
|
|
|
use Rector\ChangesReporting\Annotation\AnnotationExtractor;
|
2020-03-19 00:07:07 +00:00
|
|
|
use Rector\ChangesReporting\Application\ErrorAndDiffCollector;
|
|
|
|
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Configuration\Configuration;
|
2020-07-24 12:21:34 +00:00
|
|
|
use Symplify\SmartFileSystem\SmartFileSystem;
|
2019-05-28 14:18:05 +00:00
|
|
|
|
|
|
|
final class JsonOutputFormatter implements OutputFormatterInterface
|
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const NAME = 'json';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Configuration
|
|
|
|
*/
|
|
|
|
private $configuration;
|
|
|
|
|
2020-07-24 12:21:34 +00:00
|
|
|
/**
|
|
|
|
* @var SmartFileSystem
|
|
|
|
*/
|
|
|
|
private $smartFileSystem;
|
|
|
|
|
2021-04-10 14:54:24 +00:00
|
|
|
/**
|
|
|
|
* @var AnnotationExtractor
|
|
|
|
*/
|
|
|
|
private $annotationExtractor;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Configuration $configuration,
|
|
|
|
SmartFileSystem $smartFileSystem,
|
|
|
|
AnnotationExtractor $annotationExtractor
|
|
|
|
) {
|
2019-05-28 14:47:55 +00:00
|
|
|
$this->configuration = $configuration;
|
2020-07-24 12:21:34 +00:00
|
|
|
$this->smartFileSystem = $smartFileSystem;
|
2021-04-10 14:54:24 +00:00
|
|
|
$this->annotationExtractor = $annotationExtractor;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
return self::NAME;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:05:12 +00:00
|
|
|
public function report(ErrorAndDiffCollector $errorAndDiffCollector): void
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
$errorsArray = [
|
|
|
|
'meta' => [
|
|
|
|
'version' => $this->configuration->getPrettyVersion(),
|
2021-02-20 19:48:04 +00:00
|
|
|
'config' => $this->configuration->getMainConfigFilePath(),
|
2019-05-28 14:47:55 +00:00
|
|
|
],
|
|
|
|
'totals' => [
|
2020-01-08 15:32:02 +00:00
|
|
|
'changed_files' => $errorAndDiffCollector->getFileDiffsCount(),
|
2019-05-28 14:47:55 +00:00
|
|
|
'removed_and_added_files_count' => $errorAndDiffCollector->getRemovedAndAddedFilesCount(),
|
2019-08-06 07:37:31 +00:00
|
|
|
'removed_node_count' => $errorAndDiffCollector->getRemovedNodeCount(),
|
2019-05-28 14:47:55 +00:00
|
|
|
],
|
|
|
|
];
|
2019-05-28 14:18:05 +00:00
|
|
|
|
2020-01-08 15:32:02 +00:00
|
|
|
$fileDiffs = $errorAndDiffCollector->getFileDiffs();
|
2019-05-28 14:18:05 +00:00
|
|
|
ksort($fileDiffs);
|
|
|
|
foreach ($fileDiffs as $fileDiff) {
|
2019-05-28 20:38:21 +00:00
|
|
|
$relativeFilePath = $fileDiff->getRelativeFilePath();
|
2019-05-28 16:28:09 +00:00
|
|
|
|
2021-04-10 14:54:24 +00:00
|
|
|
$appliedRectorsWithChangelog = $fileDiff->getRectorClassesWithChangelogUrlAndRectorClassAsKey(
|
|
|
|
$this->annotationExtractor
|
|
|
|
);
|
|
|
|
|
2019-05-28 14:18:05 +00:00
|
|
|
$errorsArray['file_diffs'][] = [
|
2019-05-28 16:28:09 +00:00
|
|
|
'file' => $relativeFilePath,
|
2019-05-28 14:18:05 +00:00
|
|
|
'diff' => $fileDiff->getDiff(),
|
2020-03-19 00:07:07 +00:00
|
|
|
'applied_rectors' => $fileDiff->getRectorClasses(),
|
2021-04-10 14:54:24 +00:00
|
|
|
'applied_rectors_with_changelog' => $appliedRectorsWithChangelog,
|
2019-05-28 14:18:05 +00:00
|
|
|
];
|
2019-05-28 14:49:31 +00:00
|
|
|
|
|
|
|
// for Rector CI
|
2019-05-28 16:28:09 +00:00
|
|
|
$errorsArray['changed_files'][] = $relativeFilePath;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 14:28:46 +00:00
|
|
|
$errors = $errorAndDiffCollector->getErrors();
|
2019-05-28 14:18:05 +00:00
|
|
|
$errorsArray['totals']['errors'] = count($errors);
|
|
|
|
|
2020-12-12 13:53:10 +00:00
|
|
|
$errorsData = $this->createErrorsData($errors);
|
|
|
|
if ($errorsData !== []) {
|
|
|
|
$errorsArray['errors'] = $errorsData;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = Json::encode($errorsArray, Json::PRETTY);
|
|
|
|
|
|
|
|
$outputFile = $this->configuration->getOutputFile();
|
|
|
|
if ($outputFile !== null) {
|
|
|
|
$this->smartFileSystem->dumpFile($outputFile, $json . PHP_EOL);
|
|
|
|
} else {
|
|
|
|
echo $json . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed[] $errors
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
private function createErrorsData(array $errors): array
|
|
|
|
{
|
|
|
|
$errorsData = [];
|
|
|
|
|
2019-05-28 14:18:05 +00:00
|
|
|
foreach ($errors as $error) {
|
|
|
|
$errorData = [
|
|
|
|
'message' => $error->getMessage(),
|
2020-11-16 17:50:38 +00:00
|
|
|
'file' => $error->getRelativeFilePath(),
|
2019-05-28 14:18:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($error->getRectorClass()) {
|
|
|
|
$errorData['caused_by'] = $error->getRectorClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($error->getLine() !== null) {
|
|
|
|
$errorData['line'] = $error->getLine();
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:53:10 +00:00
|
|
|
$errorsData[] = $errorData;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:53:10 +00:00
|
|
|
return $errorsData;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
}
|