Error.php 2.9 KB

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