ErrorOutput.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * @internal
  21. */
  22. final class ErrorOutput
  23. {
  24. private OutputInterface $output;
  25. /**
  26. * @var bool
  27. */
  28. private $isDecorated;
  29. public function __construct(OutputInterface $output)
  30. {
  31. $this->output = $output;
  32. $this->isDecorated = $output->isDecorated();
  33. }
  34. /**
  35. * @param list<Error> $errors
  36. */
  37. public function listErrors(string $process, array $errors): void
  38. {
  39. $this->output->writeln(['', sprintf(
  40. 'Files that were not fixed due to errors reported during %s:',
  41. $process
  42. )]);
  43. $showDetails = $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
  44. $showTrace = $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
  45. foreach ($errors as $i => $error) {
  46. $this->output->writeln(sprintf('%4d) %s', $i + 1, $error->getFilePath()));
  47. $e = $error->getSource();
  48. if (!$showDetails || null === $e) {
  49. continue;
  50. }
  51. $class = sprintf('[%s]', \get_class($e));
  52. $message = $e->getMessage();
  53. $code = $e->getCode();
  54. if (0 !== $code) {
  55. $message .= " ({$code})";
  56. }
  57. $length = max(\strlen($class), \strlen($message));
  58. $lines = [
  59. '',
  60. $class,
  61. $message,
  62. '',
  63. ];
  64. $this->output->writeln('');
  65. foreach ($lines as $line) {
  66. if (\strlen($line) < $length) {
  67. $line .= str_repeat(' ', $length - \strlen($line));
  68. }
  69. $this->output->writeln(sprintf(' <error> %s </error>', $this->prepareOutput($line)));
  70. }
  71. if ($showTrace && !$e instanceof LintingException) { // stack trace of lint exception is of no interest
  72. $this->output->writeln('');
  73. $stackTrace = $e->getTrace();
  74. foreach ($stackTrace as $trace) {
  75. if (isset($trace['class']) && Command::class === $trace['class'] && 'run' === $trace['function']) {
  76. $this->output->writeln(' [ ... ]');
  77. break;
  78. }
  79. $this->outputTrace($trace);
  80. }
  81. }
  82. if (Error::TYPE_LINT === $error->getType() && 0 < \count($error->getAppliedFixers())) {
  83. $this->output->writeln('');
  84. $this->output->writeln(sprintf(' Applied fixers: <comment>%s</comment>', implode(', ', $error->getAppliedFixers())));
  85. $diff = $error->getDiff();
  86. if (null !== $diff) {
  87. $diffFormatter = new DiffConsoleFormatter(
  88. $this->isDecorated,
  89. sprintf(
  90. '<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
  91. PHP_EOL,
  92. PHP_EOL
  93. )
  94. );
  95. $this->output->writeln($diffFormatter->format($diff));
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * @param array{
  102. * function?: string,
  103. * line?: int,
  104. * file?: string,
  105. * class?: class-string,
  106. * type?: '::'|'->',
  107. * args?: mixed[],
  108. * object?: object,
  109. * } $trace
  110. */
  111. private function outputTrace(array $trace): void
  112. {
  113. if (isset($trace['class'], $trace['type'], $trace['function'])) {
  114. $this->output->writeln(sprintf(
  115. ' <comment>%s</comment>%s<comment>%s()</comment>',
  116. $this->prepareOutput($trace['class']),
  117. $this->prepareOutput($trace['type']),
  118. $this->prepareOutput($trace['function'])
  119. ));
  120. } elseif (isset($trace['function'])) {
  121. $this->output->writeln(sprintf(' <comment>%s()</comment>', $this->prepareOutput($trace['function'])));
  122. }
  123. if (isset($trace['file'])) {
  124. $this->output->writeln(sprintf(' in <info>%s</info> at line <info>%d</info>', $this->prepareOutput($trace['file']), $trace['line']));
  125. }
  126. }
  127. private function prepareOutput(string $string): string
  128. {
  129. return $this->isDecorated
  130. ? OutputFormatter::escape($string)
  131. : $string;
  132. }
  133. }