Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ErrorLogErrorReporter | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| report | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of fast-forward/defer. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/defer |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Defer\ErrorReporter; |
| 20 | |
| 21 | use Throwable; |
| 22 | use FastForward\Defer\ErrorReporterInterface; |
| 23 | use FastForward\Defer\Support\CallbackDescriber; |
| 24 | |
| 25 | /** |
| 26 | * This error reporter implementation MUST log all reported exceptions using error_log. |
| 27 | * It SHALL provide a detailed log message including the exception class, message, file, line, and callback description. |
| 28 | * This class MUST NOT throw exceptions during reporting. |
| 29 | */ |
| 30 | final class ErrorLogErrorReporter implements ErrorReporterInterface |
| 31 | { |
| 32 | /** |
| 33 | * Reports a throwable using error_log. |
| 34 | * |
| 35 | * This method MUST log the exception details and callback description. It MUST NOT throw exceptions. |
| 36 | * |
| 37 | * @param Throwable $throwable the exception or error to report |
| 38 | * @param callable|null $callback the related callback, if available |
| 39 | * @param array $args arguments passed to the callback, if any |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | public function report(Throwable $throwable, ?callable $callback = null, array $args = []): void |
| 44 | { |
| 45 | error_log( |
| 46 | \sprintf( |
| 47 | '[%s] Deferred callback failed: %s: %s in %s:%d | callback=%s', |
| 48 | self::class, |
| 49 | $throwable::class, |
| 50 | $throwable->getMessage(), |
| 51 | $throwable->getFile(), |
| 52 | $throwable->getLine(), |
| 53 | null !== $callback ? CallbackDescriber::describe($callback) : 'unknown' |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | } |