| 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_key_exists; |
| 13: | use function array_map; |
| 14: | use function count; |
| 15: | use function explode; |
| 16: | use function getenv; |
| 17: | use function in_array; |
| 18: | use function is_string; |
| 19: | use function ltrim; |
| 20: | use function sprintf; |
| 21: | use function str_contains; |
| 22: | use function str_replace; |
| 23: | |
| 24: | 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: | $outputIdentifiers = $output->isVerbose(); |
| 73: | $outputIdentifiersInFile = []; |
| 74: | foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { |
| 75: | if (!isset($fileErrors[$fileSpecificError->getFile()])) { |
| 76: | $fileErrors[$fileSpecificError->getFile()] = []; |
| 77: | } |
| 78: | |
| 79: | $fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError; |
| 80: | if ($outputIdentifiers) { |
| 81: | continue; |
| 82: | } |
| 83: | |
| 84: | $filePath = $fileSpecificError->getTraitFilePath() ?? $fileSpecificError->getFilePath(); |
| 85: | if (array_key_exists($filePath, $outputIdentifiersInFile)) { |
| 86: | continue; |
| 87: | } |
| 88: | |
| 89: | if ($fileSpecificError->getIdentifier() === null) { |
| 90: | continue; |
| 91: | } |
| 92: | |
| 93: | if (!in_array($fileSpecificError->getIdentifier(), [ |
| 94: | 'ignore.unmatchedIdentifier', |
| 95: | 'ignore.parseError', |
| 96: | 'ignore.unmatched', |
| 97: | ], true)) { |
| 98: | continue; |
| 99: | } |
| 100: | |
| 101: | $outputIdentifiersInFile[$filePath] = true; |
| 102: | } |
| 103: | |
| 104: | foreach ($fileErrors as $file => $errors) { |
| 105: | $rows = []; |
| 106: | foreach ($errors as $error) { |
| 107: | $message = $error->getMessage(); |
| 108: | $filePath = $error->getTraitFilePath() ?? $error->getFilePath(); |
| 109: | if (($outputIdentifiers || array_key_exists($filePath, $outputIdentifiersInFile)) && $error->getIdentifier() !== null && $error->canBeIgnored()) { |
| 110: | $message .= "\n"; |
| 111: | $message .= '🪪 ' . $error->getIdentifier(); |
| 112: | } |
| 113: | if ($error->getTip() !== null) { |
| 114: | $tip = $error->getTip(); |
| 115: | $tip = str_replace('%configurationFile%', $projectConfigFile, $tip); |
| 116: | |
| 117: | $message .= "\n"; |
| 118: | if (str_contains($tip, "\n")) { |
| 119: | $lines = explode("\n", $tip); |
| 120: | foreach ($lines as $line) { |
| 121: | $message .= '💡 ' . ltrim($line, ' •') . "\n"; |
| 122: | } |
| 123: | } else { |
| 124: | $message .= '💡 ' . $tip; |
| 125: | } |
| 126: | } |
| 127: | if (is_string($this->editorUrl)) { |
| 128: | $url = str_replace( |
| 129: | ['%file%', '%relFile%', '%line%'], |
| 130: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 131: | $this->editorUrl, |
| 132: | ); |
| 133: | |
| 134: | if (is_string($this->editorUrlTitle)) { |
| 135: | $title = str_replace( |
| 136: | ['%file%', '%relFile%', '%line%'], |
| 137: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 138: | $this->editorUrlTitle, |
| 139: | ); |
| 140: | } else { |
| 141: | $title = $this->relativePathHelper->getRelativePath($filePath); |
| 142: | } |
| 143: | |
| 144: | $message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $title . '</>'; |
| 145: | } |
| 146: | $rows[] = [ |
| 147: | $this->formatLineNumber($error->getLine()), |
| 148: | $message, |
| 149: | ]; |
| 150: | } |
| 151: | |
| 152: | $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows); |
| 153: | } |
| 154: | |
| 155: | if (count($analysisResult->getNotFileSpecificErrors()) > 0) { |
| 156: | $style->table(['', 'Error'], array_map(static fn (string $error): array => ['', OutputFormatter::escape($error)], $analysisResult->getNotFileSpecificErrors())); |
| 157: | } |
| 158: | |
| 159: | $warningsCount = count($analysisResult->getWarnings()); |
| 160: | if ($warningsCount > 0) { |
| 161: | $style->table(['', 'Warning'], array_map(static fn (string $warning): array => ['', OutputFormatter::escape($warning)], $analysisResult->getWarnings())); |
| 162: | } |
| 163: | |
| 164: | $finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount()); |
| 165: | if ($warningsCount > 0) { |
| 166: | $finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount); |
| 167: | } |
| 168: | |
| 169: | if ($analysisResult->getTotalErrorsCount() > 0) { |
| 170: | $style->error($finalMessage); |
| 171: | } else { |
| 172: | $style->warning($finalMessage); |
| 173: | } |
| 174: | |
| 175: | return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0; |
| 176: | } |
| 177: | |
| 178: | private function formatLineNumber(?int $lineNumber): string |
| 179: | { |
| 180: | if ($lineNumber === null) { |
| 181: | return ''; |
| 182: | } |
| 183: | |
| 184: | $isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode'; |
| 185: | if ($isRunningInVSCodeTerminal) { |
| 186: | return ':' . $lineNumber; |
| 187: | } |
| 188: | |
| 189: | return (string) $lineNumber; |
| 190: | } |
| 191: | |
| 192: | } |
| 193: | |