ErrorsManager.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * Manager of errors that occur during fixing.
  15. *
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. */
  20. final class ErrorsManager
  21. {
  22. /**
  23. * @var list<Error>
  24. */
  25. private array $errors = [];
  26. /**
  27. * Returns errors reported during linting before fixing.
  28. *
  29. * @return list<Error>
  30. */
  31. public function getInvalidErrors(): array
  32. {
  33. return array_filter($this->errors, static fn (Error $error): bool => Error::TYPE_INVALID === $error->getType());
  34. }
  35. /**
  36. * Returns errors reported during fixing.
  37. *
  38. * @return list<Error>
  39. */
  40. public function getExceptionErrors(): array
  41. {
  42. return array_filter($this->errors, static fn (Error $error): bool => Error::TYPE_EXCEPTION === $error->getType());
  43. }
  44. /**
  45. * Returns errors reported during linting after fixing.
  46. *
  47. * @return list<Error>
  48. */
  49. public function getLintErrors(): array
  50. {
  51. return array_filter($this->errors, static fn (Error $error): bool => Error::TYPE_LINT === $error->getType());
  52. }
  53. /**
  54. * Returns errors reported for specified path.
  55. *
  56. * @return list<Error>
  57. */
  58. public function forPath(string $path): array
  59. {
  60. return array_values(array_filter($this->errors, static fn (Error $error): bool => $path === $error->getFilePath()));
  61. }
  62. /**
  63. * Returns true if no errors were reported.
  64. */
  65. public function isEmpty(): bool
  66. {
  67. return [] === $this->errors;
  68. }
  69. public function report(Error $error): void
  70. {
  71. $this->errors[] = $error;
  72. }
  73. }