Collecting.php 869 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\ErrorHandler;
  3. use PhpParser\Error;
  4. use PhpParser\ErrorHandler;
  5. /**
  6. * Error handler that collects all errors into an array.
  7. *
  8. * This allows graceful handling of errors.
  9. */
  10. class Collecting implements ErrorHandler {
  11. /** @var Error[] Collected errors */
  12. private array $errors = [];
  13. public function handleError(Error $error): void {
  14. $this->errors[] = $error;
  15. }
  16. /**
  17. * Get collected errors.
  18. *
  19. * @return Error[]
  20. */
  21. public function getErrors(): array {
  22. return $this->errors;
  23. }
  24. /**
  25. * Check whether there are any errors.
  26. */
  27. public function hasErrors(): bool {
  28. return !empty($this->errors);
  29. }
  30. /**
  31. * Reset/clear collected errors.
  32. */
  33. public function clearErrors(): void {
  34. $this->errors = [];
  35. }
  36. }