Collecting.php 880 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. {
  12. /** @var Error[] Collected errors */
  13. private $errors = [];
  14. public function handleError(Error $error) {
  15. $this->errors[] = $error;
  16. }
  17. /**
  18. * Get collected errors.
  19. *
  20. * @return Error[]
  21. */
  22. public function getErrors() : array {
  23. return $this->errors;
  24. }
  25. /**
  26. * Check whether there are any errors.
  27. *
  28. * @return bool
  29. */
  30. public function hasErrors() : bool {
  31. return !empty($this->errors);
  32. }
  33. /**
  34. * Reset/clear collected errors.
  35. */
  36. public function clearErrors() {
  37. $this->errors = [];
  38. }
  39. }