Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ExitException
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 getFunction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getArg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Exception class.
4 *
5 * @package automattic/patchwork-redefine-exit
6 */
7
8namespace Automattic\RedefineExit;
9
10use RuntimeException;
11
12/**
13 * Exception to represent the calling of `exit()` or `die()`.
14 */
15class ExitException extends RuntimeException {
16
17    /**
18     * Whether exit or die was called.
19     *
20     * @var string
21     */
22    protected $func;
23
24    /**
25     * Argument passed to exit/die.
26     *
27     * @var string|int|null
28     */
29    protected $arg;
30
31    /**
32     * Constructor.
33     *
34     * @param string          $func 'exit' or 'die'.
35     * @param string|int|null $arg Argument passed to `exit` or `die`.
36     */
37    public function __construct( $func, $arg ) {
38        $this->func = $func;
39        $this->arg  = $arg;
40
41        $code = 0;
42        if ( is_int( $arg ) ) {
43            $code    = $arg;
44            $message = ucfirst( $func ) . " called with code $arg";
45        } elseif ( $arg === '' ) {
46            $message = ucfirst( $func ) . ' called with an empty string';
47        } elseif ( is_string( $arg ) ) {
48            $message = ucfirst( $func ) . " called: $arg";
49        } elseif ( $arg === null ) {
50            $message = ucfirst( $func ) . ' called with no argument';
51        } else {
52            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
53            $message = ucfirst( $func ) . ' called with argument ' . var_export( $arg, true );
54        }
55
56        parent::__construct( $message, $code );
57    }
58
59    /**
60     * Whether `exit` or `die` was called.
61     *
62     * @return string 'exit' or 'die'.
63     */
64    public function getFunction() {
65        return $this->func;
66    }
67
68    /**
69     * Argument passed to `exit` or `die`.
70     *
71     * @return int|string|null
72     */
73    public function getArg() {
74        return $this->arg;
75    }
76}