1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Command\ErrorFormatter;
4:
5: use PHPStan\Analyser\Error;
6: use PHPStan\Command\AnalyseCommand;
7: use PHPStan\Command\AnalysisResult;
8: use PHPStan\Command\Output;
9: use PHPStan\File\RelativePathHelper;
10: use PHPStan\File\SimpleRelativePathHelper;
11: use Symfony\Component\Console\Formatter\OutputFormatter;
12: use function array_map;
13: use function count;
14: use function explode;
15: use function getenv;
16: use function is_string;
17: use function ltrim;
18: use function sprintf;
19: use function str_contains;
20: use function str_replace;
21:
22: class TableErrorFormatter implements ErrorFormatter
23: {
24:
25: public function __construct(
26: private RelativePathHelper $relativePathHelper,
27: private SimpleRelativePathHelper $simpleRelativePathHelper,
28: private CiDetectedErrorFormatter $ciDetectedErrorFormatter,
29: private bool $showTipsOfTheDay,
30: private ?string $editorUrl,
31: private ?string $editorUrlTitle,
32: )
33: {
34: }
35:
36: /** @api */
37: public function formatErrors(
38: AnalysisResult $analysisResult,
39: Output $output,
40: ): int
41: {
42: $this->ciDetectedErrorFormatter->formatErrors($analysisResult, $output);
43: $projectConfigFile = 'phpstan.neon';
44: if ($analysisResult->getProjectConfigFile() !== null) {
45: $projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile());
46: }
47:
48: $style = $output->getStyle();
49:
50: if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) {
51: $style->success('No errors');
52:
53: if ($this->showTipsOfTheDay) {
54: if ($analysisResult->isDefaultLevelUsed()) {
55: $output->writeLineFormatted('šŸ’” Tip of the Day:');
56: $output->writeLineFormatted(sprintf(
57: "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.",
58: AnalyseCommand::OPTION_LEVEL,
59: AnalyseCommand::DEFAULT_LEVEL,
60: ));
61: $output->writeLineFormatted('');
62: }
63: }
64:
65: return 0;
66: }
67:
68: /** @var array<string, Error[]> $fileErrors */
69: $fileErrors = [];
70: foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
71: if (!isset($fileErrors[$fileSpecificError->getFile()])) {
72: $fileErrors[$fileSpecificError->getFile()] = [];
73: }
74:
75: $fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError;
76: }
77:
78: foreach ($fileErrors as $file => $errors) {
79: $rows = [];
80: foreach ($errors as $error) {
81: $message = $error->getMessage();
82: if ($error->getTip() !== null) {
83: $tip = $error->getTip();
84: $tip = str_replace('%configurationFile%', $projectConfigFile, $tip);
85:
86: $message .= "\n";
87: if (str_contains($tip, "\n")) {
88: $lines = explode("\n", $tip);
89: foreach ($lines as $line) {
90: $message .= 'šŸ’” ' . ltrim($line, ' ā€¢') . "\n";
91: }
92: } else {
93: $message .= 'šŸ’” ' . $tip;
94: }
95: }
96: if (is_string($this->editorUrl)) {
97: $editorFile = $error->getTraitFilePath() ?? $error->getFilePath();
98: $url = str_replace(
99: ['%file%', '%relFile%', '%line%'],
100: [$editorFile, $this->simpleRelativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
101: $this->editorUrl,
102: );
103:
104: if (is_string($this->editorUrlTitle)) {
105: $title = str_replace(
106: ['%file%', '%relFile%', '%line%'],
107: [$editorFile, $this->simpleRelativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
108: $this->editorUrlTitle,
109: );
110: } else {
111: $title = $this->relativePathHelper->getRelativePath($editorFile);
112: }
113:
114: $message .= "\nāœļø <href=" . OutputFormatter::escape($url) . '>' . $title . '</>';
115: }
116: $rows[] = [
117: $this->formatLineNumber($error->getLine()),
118: $message,
119: ];
120: }
121:
122: $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows);
123: }
124:
125: if (count($analysisResult->getNotFileSpecificErrors()) > 0) {
126: $style->table(['', 'Error'], array_map(static fn (string $error): array => ['', $error], $analysisResult->getNotFileSpecificErrors()));
127: }
128:
129: $warningsCount = count($analysisResult->getWarnings());
130: if ($warningsCount > 0) {
131: $style->table(['', 'Warning'], array_map(static fn (string $warning): array => ['', $warning], $analysisResult->getWarnings()));
132: }
133:
134: $finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount());
135: if ($warningsCount > 0) {
136: $finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount);
137: }
138:
139: if ($analysisResult->getTotalErrorsCount() > 0) {
140: $style->error($finalMessage);
141: } else {
142: $style->warning($finalMessage);
143: }
144:
145: return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0;
146: }
147:
148: private function formatLineNumber(?int $lineNumber): string
149: {
150: if ($lineNumber === null) {
151: return '';
152: }
153:
154: $isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode';
155: if ($isRunningInVSCodeTerminal) {
156: return ':' . $lineNumber;
157: }
158:
159: return (string) $lineNumber;
160: }
161:
162: }
163: