| 1 | <?php |
||
| 17 | class ExceptionHelper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The sniffed file. |
||
| 21 | * |
||
| 22 | * @var File |
||
| 23 | */ |
||
| 24 | private File $file; |
||
|
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * ExceptionHelper constructor. |
||
| 28 | * |
||
| 29 | * @param File $file The sniffed file. |
||
| 30 | */ |
||
| 31 | public function __construct(File $file) |
||
| 32 | { |
||
| 33 | $this->file = $file; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Registers the exception as an error or warning on the file. |
||
| 38 | * |
||
| 39 | * @param CodeWarning $exception The error which should be handled. |
||
| 40 | * |
||
| 41 | * @return bool Should this error be fixed? |
||
| 42 | */ |
||
| 43 | public function handleException(CodeWarning $exception): bool |
||
| 44 | { |
||
| 45 | $isError = $exception instanceof CodeError; |
||
| 46 | $isFixable = $exception->isFixable(); |
||
| 47 | $method = 'add'; |
||
| 48 | |||
| 49 | if ($isFixable) { |
||
| 50 | $method .= 'Fixable'; |
||
| 51 | } |
||
| 52 | |||
| 53 | $method .= $isError ? 'Error' : 'Warning'; |
||
| 54 | |||
| 55 | return $this->file->$method( |
||
| 56 | $exception->getMessage(), |
||
| 57 | $exception->getStackPosition(), |
||
| 58 | $exception->getCode(), |
||
| 59 | $exception->getPayload() |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 |