/
TableErrorFormatter.php
150 lines (123 loc) · 4.62 KB
/
TableErrorFormatter.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
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
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Command\AnalyseCommand;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
class TableErrorFormatter implements ErrorFormatter
{
/** @var RelativePathHelper */
private $relativePathHelper;
/** @var bool */
private $showTipsOfTheDay;
/** @var bool */
private $checkThisOnly;
/** @var bool */
private $inferPrivatePropertyTypeFromConstructor;
/** @var bool */
private $checkMissingTypehints;
public function __construct(
RelativePathHelper $relativePathHelper,
bool $showTipsOfTheDay,
bool $checkThisOnly,
bool $inferPrivatePropertyTypeFromConstructor,
bool $checkMissingTypehints
)
{
$this->relativePathHelper = $relativePathHelper;
$this->showTipsOfTheDay = $showTipsOfTheDay;
$this->checkThisOnly = $checkThisOnly;
$this->inferPrivatePropertyTypeFromConstructor = $inferPrivatePropertyTypeFromConstructor;
$this->checkMissingTypehints = $checkMissingTypehints;
}
public function formatErrors(
AnalysisResult $analysisResult,
Output $output
): int
{
$projectConfigFile = 'phpstan.neon';
if ($analysisResult->getProjectConfigFile() !== null) {
$projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile());
}
$style = $output->getStyle();
$showInferPropertiesTip = function () use ($output, $analysisResult, $projectConfigFile): void {
if (
$this->checkThisOnly
|| !$analysisResult->hasInferrablePropertyTypesFromConstructor()
|| $this->inferPrivatePropertyTypeFromConstructor
) {
return;
}
$output->writeLineFormatted('💡 Tip of the Day:');
$output->writeLineFormatted("One or more properties in your code do not have a phpDoc with a type\nbut it could be inferred from the constructor to find more bugs.");
$output->writeLineFormatted(sprintf('Use <fg=cyan>inferPrivatePropertyTypeFromConstructor: true</> in your <fg=cyan>%s</> to try it out!', $projectConfigFile));
$output->writeLineFormatted('');
};
if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) {
$style->success('No errors');
if ($this->showTipsOfTheDay) {
if ($analysisResult->isDefaultLevelUsed()) {
$output->writeLineFormatted('💡 Tip of the Day:');
$output->writeLineFormatted(sprintf(
"PHPStan is performing only the most basic checks.\nYou can pass a higher rule level through the <fg=cyan>--%s</> option\n(the default and current level is %d) to analyse code more thoroughly.",
AnalyseCommand::OPTION_LEVEL,
AnalyseCommand::DEFAULT_LEVEL
));
$output->writeLineFormatted('');
} else {
$showInferPropertiesTip();
}
}
return 0;
}
/** @var array<string, \PHPStan\Analyser\Error[]> $fileErrors */
$fileErrors = [];
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
if (!isset($fileErrors[$fileSpecificError->getFile()])) {
$fileErrors[$fileSpecificError->getFile()] = [];
}
$fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError;
}
foreach ($fileErrors as $file => $errors) {
$rows = [];
foreach ($errors as $error) {
$message = $error->getMessage();
if ($error->getTip() !== null) {
$tip = $error->getTip();
$tip = str_replace('%configurationFile%', $projectConfigFile, $tip);
$message .= "\n💡 " . $tip;
}
$rows[] = [
(string) $error->getLine(),
$message,
];
}
$relativeFilePath = $this->relativePathHelper->getRelativePath($file);
$style->table(['Line', $relativeFilePath], $rows);
}
if (count($analysisResult->getNotFileSpecificErrors()) > 0) {
$style->table(['', 'Error'], array_map(static function (string $error): array {
return ['', $error];
}, $analysisResult->getNotFileSpecificErrors()));
}
$warningsCount = count($analysisResult->getWarnings());
if ($warningsCount > 0) {
$style->table(['', 'Warning'], array_map(static function (string $warning): array {
return ['', $warning];
}, $analysisResult->getWarnings()));
}
$finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount());
if ($warningsCount > 0) {
$finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount);
}
if ($analysisResult->getTotalErrorsCount() > 0) {
$style->error($finalMessage);
} else {
$style->warning($finalMessage);
}
if ($this->checkMissingTypehints && $this->showTipsOfTheDay) {
$showInferPropertiesTip();
}
return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0;
}
}