-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathJsonOutputFactory.php
More file actions
85 lines (69 loc) · 2.59 KB
/
JsonOutputFactory.php
File metadata and controls
85 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Output\Factory;
use Nette\Utils\Json;
use Rector\Parallel\ValueObject\Bridge;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
/**
* @see \Rector\Tests\ChangesReporting\Output\Factory\JsonOutputFactoryTest
*/
final class JsonOutputFactory
{
public static function create(ProcessResult $processResult, Configuration $configuration): string
{
$errorsJson = [
'totals' => [
'changed_files' => $processResult->getTotalChanged(),
],
];
// We need onlyWithChanges: false to include all file diffs
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
ksort($fileDiffs);
foreach ($fileDiffs as $fileDiff) {
$filePath = $configuration->isReportingWithRealPath()
? ($fileDiff->getAbsoluteFilePath() ?? '')
: $fileDiff->getRelativeFilePath()
;
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
$errorsJson[Bridge::FILE_DIFFS][] = [
'file' => $filePath,
'diff' => $fileDiff->getDiff(),
'applied_rectors' => $fileDiff->getRectorClasses(),
];
}
// for Rector CI
$errorsJson['changed_files'][] = $filePath;
}
$systemErrors = $processResult->getSystemErrors();
$errorsJson['totals']['errors'] = count($systemErrors);
$errorsData = self::createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
if ($errorsData !== []) {
$errorsJson['errors'] = $errorsData;
}
return Json::encode($errorsJson, pretty: true);
}
/**
* @param SystemError[] $errors
* @return mixed[]
*/
private static function createErrorsData(array $errors, bool $absoluteFilePath): array
{
$errorsData = [];
foreach ($errors as $error) {
$errorDataJson = [
'message' => $error->getMessage(),
'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath(),
];
if ($error->getRectorClass() !== null) {
$errorDataJson['caused_by'] = $error->getRectorClass();
}
if ($error->getLine() !== null) {
$errorDataJson['line'] = $error->getLine();
}
$errorsData[] = $errorDataJson;
}
return $errorsData;
}
}