Completed
Pull Request — master (#14)
by Aivis
02:14
created

ExceptionEncoder::stackTraceArgsToArray()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 47
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 22
nc 11
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Understand\UnderstandLaravel5;
2
3
class ExceptionEncoder
4
{
5
6
    /**
7
     * Serialize exception object
8
     *
9
     * @param \Exception $exception
10
     * @return type
11
     */
12
    public function exceptionToArray(\Exception $exception)
13
    {
14
        $trace = $exception->getTrace();
15
        $className = get_class($exception);
16
        $message = $exception->getMessage() ? $exception->getMessage() : $className;
17
18
        return [
19
            'message' => $message,
20
            'class' => $className,
21
            'code' => $exception->getCode(),
22
            'file' => $exception->getFile(),
23
            'line' => $exception->getLine(),
24
            'stack' => $this->stackTraceToArray($trace)
25
        ];
26
    }
27
28
    /**
29
     * Serialize stack trace to array
30
     *
31
     * @param array $stackTrace
32
     * @return array
33
     */
34
    public function stackTraceToArray(array $stackTrace)
35
    {
36
        $stack = [];
37
38
        foreach ($stackTrace as $trace)
39
        {
40
            $type = $this->stackTraceCallToString($trace);
41
            $args = $this->stackTraceArgsToArray($trace);
42
43
            $stack[] = [
44
                'class' => isset($trace['class']) ? $trace['class'] : null,
45
                'function' => isset($trace['function']) ? $trace['function'] : null,
46
                'args' => $args,
47
                'type' => $type,
48
                'file' => $this->getStackTraceFile($trace),
49
                'line' => $this->getStackTraceLine($trace)
50
            ];
51
        }
52
53
        return $stack;
54
    }
55
56
    /**
57
     * Return stack trace line number
58
     *
59
     * @param array $trace
60
     * @return mixed
61
     */
62
    protected function getStackTraceLine(array $trace)
63
    {
64
        if (isset($trace['line']))
65
        {
66
            return $trace['line'];
67
        }
68
    }
69
70
    /**
71
     * Return stack trace file
72
     *
73
     * @param array $trace
74
     * @return mixed
75
     */
76
    protected function getStackTraceFile(array $trace)
77
    {
78
        if (isset($trace['file']))
79
        {
80
            return $trace['file'];
81
        }
82
    }
83
84
    /**
85
     * Return call type
86
     *
87
     * @param array $trace
88
     * @return string
89
     */
90
    protected function stackTraceCallToString(array $trace)
91
    {
92
        if (! isset($trace['type']))
93
        {
94
            return 'function';
95
        }
96
97
        if ($trace['type'] == '::')
98
        {
99
            return 'static';
100
        }
101
102
        if ($trace['type'] == '->')
103
        {
104
            return 'method';
105
        }
106
    }
107
108
    /**
109
     * Serialize stack trace function arguments
110
     *
111
     * @param array $trace
112
     * @return array
113
     */
114
    protected function stackTraceArgsToArray(array $trace)
115
    {
116
        $params = [];
117
118
        if (! isset($trace['args']))
119
        {
120
            return $params;
121
        }
122
123
        foreach ($trace['args'] as $arg)
124
        {
125
            if (is_array($arg))
126
            {
127
                $params[] = 'array(' . count($arg) . ')';
128
            }
129
            else if (is_object($arg))
130
            {
131
                $params[] = get_class($arg);
132
            }
133
            else if (is_string($arg))
134
            {
135
                $params[] = 'string(' . $arg . ')';
136
            }
137
            else if (is_int($arg))
138
            {
139
                $params[] = 'int(' . $arg . ')';
140
            }
141
            else if (is_float($arg))
142
            {
143
                $params[] = 'float(' . $arg . ')';
144
            }
145
            else if (is_bool($arg))
146
            {
147
                $params[] = 'bool(' . ($arg ? 'true' : 'false') . ')';
148
            }
149
            else if ($arg instanceof \__PHP_Incomplete_Class)
150
            {
151
                $params[] = 'object(__PHP_Incomplete_Class)'; 
152
            }
153
            else
154
            {
155
                $params[] = gettype($arg);
156
            }
157
        }
158
159
        return $params;
160
    }
161
162
}
163