| 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\CommandHelper; |
| 9: | use PHPStan\Command\Output; |
| 10: | use PHPStan\DependencyInjection\AutowiredParameter; |
| 11: | use PHPStan\DependencyInjection\AutowiredService; |
| 12: | use PHPStan\File\RelativePathHelper; |
| 13: | use PHPStan\File\SimpleRelativePathHelper; |
| 14: | use Symfony\Component\Console\Formatter\OutputFormatter; |
| 15: | use function array_map; |
| 16: | use function array_slice; |
| 17: | use function count; |
| 18: | use function explode; |
| 19: | use function getenv; |
| 20: | use function implode; |
| 21: | use function in_array; |
| 22: | use function is_string; |
| 23: | use function ltrim; |
| 24: | use function rtrim; |
| 25: | use function sprintf; |
| 26: | use function str_contains; |
| 27: | use function str_replace; |
| 28: | |
| 29: | #[AutowiredService(name: 'errorFormatter.table')] |
| 30: | final class TableErrorFormatter implements ErrorFormatter |
| 31: | { |
| 32: | |
| 33: | public const ERRORS_LIMIT = 1000; |
| 34: | private const FORCE_SHOW_ALL_ERRORS = 'PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS'; |
| 35: | |
| 36: | public function __construct( |
| 37: | private RelativePathHelper $relativePathHelper, |
| 38: | #[AutowiredParameter(ref: '@simpleRelativePathHelper')] |
| 39: | private SimpleRelativePathHelper $simpleRelativePathHelper, |
| 40: | private CiDetectedErrorFormatter $ciDetectedErrorFormatter, |
| 41: | #[AutowiredParameter(ref: '%tipsOfTheDay%')] |
| 42: | private bool $showTipsOfTheDay, |
| 43: | #[AutowiredParameter] |
| 44: | private ?string $editorUrl, |
| 45: | #[AutowiredParameter] |
| 46: | private ?string $editorUrlTitle, |
| 47: | #[AutowiredParameter] |
| 48: | private string $usedLevel, |
| 49: | private ?int $errorsBudget = null, |
| 50: | ) |
| 51: | { |
| 52: | if ($this->errorsBudget !== null) { |
| 53: | return; |
| 54: | } |
| 55: | |
| 56: | $forceShowAll = getenv(self::FORCE_SHOW_ALL_ERRORS); |
| 57: | if (!in_array($forceShowAll, [false, '0'], true)) { |
| 58: | return; |
| 59: | } |
| 60: | |
| 61: | $this->errorsBudget = self::ERRORS_LIMIT; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | public function formatErrors( |
| 66: | AnalysisResult $analysisResult, |
| 67: | Output $output, |
| 68: | ): int |
| 69: | { |
| 70: | $this->ciDetectedErrorFormatter->formatErrors($analysisResult, $output); |
| 71: | $projectConfigFile = 'phpstan.neon'; |
| 72: | if ($analysisResult->getProjectConfigFile() !== null) { |
| 73: | $projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile()); |
| 74: | } |
| 75: | |
| 76: | $style = $output->getStyle(); |
| 77: | |
| 78: | if (!$analysisResult->hasErrors() && !$analysisResult->hasWarnings()) { |
| 79: | $style->success('No errors'); |
| 80: | |
| 81: | if ($this->showTipsOfTheDay) { |
| 82: | if ($analysisResult->isDefaultLevelUsed()) { |
| 83: | $output->writeLineFormatted('💡 Tip of the Day:'); |
| 84: | $output->writeLineFormatted(sprintf( |
| 85: | "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.", |
| 86: | AnalyseCommand::OPTION_LEVEL, |
| 87: | (int) AnalyseCommand::DEFAULT_LEVEL, |
| 88: | )); |
| 89: | $output->writeLineFormatted(''); |
| 90: | } |
| 91: | } |
| 92: | |
| 93: | return 0; |
| 94: | } |
| 95: | |
| 96: | |
| 97: | $fileErrors = []; |
| 98: | foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { |
| 99: | if (!isset($fileErrors[$fileSpecificError->getFile()])) { |
| 100: | $fileErrors[$fileSpecificError->getFile()] = []; |
| 101: | } |
| 102: | |
| 103: | $fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError; |
| 104: | } |
| 105: | |
| 106: | $errorsBudget = $this->errorsBudget; |
| 107: | $printedErrors = 0; |
| 108: | foreach ($fileErrors as $file => $errors) { |
| 109: | $rows = []; |
| 110: | foreach ($errors as $error) { |
| 111: | $message = $error->getMessage(); |
| 112: | $filePath = $error->getTraitFilePath() ?? $error->getFilePath(); |
| 113: | if ($error->getIdentifier() !== null) { |
| 114: | $message .= "\n"; |
| 115: | $message .= '🪪 ' . $error->getIdentifier(); |
| 116: | if (!$error->canBeIgnored()) { |
| 117: | $message .= ' <fg=red>(non-ignorable)</>'; |
| 118: | } |
| 119: | } |
| 120: | if ($error->getTip() !== null) { |
| 121: | $tip = $error->getTip(); |
| 122: | $tip = str_replace('%configurationFile%', $projectConfigFile, $tip); |
| 123: | |
| 124: | $message .= "\n"; |
| 125: | if (str_contains($tip, "\n")) { |
| 126: | $lines = explode("\n", $tip); |
| 127: | foreach ($lines as $line) { |
| 128: | $message .= '💡 ' . ltrim($line, ' •') . "\n"; |
| 129: | } |
| 130: | $message = rtrim($message, "\n"); |
| 131: | } else { |
| 132: | $message .= '💡 ' . $tip; |
| 133: | } |
| 134: | } |
| 135: | |
| 136: | if (getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm') { |
| 137: | $title = $this->simpleRelativePathHelper->getRelativePath($filePath); |
| 138: | $message .= sprintf("\nat %s:%d", $title, $error->getLine() ?? 0); |
| 139: | |
| 140: | } elseif (is_string($this->editorUrl)) { |
| 141: | $url = str_replace( |
| 142: | ['%file%', '%relFile%', '%line%'], |
| 143: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 144: | $this->editorUrl, |
| 145: | ); |
| 146: | |
| 147: | if (is_string($this->editorUrlTitle)) { |
| 148: | $title = str_replace( |
| 149: | ['%file%', '%relFile%', '%line%'], |
| 150: | [$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()], |
| 151: | $this->editorUrlTitle, |
| 152: | ); |
| 153: | } else { |
| 154: | $title = $this->relativePathHelper->getRelativePath($filePath); |
| 155: | } |
| 156: | |
| 157: | $message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $title . '</>'; |
| 158: | } |
| 159: | |
| 160: | if ( |
| 161: | $error->getIdentifier() !== null |
| 162: | && in_array($error->getIdentifier(), ['phpstan.type', 'phpstan.nativeType', 'phpstan.variable', 'phpstan.dumpType', 'phpstan.unknownExpectation'], true) |
| 163: | ) { |
| 164: | $message = '<fg=red>' . $message . '</>'; |
| 165: | } |
| 166: | |
| 167: | $rows[] = [ |
| 168: | $this->formatLineNumber($error->getLine()), |
| 169: | $message, |
| 170: | ]; |
| 171: | } |
| 172: | |
| 173: | $printedErrors += count($rows); |
| 174: | if ($errorsBudget !== null && $printedErrors > $errorsBudget) { |
| 175: | $rows = array_slice($rows, 0, $errorsBudget - ($printedErrors - count($rows))); |
| 176: | |
| 177: | $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows); |
| 178: | break; |
| 179: | } |
| 180: | |
| 181: | $style->table(['Line', $this->relativePathHelper->getRelativePath($file)], $rows); |
| 182: | } |
| 183: | |
| 184: | if (count($analysisResult->getNotFileSpecificErrors()) > 0) { |
| 185: | $style->table(['', 'Error'], array_map(static fn (string $error): array => ['', OutputFormatter::escape($error)], $analysisResult->getNotFileSpecificErrors())); |
| 186: | } |
| 187: | |
| 188: | $warningsCount = count($analysisResult->getWarnings()); |
| 189: | if ($warningsCount > 0) { |
| 190: | $style->table(['', 'Warning'], array_map(static fn (string $warning): array => ['', OutputFormatter::escape($warning)], $analysisResult->getWarnings())); |
| 191: | } |
| 192: | |
| 193: | if ($errorsBudget !== null && $printedErrors > $errorsBudget) { |
| 194: | $style->error(sprintf('Found %s+ errors', $errorsBudget)); |
| 195: | |
| 196: | $note = []; |
| 197: | $note[] = sprintf('Result is limited to the first %d errors', $errorsBudget); |
| 198: | if ($this->usedLevel !== CommandHelper::DEFAULT_LEVEL) { |
| 199: | $note[] = '- Consider lowering the PHPStan level'; |
| 200: | } |
| 201: | $note[] = sprintf('- Pass %s=1 environment variable to show all errors', self::FORCE_SHOW_ALL_ERRORS); |
| 202: | $note[] = '- Consider using PHPStan Pro for more comfortable error browsing'; |
| 203: | $note[] = ' Learn more: https://phpstan.com'; |
| 204: | $style->note(implode("\n", $note)); |
| 205: | } else { |
| 206: | $finalMessage = sprintf($analysisResult->getTotalErrorsCount() === 1 ? 'Found %d error' : 'Found %d errors', $analysisResult->getTotalErrorsCount()); |
| 207: | if ($warningsCount > 0) { |
| 208: | $finalMessage .= sprintf($warningsCount === 1 ? ' and %d warning' : ' and %d warnings', $warningsCount); |
| 209: | } |
| 210: | |
| 211: | if ($analysisResult->getTotalErrorsCount() > 0) { |
| 212: | $style->error($finalMessage); |
| 213: | } else { |
| 214: | $style->warning($finalMessage); |
| 215: | } |
| 216: | } |
| 217: | |
| 218: | return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0; |
| 219: | } |
| 220: | |
| 221: | private function formatLineNumber(?int $lineNumber): string |
| 222: | { |
| 223: | if ($lineNumber === null) { |
| 224: | return ''; |
| 225: | } |
| 226: | |
| 227: | $isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode'; |
| 228: | if ($isRunningInVSCodeTerminal) { |
| 229: | return ':' . $lineNumber; |
| 230: | } |
| 231: | |
| 232: | return (string) $lineNumber; |
| 233: | } |
| 234: | |
| 235: | } |
| 236: | |