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 in_array; |
17: | use function is_string; |
18: | use function ltrim; |
19: | use function rtrim; |
20: | use function sprintf; |
21: | use function str_contains; |
22: | use function str_replace; |
23: | |
24: | final class TableErrorFormatter implements ErrorFormatter |
25: | { |
26: | |
27: | public function __construct( |
28: | private RelativePathHelper $relativePathHelper, |
29: | private SimpleRelativePathHelper $simpleRelativePathHelper, |
30: | private CiDetectedErrorFormatter $ciDetectedErrorFormatter, |
31: | private bool $showTipsOfTheDay, |
32: | private ?string $editorUrl, |
33: | private ?string $editorUrlTitle, |
34: | ) |
35: | { |
36: | } |
37: | |
38: | |
39: | public function formatErrors( |
40: | AnalysisResult $analysisResult, |
41: | Output $output, |
42: | ): int |
43: | { |
44: | $this->ciDetectedErrorFormatter->formatErrors($analysisResult, $output); |
45: | $projectConfigFile = 'phpstan.neon'; |
46: | if ($analysisResult->getProjectConfigFile() !== null) { |
47: | $projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile()); |
48: | } |
49: | |
50: | $style = $output->getStyle(); |
51: | |
52: | if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) { |
53: | $style->success('No errors'); |
54: | |
55: | if ($this->showTipsOfTheDay) { |
56: | if ($analysisResult->isDefaultLevelUsed()) { |
57: | $output->writeLineFormatted('💡 Tip of the Day:'); |
58: | $output->writeLineFormatted(sprintf( |
59: | "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.", |
60: | AnalyseCommand::OPTION_LEVEL, |
61: | AnalyseCommand::DEFAULT_LEVEL, |
62: | )); |
63: | $output->writeLineFormatted(''); |
64: | } |
65: | } |
66: | |
67: | return 0; |
68: | } |
69: | |
70: | |
71: | $fileErrors = []; |
72: | foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { |
73: | if (!isset($fileErrors[$fileSpecificError->getFile()])) { |
74: | $fileErrors[$fileSpecificError->getFile()] = []; |
75: | } |
76: | |
77: | $fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError; |
78: | } |
79: | |
80: | foreach ($fileErrors as $file => $errors) { |
81: | $rows = []; |
82: | foreach ($errors as $error) { |
83: | $message = $error->getMessage(); |
84: | $filePath = $error->getTraitFilePath() ?? $error->getFilePath(); |
85: | if ($error->getIdentifier() !== null && $error->canBeIgnored()) { |
86: | $message .= "\n"; |
87: | $message .= '🪪 ' . $error->getIdentifier(); |
88: | } |
89: | if ($error->getTip() !== null) { |
90: | $tip = $error->getTip(); |
91: | $tip = str_replace('%configurationFile%', $projectConfigFile, $tip); |
92: | |
93: | $message .= "\n"; |
94: | if (str_contains($tip, "\n")) { |
95: | $lines = explode("\n", $tip); |
96: | foreach ($lines as $line) { |
97: | $message .= '💡 ' . ltrim($line, ' •') . "\n"; |
98: | } |
99: | $message = rtrim($message, "\n"); |
100: | } else { |
101: | $message .= '💡 ' . $tip; |
102: | } |
103: | } |
104: | if (is_string($this->editorUrl)) { |
105: | $url = str_replace( |
106: | ['%file%', '%relFile%', '%line%'], |
107: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
108: | $this->editorUrl, |
109: | ); |
110: | |
111: | if (is_string($this->editorUrlTitle)) { |
112: | $title = str_replace( |
113: | ['%file%', '%relFile%', '%line%'], |
114: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
115: | $this->editorUrlTitle, |
116: | ); |
117: | } else { |
118: | $title = $this->relativePathHelper->getRelativePath($filePath); |
119: | } |
120: | |
121: | $message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $title . '</>'; |
122: | } |
123: | |
124: | if ( |
125: | $error->getIdentifier() !== null |
126: | && in_array($error->getIdentifier(), ['phpstan.type', 'phpstan.nativeType', 'phpstan.variable', 'phpstan.dumpType', 'phpstan.unknownExpectation'], true) |
127: | ) { |
128: | $message = '<fg=red>' . $message . '</>'; |
129: | } |
130: | |
131: | $rows[] = [ |
132: | $this->formatLineNumber($error->getLine()), |
133: | $message, |
134: | ]; |
135: | } |
136: | |
137: | $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows); |
138: | } |
139: | |
140: | if (count($analysisResult->getNotFileSpecificErrors()) > 0) { |
141: | $style->table(['', 'Error'], array_map(static fn (string $error): array => ['', OutputFormatter::escape($error)], $analysisResult->getNotFileSpecificErrors())); |
142: | } |
143: | |
144: | $warningsCount = count($analysisResult->getWarnings()); |
145: | if ($warningsCount > 0) { |
146: | $style->table(['', 'Warning'], array_map(static fn (string $warning): array => ['', OutputFormatter::escape($warning)], $analysisResult->getWarnings())); |
147: | } |
148: | |
149: | $finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount()); |
150: | if ($warningsCount > 0) { |
151: | $finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount); |
152: | } |
153: | |
154: | if ($analysisResult->getTotalErrorsCount() > 0) { |
155: | $style->error($finalMessage); |
156: | } else { |
157: | $style->warning($finalMessage); |
158: | } |
159: | |
160: | return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0; |
161: | } |
162: | |
163: | private function formatLineNumber(?int $lineNumber): string |
164: | { |
165: | if ($lineNumber === null) { |
166: | return ''; |
167: | } |
168: | |
169: | $isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode'; |
170: | if ($isRunningInVSCodeTerminal) { |
171: | return ':' . $lineNumber; |
172: | } |
173: | |
174: | return (string) $lineNumber; |
175: | } |
176: | |
177: | } |
178: | |