-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBaselineAnalyzer.php
More file actions
167 lines (146 loc) · 6.1 KB
/
BaselineAnalyzer.php
File metadata and controls
167 lines (146 loc) · 6.1 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace staabm\PHPStanBaselineAnalysis;
use Safe\DateTimeImmutable;
use function Safe\filemtime;
final class BaselineAnalyzer
{
/**
* @api
* @var string
*/
public const CLASS_COMPLEXITY_ERROR_MESSAGE = 'Class cognitive complexity is %d, keep it under %d';
/**
* @api
* @var string
*/
public const PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible property types, only %d - %.1f %% actually have it. Add more property types to get over %s %%';
/**
* @api
* @var string
*/
public const PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible param types, only %d - %.1f %% actually have it. Add more param types to get over %s %%';
/**
* @api
* @var string
*/
public const RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE = 'Out of %d possible return types, only %d - %.1f %% actually have it. Add more return types to get over %s %%';
/**
* @var Baseline
*/
private $baseline;
private bool $fileMtime;
public function __construct(Baseline $baseline)
{
$this->baseline = $baseline;
}
public function useFileMtime(bool $useFileMtime)
{
$this->fileMtime = $useFileMtime;
}
public function analyze(): AnalyzerResult
{
$result = new AnalyzerResult();
if ($this->fileMtime) {
$result->referenceDate = DateTimeImmutable::createFromFormat("U", (string)filemtime($this->baseline->getFilePath()));
} else {
$result->referenceDate = new DateTimeImmutable();
}
/**
* @var BaselineError $baselineError
*/
foreach ($this->baseline->getIgnoreErrors() as $baselineError) {
// accumulating errors
$result->overallErrors += $baselineError->count;
$result->deprecations += $this->countDeprecations($baselineError);
$result->classesComplexity += $this->countClassesComplexity($baselineError);
$result->invalidPhpdocs += $this->countInvalidPhpdocs($baselineError);
$result->unknownTypes += $this->countUnknownTypes($baselineError);
$result->anonymousVariables += $this->countAnonymousVariables($baselineError);
$result->unusedSymbols += $this->countUnusedSymbols($baselineError);
// project wide errors, only reported once per baseline
$this->checkSeaLevels($result, $baselineError);
}
return $result;
}
private function countDeprecations(BaselineError $baselineError): int
{
return $baselineError->isDeprecationError() ? $baselineError->count : 0;
}
private function countClassesComplexity(BaselineError $baselineError): int
{
if (sscanf($baselineError->unwrapMessage(), self::CLASS_COMPLEXITY_ERROR_MESSAGE, $value, $limit) > 0) {
return (int)$value * $baselineError->count;
}
return 0;
}
private function countInvalidPhpdocs(BaselineError $baselineError): int
{
return $baselineError->isInvalidPhpDocError()
? $baselineError->count
: 0;
}
private function countUnknownTypes(BaselineError $baselineError): int
{
return $baselineError->isUnknownTypeError()
? $baselineError->count
: 0;
}
private function countAnonymousVariables(BaselineError $baselineError): int
{
return $baselineError->isAnonymousVariableError()
? $baselineError->count
: 0;
}
private function countUnusedSymbols(BaselineError $baselineError): int
{
return $baselineError->isUnusedSymbolError()
? $baselineError->count
: 0;
}
private function checkSeaLevels(AnalyzerResult $result, BaselineError $baselineError): void
{
if (
sscanf(
$this->normalizeMessage($baselineError),
$this->printfToScanfFormat(self::PROPERTY_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
) {
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
throw new \LogicException('Invalid property coveragePercent: '. $coveragePercent);
}
$result->propertyTypeCoverage = $coveragePercent;
}
if (
sscanf(
$this->normalizeMessage($baselineError),
$this->printfToScanfFormat(self::PARAM_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
) {
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
throw new \LogicException('Invalid parameter coveragePercent: '. $coveragePercent);
}
$result->paramTypeCoverage = $coveragePercent;
}
if (
sscanf(
$this->normalizeMessage($baselineError),
$this->printfToScanfFormat(self::RETURN_TYPE_DEClARATION_SEA_LEVEL_MESSAGE),
$absoluteCountMin, $coveragePercent, $goalPercent) >= 2
) {
if (!is_int($coveragePercent) || $coveragePercent < 0 || $coveragePercent > 100) {
throw new \LogicException('Invalid return coveragePercent: '. $coveragePercent);
}
$result->returnTypeCoverage = $coveragePercent;
}
}
private function printfToScanfFormat(string $format): string {
// we don't need the float value, therefore simply ignore it, to make the format parseable by sscanf
// see https://github.com/php/php-src/issues/12126
// additionally this makes the output format of tomasvotruba/type-coverage 0.2.* compatible with tomasvotruba/type-coverage 0.1.*
return str_replace('%d - %.1f', '%d', $format);
}
private function normalizeMessage(BaselineError $baselineError): string {
// makes the message format of tomasvotruba/type-coverage 0.2.* compatible with tomasvotruba/type-coverage 0.1.*
return \Safe\preg_replace('/only \d+ \- (\d+).\d %/', 'only $1 %', $baselineError->unwrapMessage());
}
}