rector/packages/ChangesReporting/Output/JsonOutputFormatter.php

129 lines
3.6 KiB
PHP
Raw Normal View History

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;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
2020-03-19 00:07:07 +00:00
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Core\Configuration\Configuration;
use Rector\Core\ValueObject\ProcessResult;
use Symplify\SmartFileSystem\SmartFileSystem;
2019-05-28 14:18:05 +00:00
final class JsonOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
public const NAME = 'json';
/**
* @var Configuration
*/
private $configuration;
/**
* @var SmartFileSystem
*/
private $smartFileSystem;
/**
* @var RectorsChangelogResolver
*/
private $rectorsChangelogResolver;
public function __construct(
Configuration $configuration,
SmartFileSystem $smartFileSystem,
RectorsChangelogResolver $rectorsChangelogResolver
) {
$this->configuration = $configuration;
$this->smartFileSystem = $smartFileSystem;
$this->rectorsChangelogResolver = $rectorsChangelogResolver;
2019-05-28 14:18:05 +00:00
}
public function getName(): string
{
return self::NAME;
2019-05-28 14:18:05 +00:00
}
public function report(ProcessResult $processResult): void
2019-05-28 14:18:05 +00:00
{
$errorsArray = [
'meta' => [
'version' => $this->configuration->getPrettyVersion(),
'config' => $this->configuration->getMainConfigFilePath(),
],
'totals' => [
'changed_files' => count($processResult->getFileDiffs()),
'removed_and_added_files_count' => $processResult->getRemovedAndAddedFilesCount(),
'removed_node_count' => $processResult->getRemovedNodeCount(),
],
];
2019-05-28 14:18:05 +00:00
$fileDiffs = $processResult->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();
$appliedRectorsWithChangelog = $this->rectorsChangelogResolver->resolve($fileDiff->getRectorClasses());
2019-05-28 14:18:05 +00:00
$errorsArray['file_diffs'][] = [
'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(),
'applied_rectors_with_changelog' => $appliedRectorsWithChangelog,
2019-05-28 14:18:05 +00:00
];
2019-05-28 14:49:31 +00:00
// for Rector CI
$errorsArray['changed_files'][] = $relativeFilePath;
2019-05-28 14:18:05 +00:00
}
$errors = $processResult->getErrors();
2019-05-28 14:18:05 +00:00
$errorsArray['totals']['errors'] = count($errors);
$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(),
'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();
}
$errorsData[] = $errorData;
2019-05-28 14:18:05 +00:00
}
return $errorsData;
2019-05-28 14:18:05 +00:00
}
}