-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ResultPrinter.php
104 lines (91 loc) · 4.06 KB
/
ResultPrinter.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
<?php
namespace staabm\PHPStanBaselineAnalysis;
use Exception;
use \Iterator;
use Safe\DateTimeImmutable;
use function Safe\sprintf;
use function Safe\json_encode;
final class ResultPrinter {
const DATE_FORMAT = DateTimeImmutable::RFC2822;
const FORMAT_JSON = 'json';
const FORMAT_TEXT = 'text';
// use plural string-values
const KEY_REFERENCE_DATE = 'Date';
const KEY_OVERALL_ERRORS = 'Overall-Errors';
const KEY_CLASSES_COMPLEXITY = 'Classes-Cognitive-Complexity';
const KEY_DEPRECATIONS = 'Deprecations';
const KEY_INVALID_PHPDOCS = 'Invalid-Phpdocs';
const KEY_UNKNOWN_TYPES = 'Unknown-Types';
const KEY_ANONYMOUS_VARIABLES = 'Anonymous-Variables';
const KEY_PROPERTY_TYPE_COVERAGE = 'Native-Property-Type-Coverage';
const KEY_PARAM_TYPE_COVERAGE = 'Native-Param-Type-Coverage';
const KEY_RETURN_TYPE_COVERAGE = 'Native-Return-Type-Coverage';
const KEY_UNUSED_SYMBOLS = 'Unused-Symbols';
/**
* @phpstan-assert-if-true self::KEY_* $filterString
*/
public static function isFilterKey(string $filterString): bool
{
return in_array($filterString, self::getFilterKeys(), true);
}
/**
* @return array<self::KEY_*>
*/
public static function getFilterKeys(): array {
return [
self::KEY_CLASSES_COMPLEXITY,
self::KEY_DEPRECATIONS,
self::KEY_INVALID_PHPDOCS,
self::KEY_UNKNOWN_TYPES,
self::KEY_ANONYMOUS_VARIABLES,
self::KEY_UNUSED_SYMBOLS,
];
}
/**
* @return Iterator<string>
*/
public function streamText(Baseline $baseline, AnalyzerResult $result): Iterator
{
$referenceDate = '';
if ($result->referenceDate !== null) {
$referenceDate = $result->referenceDate->format(ResultPrinter::DATE_FORMAT);
}
yield sprintf("Analyzing %s\n", $baseline->getFilePath());
yield sprintf(" %s: %s\n", self::KEY_REFERENCE_DATE, $referenceDate);
yield sprintf(" %s: %s\n", self::KEY_OVERALL_ERRORS, $result->overallErrors);
yield sprintf(" %s: %s\n", self::KEY_CLASSES_COMPLEXITY, $result->classesComplexity);
yield sprintf(" %s: %s\n", self::KEY_DEPRECATIONS, $result->deprecations);
yield sprintf(" %s: %s\n", self::KEY_INVALID_PHPDOCS, $result->invalidPhpdocs);
yield sprintf(" %s: %s\n", self::KEY_UNKNOWN_TYPES, $result->unknownTypes);
yield sprintf(" %s: %s\n", self::KEY_ANONYMOUS_VARIABLES, $result->anonymousVariables);
yield sprintf(" %s: %s\n", self::KEY_PROPERTY_TYPE_COVERAGE, $result->propertyTypeCoverage);
yield sprintf(" %s: %s\n", self::KEY_PARAM_TYPE_COVERAGE, $result->paramTypeCoverage);
yield sprintf(" %s: %s\n", self::KEY_RETURN_TYPE_COVERAGE, $result->returnTypeCoverage);
yield sprintf(" %s: %s\n", self::KEY_UNUSED_SYMBOLS, $result->unusedSymbols);
}
/**
* @return Iterator<string>
*/
public function streamJson(Baseline $baseline, AnalyzerResult $result): Iterator
{
$referenceDate = null;
if ($result->referenceDate !== null) {
$referenceDate = $result->referenceDate->format(ResultPrinter::DATE_FORMAT);
}
yield json_encode([
$baseline->getFilePath() => [
self::KEY_REFERENCE_DATE => $referenceDate,
self::KEY_OVERALL_ERRORS => $result->overallErrors,
self::KEY_CLASSES_COMPLEXITY => $result->classesComplexity,
self::KEY_DEPRECATIONS => $result->deprecations,
self::KEY_INVALID_PHPDOCS => $result->invalidPhpdocs,
self::KEY_UNKNOWN_TYPES => $result->unknownTypes,
self::KEY_ANONYMOUS_VARIABLES => $result->anonymousVariables,
self::KEY_PROPERTY_TYPE_COVERAGE => $result->propertyTypeCoverage,
self::KEY_PARAM_TYPE_COVERAGE => $result->paramTypeCoverage,
self::KEY_RETURN_TYPE_COVERAGE => $result->returnTypeCoverage,
self::KEY_UNUSED_SYMBOLS => $result->unusedSymbols,
]
]);
}
}