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