forked from easy-coding-standard/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonOutputFormatter.php
More file actions
94 lines (80 loc) · 3.12 KB
/
JsonOutputFormatter.php
File metadata and controls
94 lines (80 loc) · 3.12 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
<?php
declare(strict_types=1);
namespace Symplify\EasyCodingStandard\Console\Output;
use Nette\Utils\Json;
use Symplify\EasyCodingStandard\Console\ExitCode;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult;
/**
* @see \Symplify\EasyCodingStandard\Tests\Console\Output\JsonOutputFormatterTest
*/
final readonly class JsonOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
private const FILES = 'files';
public function __construct(
private EasyCodingStandardStyle $easyCodingStandardStyle,
private ExitCodeResolver $exitCodeResolver
) {
}
/**
* @return ExitCode::*
*/
public function report(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): int
{
$json = $this->createJsonContent($errorAndDiffResult, $configuration->isReportingWithRealPath());
$this->easyCodingStandardStyle->writeln($json);
return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration);
}
public static function getName(): string
{
return 'json';
}
public static function hasSupportForProgressBars(): bool
{
return false;
}
/**
* @api
*/
public function createJsonContent(ErrorAndDiffResult $errorAndDiffResult, bool $absoluteFilePath = false): string
{
$errorsArrayJson = $this->createBaseErrorsJson($errorAndDiffResult);
$codingStandardErrors = $errorAndDiffResult->getErrors();
foreach ($codingStandardErrors as $codingStandardError) {
$filePath = $absoluteFilePath ? $codingStandardError->getAbsoluteFilePath() : $codingStandardError->getRelativeFilePath();
$errorsArrayJson[self::FILES][$filePath]['errors'][] = [
'line' => $codingStandardError->getLine(),
'file_path' => $filePath,
'message' => $codingStandardError->getMessage(),
'source_class' => $codingStandardError->getCheckerClass(),
];
}
$fileDiffs = $errorAndDiffResult->getFileDiffs();
foreach ($fileDiffs as $fileDiff) {
$filePath = $absoluteFilePath ? $fileDiff->getAbsoluteFilePath() : $fileDiff->getRelativeFilePath();
$errorsArrayJson[self::FILES][$filePath]['diffs'][] = [
'diff' => $fileDiff->getDiff(),
'applied_checkers' => $fileDiff->getAppliedCheckers(),
];
}
return Json::encode($errorsArrayJson, Json::PRETTY);
}
/**
* @return array{totals: array{errors: int, diffs: int}, files: string[]}
*/
private function createBaseErrorsJson(ErrorAndDiffResult $errorAndDiffResult): array
{
return [
'totals' => [
'errors' => $errorAndDiffResult->getErrorCount(),
'diffs' => $errorAndDiffResult->getFileDiffsCount(),
],
self::FILES => [],
];
}
}