-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathJson.php
123 lines (104 loc) · 3.68 KB
/
Json.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace NunoMaduro\PhpInsights\Application\Console\Formatters;
use Exception;
use InvalidArgumentException;
use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\Insights\Insight;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Json implements Formatter
{
/** @var OutputInterface */
private $output;
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
}
/**
* Format the result to the desired format.
*
* @param InsightCollection $insightCollection
* @param string $dir
* @param array<string> $metrics
*
* @throws Exception
*/
public function format(
InsightCollection $insightCollection,
string $dir,
array $metrics
): void
{
$results = $insightCollection->results();
$data = [
"summary" => [
"code" => $results->getCodeQuality(),
"complexity" => $results->getComplexity(),
'architecture' => $results->getStructure(),
'style' => $results->getStyle(),
'security issues' => $results->getTotalSecurityIssues(),
],
];
$data += $this->issues($insightCollection, $metrics);
$json = json_encode($data);
if ($json === false) {
throw new InvalidArgumentException("Failed parsing result to JSON.");
}
$this->output->write($json);
}
/**
* Outputs the issues errors according to the format.
*
* @param InsightCollection $insightCollection
* @param array<string> $metrics
*
* @return array<string, array<int, array<string, int|string>>|null>
*/
private function issues(
InsightCollection $insightCollection,
array $metrics
): array
{
$data = [];
foreach ($metrics as $metricClass) {
$category = explode('\\', $metricClass);
$category = $category[count($category) - 2];
if (! isset($data[$category])) {
$data[$category] = [];
}
$current = $data[$category];
/** @var Insight $insight */
foreach ($insightCollection->allFrom(new $metricClass()) as $insight) {
if (! $insight->hasIssue()) {
continue;
}
if (! $insight instanceof HasDetails) {
$current[] = [
'title' => $insight->getTitle(),
'insightClass' => $insight->getInsightClass(),
];
continue;
}
/** @var \NunoMaduro\PhpInsights\Domain\Details $detail */
foreach ($insight->getDetails() as $detail) {
$current[] = array_filter([
'title' => $insight->getTitle(),
'insightClass' => $insight->getInsightClass(),
'file' => $detail->hasFile() ? $detail->getFile() : null,
'line' => $detail->hasLine() ? $detail->getLine() : null,
'function' => $detail->hasFunction() ? $detail->getFunction() : null,
'message' => $detail->hasMessage() ? $detail->getMessage() : null,
]);
}
}
$data[$category] = $current;
}
return $data;
}
}