ErrorOutput.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Console\Output;
  13. use PhpCsFixer\Differ\DiffConsoleFormatter;
  14. use PhpCsFixer\Error\Error;
  15. use PhpCsFixer\Linter\LintingException;
  16. use Symfony\Component\Console\Command\Command;
  17. use Symfony\Component\Console\Formatter\OutputFormatter;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * @readonly
  21. *
  22. * @internal
  23. */
  24. final class ErrorOutput
  25. {
  26. private OutputInterface $output;
  27. private bool $isDecorated;
  28. public function __construct(OutputInterface $output)
  29. {
  30. $this->output = $output;
  31. $this->isDecorated = $output->isDecorated();
  32. }
  33. /**
  34. * @param list<Error> $errors
  35. */
  36. public function listErrors(string $process, array $errors): void
  37. {
  38. $this->output->writeln(['', \sprintf(
  39. 'Files that were not fixed due to errors reported during %s:',
  40. $process
  41. )]);
  42. $showDetails = $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
  43. $showTrace = $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
  44. foreach ($errors as $i => $error) {
  45. $this->output->writeln(\sprintf('%4d) %s', $i + 1, $error->getFilePath()));
  46. $e = $error->getSource();
  47. if (!$showDetails || null === $e) {
  48. continue;
  49. }
  50. $class = \sprintf('[%s]', \get_class($e));
  51. $message = $e->getMessage();
  52. $code = $e->getCode();
  53. if (0 !== $code) {
  54. $message .= " ({$code})";
  55. }
  56. $length = max(\strlen($class), \strlen($message));
  57. $lines = [
  58. '',
  59. $class,
  60. $message,
  61. '',
  62. ];
  63. $this->output->writeln('');
  64. foreach ($lines as $line) {
  65. if (\strlen($line) < $length) {
  66. $line .= str_repeat(' ', $length - \strlen($line));
  67. }
  68. $this->output->writeln(\sprintf(' <error> %s </error>', $this->prepareOutput($line)));
  69. }
  70. if ($showTrace && !$e instanceof LintingException) { // stack trace of lint exception is of no interest
  71. $this->output->writeln('');
  72. $stackTrace = $e->getTrace();
  73. foreach ($stackTrace as $trace) {
  74. if (isset($trace['class']) && Command::class === $trace['class'] && 'run' === $trace['function']) {
  75. $this->output->writeln(' [ ... ]');
  76. break;
  77. }
  78. $this->outputTrace($trace);
  79. }
  80. }
  81. if (Error::TYPE_LINT === $error->getType() && 0 < \count($error->getAppliedFixers())) {
  82. $this->output->writeln('');
  83. $this->output->writeln(\sprintf(' Applied fixers: <comment>%s</comment>', implode(', ', $error->getAppliedFixers())));
  84. $diff = $error->getDiff();
  85. if (null !== $diff) {
  86. $diffFormatter = new DiffConsoleFormatter(
  87. $this->isDecorated,
  88. \sprintf(
  89. '<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
  90. PHP_EOL,
  91. PHP_EOL
  92. )
  93. );
  94. $this->output->writeln($diffFormatter->format($diff));
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * @param array{
  101. * function?: string,
  102. * line?: int,
  103. * file?: string,
  104. * class?: class-string,
  105. * type?: '::'|'->',
  106. * args?: mixed[],
  107. * object?: object,
  108. * } $trace
  109. */
  110. private function outputTrace(array $trace): void
  111. {
  112. if (isset($trace['class'], $trace['type'], $trace['function'])) {
  113. $this->output->writeln(\sprintf(
  114. ' <comment>%s</comment>%s<comment>%s()</comment>',
  115. $this->prepareOutput($trace['class']),
  116. $this->prepareOutput($trace['type']),
  117. $this->prepareOutput($trace['function'])
  118. ));
  119. } elseif (isset($trace['function'])) {
  120. $this->output->writeln(\sprintf(' <comment>%s()</comment>', $this->prepareOutput($trace['function'])));
  121. }
  122. if (isset($trace['file'])) {
  123. $this->output->writeln(\sprintf(' in <info>%s</info> at line <info>%d</info>', $this->prepareOutput($trace['file']), $trace['line']));
  124. }
  125. }
  126. private function prepareOutput(string $string): string
  127. {
  128. return $this->isDecorated
  129. ? OutputFormatter::escape($string)
  130. : $string;
  131. }
  132. }