Error.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Error;
  13. /**
  14. * An abstraction for errors that can occur before and during fixing.
  15. *
  16. * @author Andreas Möller <am@localheinz.com>
  17. *
  18. * @internal
  19. */
  20. final class Error implements \JsonSerializable
  21. {
  22. /**
  23. * Error which has occurred in linting phase, before applying any fixers.
  24. */
  25. public const TYPE_INVALID = 1;
  26. /**
  27. * Error which has occurred during fixing phase.
  28. */
  29. public const TYPE_EXCEPTION = 2;
  30. /**
  31. * Error which has occurred in linting phase, after applying any fixers.
  32. */
  33. public const TYPE_LINT = 3;
  34. /** @var self::TYPE_* */
  35. private int $type;
  36. private string $filePath;
  37. private ?\Throwable $source;
  38. /**
  39. * @var list<string>
  40. */
  41. private array $appliedFixers;
  42. private ?string $diff;
  43. /**
  44. * @param self::TYPE_* $type
  45. * @param list<string> $appliedFixers
  46. */
  47. public function __construct(int $type, string $filePath, ?\Throwable $source = null, array $appliedFixers = [], ?string $diff = null)
  48. {
  49. $this->type = $type;
  50. $this->filePath = $filePath;
  51. $this->source = $source;
  52. $this->appliedFixers = $appliedFixers;
  53. $this->diff = $diff;
  54. }
  55. public function getFilePath(): string
  56. {
  57. return $this->filePath;
  58. }
  59. public function getSource(): ?\Throwable
  60. {
  61. return $this->source;
  62. }
  63. public function getType(): int
  64. {
  65. return $this->type;
  66. }
  67. /**
  68. * @return list<string>
  69. */
  70. public function getAppliedFixers(): array
  71. {
  72. return $this->appliedFixers;
  73. }
  74. public function getDiff(): ?string
  75. {
  76. return $this->diff;
  77. }
  78. /**
  79. * @return array{
  80. * type: self::TYPE_*,
  81. * filePath: string,
  82. * source: null|array{class: class-string, message: string, code: int, file: string, line: int},
  83. * appliedFixers: list<string>,
  84. * diff: null|string
  85. * }
  86. */
  87. public function jsonSerialize(): array
  88. {
  89. return [
  90. 'type' => $this->type,
  91. 'filePath' => $this->filePath,
  92. 'source' => null !== $this->source
  93. ? [
  94. 'class' => \get_class($this->source),
  95. 'message' => $this->source->getMessage(),
  96. 'code' => $this->source->getCode(),
  97. 'file' => $this->source->getFile(),
  98. 'line' => $this->source->getLine(),
  99. ]
  100. : null,
  101. 'appliedFixers' => $this->appliedFixers,
  102. 'diff' => $this->diff,
  103. ];
  104. }
  105. }