-
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathJsonOutputFormatter.php
More file actions
100 lines (80 loc) · 2.89 KB
/
Copy pathJsonOutputFormatter.php
File metadata and controls
100 lines (80 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Output;
use Nette\Utils\Json;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\ProcessResult;
final class JsonOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
final public const NAME = 'json';
public function __construct(
private readonly RectorsChangelogResolver $rectorsChangelogResolver
) {
}
public function getName(): string
{
return self::NAME;
}
public function report(ProcessResult $processResult, Configuration $configuration): void
{
$errorsArray = [
'meta' => [
'config' => $configuration->getMainConfigFilePath(),
],
'totals' => [
'changed_files' => count($processResult->getFileDiffs()),
'removed_and_added_files_count' => $processResult->getRemovedAndAddedFilesCount(),
'removed_node_count' => $processResult->getRemovedNodeCount(),
],
];
$fileDiffs = $processResult->getFileDiffs();
ksort($fileDiffs);
foreach ($fileDiffs as $fileDiff) {
$relativeFilePath = $fileDiff->getRelativeFilePath();
$appliedRectorsWithChangelog = $this->rectorsChangelogResolver->resolve($fileDiff->getRectorClasses());
$errorsArray['file_diffs'][] = [
'file' => $relativeFilePath,
'diff' => $fileDiff->getDiff(),
'applied_rectors' => $fileDiff->getRectorClasses(),
'applied_rectors_with_changelog' => $appliedRectorsWithChangelog,
];
// for Rector CI
$errorsArray['changed_files'][] = $relativeFilePath;
}
$errors = $processResult->getErrors();
$errorsArray['totals']['errors'] = count($errors);
$errorsData = $this->createErrorsData($errors);
if ($errorsData !== []) {
$errorsArray['errors'] = $errorsData;
}
$json = Json::encode($errorsArray, Json::PRETTY);
echo $json . PHP_EOL;
}
/**
* @param mixed[] $errors
* @return mixed[]
*/
private function createErrorsData(array $errors): array
{
$errorsData = [];
foreach ($errors as $error) {
$errorData = [
'message' => $error->getMessage(),
'file' => $error->getRelativeFilePath(),
];
if ($error->getRectorClass()) {
$errorData['caused_by'] = $error->getRectorClass();
}
if ($error->getLine() !== null) {
$errorData['line'] = $error->getLine();
}
$errorsData[] = $errorData;
}
return $errorsData;
}
}