2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2016-10-09 13:15:24 +02:00
|
|
|
|
|
|
|
namespace PhpParser\ErrorHandler;
|
|
|
|
|
|
|
|
use PhpParser\Error;
|
|
|
|
use PhpParser\ErrorHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handler that collects all errors into an array.
|
|
|
|
*
|
|
|
|
* This allows graceful handling of errors.
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
class Collecting implements ErrorHandler {
|
2016-10-09 13:15:24 +02:00
|
|
|
/** @var Error[] Collected errors */
|
2023-08-16 21:18:30 +02:00
|
|
|
private array $errors = [];
|
2016-10-09 13:15:24 +02:00
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
public function handleError(Error $error): void {
|
2016-10-09 13:15:24 +02:00
|
|
|
$this->errors[] = $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get collected errors.
|
|
|
|
*
|
|
|
|
* @return Error[]
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function getErrors(): array {
|
2016-10-09 13:15:24 +02:00
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether there are any errors.
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function hasErrors(): bool {
|
2016-10-09 13:15:24 +02:00
|
|
|
return !empty($this->errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset/clear collected errors.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
public function clearErrors(): void {
|
2016-10-09 13:15:24 +02:00
|
|
|
$this->errors = [];
|
|
|
|
}
|
2018-01-10 15:04:06 -02:00
|
|
|
}
|