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