| 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: | (int) AnalyseCommand::OPTION_LEVEL, |
| 68: | (int) 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: | |
| 112: | if (getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm') { |
| 113: | $title = $this->simpleRelativePathHelper->getRelativePath($filePath); |
| 114: | $message .= sprintf("\nat %s:%d", $title, $error->getLine() ?? 0); |
| 115: | |
| 116: | } elseif (is_string($this->editorUrl)) { |
| 117: | $url = str_replace( |
| 118: | ['%file%', '%relFile%', '%line%'], |
| 119: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 120: | $this->editorUrl, |
| 121: | ); |
| 122: | |
| 123: | if (is_string($this->editorUrlTitle)) { |
| 124: | $title = str_replace( |
| 125: | ['%file%', '%relFile%', '%line%'], |
| 126: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 127: | $this->editorUrlTitle, |
| 128: | ); |
| 129: | } else { |
| 130: | $title = $this->relativePathHelper->getRelativePath($filePath); |
| 131: | } |
| 132: | |
| 133: | $message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $title . '</>'; |
| 134: | } |
| 135: | |
| 136: | if ( |
| 137: | $error->getIdentifier() !== null |
| 138: | && in_array($error->getIdentifier(), ['phpstan.type', 'phpstan.nativeType', 'phpstan.variable', 'phpstan.dumpType', 'phpstan.unknownExpectation'], true) |
| 139: | ) { |
| 140: | $message = '<fg=red>' . $message . '</>'; |
| 141: | } |
| 142: | |
| 143: | $rows[] = [ |
| 144: | $this->formatLineNumber($error->getLine()), |
| 145: | $message, |
| 146: | ]; |
| 147: | } |
| 148: | |
| 149: | $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows); |
| 150: | } |
| 151: | |
| 152: | if (count($analysisResult->getNotFileSpecificErrors()) > 0) { |
| 153: | $style->table(['', 'Error'], array_map(static fn (string $error): array => ['', OutputFormatter::escape($error)], $analysisResult->getNotFileSpecificErrors())); |
| 154: | } |
| 155: | |
| 156: | $warningsCount = count($analysisResult->getWarnings()); |
| 157: | if ($warningsCount > 0) { |
| 158: | $style->table(['', 'Warning'], array_map(static fn (string $warning): array => ['', OutputFormatter::escape($warning)], $analysisResult->getWarnings())); |
| 159: | } |
| 160: | |
| 161: | $finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount()); |
| 162: | if ($warningsCount > 0) { |
| 163: | $finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount); |
| 164: | } |
| 165: | |
| 166: | if ($analysisResult->getTotalErrorsCount() > 0) { |
| 167: | $style->error($finalMessage); |
| 168: | } else { |
| 169: | $style->warning($finalMessage); |
| 170: | } |
| 171: | |
| 172: | return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0; |
| 173: | } |
| 174: | |
| 175: | private function formatLineNumber(?int $lineNumber): string |
| 176: | { |
| 177: | if ($lineNumber === null) { |
| 178: | return ''; |
| 179: | } |
| 180: | |
| 181: | $isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode'; |
| 182: | if ($isRunningInVSCodeTerminal) { |
| 183: | return ':' . $lineNumber; |
| 184: | } |
| 185: | |
| 186: | return (string) $lineNumber; |
| 187: | } |
| 188: | |
| 189: | } |
| 190: | |