Completed
Push — master ( 804175...5af3ff )
by Boy
9s
created

helpers.php ➔ convert_exception_to_array()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 8
nop 1
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
if (!function_exists('convert_exception_to_array')) {
4
5
    /**
6
     * Converts an \Exception into an array
7
     *
8
     * @param $e
9
     *
10
     * @return array
11
     * @throws Exception
12
     */
13
    function convert_exception_to_array($e)
14
    {
15
        if (PHP_VERSION_ID < 70000) {
16
17
            if (!($e instanceof \Exception)) {
18
19
                throw new \Exception('input must be instance of Exception');
20
            }
21
        } else {
22
23
            if (!($e instanceof \Throwable)) {
24
25
                throw new \Exception('input must be instance of Throwable');
26
            }
27
        }
28
29
        $destination        = new \stdClass();
30
        $destination->class = get_class($e);
31
        foreach ((new \ReflectionObject($e))->getProperties() as $sourceProperty) {
32
33
            if (!in_array($sourceProperty->name, ['messages', 'severity', 'xdebug_message'])) {
34
35
                $sourceProperty->setAccessible(true);
36
                $destination->{$sourceProperty->getName()} = $sourceProperty->getValue($e);
37
            }
38
        }
39
40
        // return exception
41
        return (array)$destination;
42
    }
43
}
44