|
| 1 | +<?php |
| 2 | + |
| 3 | +$exceptionHandler = function (Throwable $e) { |
| 4 | + static $fh; |
| 5 | + if (is_null($fh)) { |
| 6 | + $fh = fopen(__DIR__ . '/app.log', 'a'); |
| 7 | + if (!$fh) { |
| 8 | + echo 'Unable to access the log file.', PHP_EOL; |
| 9 | + exit(1); |
| 10 | + } |
| 11 | + } |
| 12 | + $message = sprintf('%s [%d]: %s', get_class($e), $e->getCode(), $e->getMessage()); |
| 13 | + $msgLength = mb_strlen($message); |
| 14 | + $line = str_repeat('-', $msgLength); |
| 15 | + $logMessage = sprintf( |
| 16 | + "%s\n%s\n> File: %s\n> Line: %d\n> Trace: %s\n%s\n", |
| 17 | + $line, |
| 18 | + $message, |
| 19 | + $e->getFile(), |
| 20 | + $e->getLine(), |
| 21 | + $e->getTraceAsString(), |
| 22 | + $line |
| 23 | + ); |
| 24 | + fwrite($fh, $logMessage); |
| 25 | +}; |
| 26 | + |
| 27 | +$errorHandler = function (int $code, string $message, string $file, int $line) use ($exceptionHandler) { |
| 28 | + $exception = new ErrorException($message, $code, $code, $file, $line); |
| 29 | + $exceptionHandler($exception); |
| 30 | + if (in_array($code, [E_ERROR, E_RECOVERABLE_ERROR, E_USER_ERROR])) { |
| 31 | + exit(1); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +set_error_handler($errorHandler); |
| 36 | +set_exception_handler($exceptionHandler); |
| 37 | + |
| 38 | +class NotANumber extends Exception |
| 39 | +{ |
| 40 | +} |
| 41 | + |
| 42 | +class DecimalNumber extends Exception |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +class NumberIsZeroOrNegative extends Exception |
| 47 | +{ |
| 48 | +} |
| 49 | + |
| 50 | +function printError(string $message): void |
| 51 | +{ |
| 52 | + echo '(!) ', $message, PHP_EOL; |
| 53 | +} |
| 54 | + |
| 55 | +function calculateFactorial($number): int |
| 56 | +{ |
| 57 | + if (!is_numeric($number)) { |
| 58 | + throw new NotANumber(sprintf('%s is not a number.', $number)); |
| 59 | + } |
| 60 | + $number = $number * 1; |
| 61 | + if (is_float($number)) { |
| 62 | + throw new DecimalNumber(sprintf('%s is decimal; integer is expected.', $number)); |
| 63 | + } |
| 64 | + if ($number < 1) { |
| 65 | + throw new NumberIsZeroOrNegative(sprintf('Given %d while higher than zero is expected.', $number)); |
| 66 | + } |
| 67 | + $factorial = 1; |
| 68 | + for ($i = 2; $i <= $number; $i++) { |
| 69 | + $factorial *= $i; |
| 70 | + } |
| 71 | + return $factorial; |
| 72 | +} |
| 73 | + |
| 74 | +$arguments = array_slice($argv, 1); |
| 75 | +if (!count($arguments)) { |
| 76 | + printError('At least one number is required.'); |
| 77 | +} else { |
| 78 | + foreach ($arguments as $argument) { |
| 79 | + try { |
| 80 | + $factorial = calculateFactorial($argument); |
| 81 | + echo $argument, '! = ', $factorial, PHP_EOL; |
| 82 | + } catch (NotANumber | DecimalNumber | NumberIsZeroOrNegative $e) { |
| 83 | + printError(sprintf('[%s]: %s', get_class($e), $e->getMessage())); |
| 84 | + } catch (Throwable $e) { |
| 85 | + printError("Unexpected error occured for [$argument] input number."); |
| 86 | + $exceptionHandler($e); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments