2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\ChangesReporting\Output;
|
2019-05-28 14:18:05 +00:00
|
|
|
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Nette\Utils\Json;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
|
|
|
|
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
|
|
|
|
use Rector\Core\ValueObject\Configuration;
|
|
|
|
use Rector\Core\ValueObject\Error\SystemError;
|
|
|
|
use Rector\Core\ValueObject\ProcessResult;
|
|
|
|
use Rector\Parallel\ValueObject\Bridge;
|
2022-06-07 08:22:29 +00:00
|
|
|
final class JsonOutputFormatter implements OutputFormatterInterface
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const NAME = 'json';
|
2021-04-10 14:54:24 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\ChangesReporting\Annotation\RectorsChangelogResolver
|
2021-04-10 14:54:24 +00:00
|
|
|
*/
|
2021-04-10 16:22:25 +00:00
|
|
|
private $rectorsChangelogResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(RectorsChangelogResolver $rectorsChangelogResolver)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-04-10 16:22:25 +00:00
|
|
|
$this->rectorsChangelogResolver = $rectorsChangelogResolver;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getName() : string
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
return self::NAME;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function report(ProcessResult $processResult, Configuration $configuration) : void
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2022-01-07 15:47:35 +00:00
|
|
|
$errorsJson = ['totals' => ['changed_files' => \count($processResult->getFileDiffs()), 'removed_and_added_files_count' => $processResult->getRemovedAndAddedFilesCount(), 'removed_node_count' => $processResult->getRemovedNodeCount()]];
|
2021-04-12 10:34:04 +00:00
|
|
|
$fileDiffs = $processResult->getFileDiffs();
|
2021-05-09 20:15:43 +00:00
|
|
|
\ksort($fileDiffs);
|
2019-05-28 14:18:05 +00:00
|
|
|
foreach ($fileDiffs as $fileDiff) {
|
2019-05-28 20:38:21 +00:00
|
|
|
$relativeFilePath = $fileDiff->getRelativeFilePath();
|
2021-04-10 16:22:25 +00:00
|
|
|
$appliedRectorsWithChangelog = $this->rectorsChangelogResolver->resolve($fileDiff->getRectorClasses());
|
2022-06-07 08:22:29 +00:00
|
|
|
$errorsJson[Bridge::FILE_DIFFS][] = ['file' => $relativeFilePath, 'diff' => $fileDiff->getDiff(), 'applied_rectors' => $fileDiff->getRectorClasses(), 'applied_rectors_with_changelog' => $appliedRectorsWithChangelog];
|
2019-05-28 14:49:31 +00:00
|
|
|
// for Rector CI
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorsJson['changed_files'][] = $relativeFilePath;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-04-12 10:34:04 +00:00
|
|
|
$errors = $processResult->getErrors();
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorsJson['totals']['errors'] = \count($errors);
|
2020-12-12 13:53:10 +00:00
|
|
|
$errorsData = $this->createErrorsData($errors);
|
|
|
|
if ($errorsData !== []) {
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorsJson['errors'] = $errorsData;
|
2020-12-12 13:53:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$json = Json::encode($errorsJson, Json::PRETTY);
|
2021-06-21 20:01:51 +00:00
|
|
|
echo $json . \PHP_EOL;
|
2020-12-12 13:53:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-02-05 11:57:25 +00:00
|
|
|
* @param SystemError[] $errors
|
2020-12-12 13:53:10 +00:00
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2021-07-05 23:06:30 +00:00
|
|
|
private function createErrorsData(array $errors) : array
|
2020-12-12 13:53:10 +00:00
|
|
|
{
|
|
|
|
$errorsData = [];
|
2019-05-28 14:18:05 +00:00
|
|
|
foreach ($errors as $error) {
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorDataJson = ['message' => $error->getMessage(), 'file' => $error->getRelativeFilePath()];
|
2022-02-05 11:57:25 +00:00
|
|
|
if ($error->getRectorClass() !== null) {
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorDataJson['caused_by'] = $error->getRectorClass();
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
if ($error->getLine() !== null) {
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorDataJson['line'] = $error->getLine();
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-12-06 19:42:49 +00:00
|
|
|
$errorsData[] = $errorDataJson;
|
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
|
|
|
}
|
|
|
|
}
|