Completed
Pull Request — v1 (#422)
by
unknown
03:27
created

Inspector::getFrames()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 35
ccs 20
cts 22
cp 0.9091
rs 8.439
cc 6
eloc 19
nc 7
nop 0
crap 6.027
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Exception;
8
9
use Throwable;
10
11
class Inspector
12
{
13
    /**
14
     * @var Throwable
15
     */
16
    private $exception;
17
18
    /**
19
     * @var \Whoops\Exception\FrameCollection
20
     */
21
    private $frames;
22
23
    /**
24
     * @var \Whoops\Exception\Inspector
25
     */
26
    private $previousExceptionInspector;
27
28
    /**
29
     * @param mixed $exception The exception to inspect
30
     */
31 4
    public function __construct($exception)
32
    {
33 4
        $this->exception = $exception;
34 4
    }
35
36
    /**
37
     * @return Throwable
38
     */
39 3
    public function getException()
40
    {
41 3
        return $this->exception;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 2
    public function getExceptionName()
48
    {
49 2
        return get_class($this->exception);
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getExceptionMessage()
56
    {
57
        return $this->exception->getMessage();
58
    }
59
60
    /**
61
     * Does the wrapped Exception have a previous Exception?
62
     * @return bool
63
     */
64 2
    public function hasPreviousException()
65
    {
66 2
        return $this->previousExceptionInspector || $this->exception->getPrevious();
67
    }
68
69
    /**
70
     * Returns an Inspector for a previous Exception, if any.
71
     * @todo   Clean this up a bit, cache stuff a bit better.
72
     * @return Inspector
73
     */
74 2
    public function getPreviousExceptionInspector()
75
    {
76 2
        if ($this->previousExceptionInspector === null) {
77 2
            $previousException = $this->exception->getPrevious();
78
79 2
            if ($previousException) {
80 1
                $this->previousExceptionInspector = new Inspector($previousException);
81 1
            }
82 2
        }
83
84 2
        return $this->previousExceptionInspector;
85
    }
86
87
    /**
88
     * Returns an iterator for the inspected exception's
89
     * frames.
90
     * @return \Whoops\Exception\FrameCollection
91
     */
92 3
    public function getFrames()
93
    {
94 3
        if ($this->frames === null) {
95 3
            $frames = $this->exception->getTrace();
96
97
            // If we're handling an ErrorException thrown by Whoops,
98
            // get rid of the last frame, which matches the handleError method,
99
            // and do not add the current exception to trace. We ensure that
100
            // the next frame does have a filename / linenumber, though.
101 3
            if ($this->exception instanceof ErrorException && empty($frames[1]['line'])) {
102
                $frames = array($this->getFrameFromError($this->exception));
103
            } else {
104 3
                $firstFrame = $this->getFrameFromException($this->exception);
105 3
                array_unshift($frames, $firstFrame);
106
            }
107 3
            $this->frames = new FrameCollection($frames);
108
109 3
            if ($previousInspector = $this->getPreviousExceptionInspector()) {
110
                // Keep outer frame on top of the inner one
111 1
                $outerFrames = $this->frames;
112 1
                $newFrames = clone $previousInspector->getFrames();
113
                // I assume it will always be set, but let's be safe
114 1
                if (isset($newFrames[0])) {
115 1
                    $newFrames[0]->addComment(
116 1
                        $previousInspector->getExceptionMessage(),
117
                        'Exception message:'
118 1
                    );
119 1
                }
120 1
                $newFrames->prependFrames($outerFrames->topDiff($newFrames));
0 ignored issues
show
Documentation introduced by
$outerFrames->topDiff($newFrames) is of type array<integer,array>, but the function expects a array<integer,object<Whoops\Exception\Frame>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121 1
                $this->frames = $newFrames;
122 1
            }
123 3
        }
124
125 3
        return $this->frames;
126
    }
127
128
    /**
129
     * Given an exception, generates an array in the format
130
     * generated by Throwable::getTrace()
131
     * @param mixed $exception
132
     * @return array
133
     */
134 1
    protected function getFrameFromException($exception)
135
    {
136
        return array(
137 1
            'file'  => $exception->getFile(),
138 1
            'line'  => $exception->getLine(),
139 1
            'class' => get_class($exception),
140
            'args'  => array(
141 1
                $exception->getMessage(),
142 1
            ),
143 1
        );
144
    }
145
146
    /**
147
     * Given an error, generates an array in the format
148
     * generated by ErrorException
149
     * @param  ErrorException $exception
150
     * @return array
151
     */
152
    protected function getFrameFromError(ErrorException $exception)
153
    {
154
        return array(
155
            'file'  => $exception->getFile(),
156
            'line'  => $exception->getLine(),
157
            'class' => null,
158
            'args'  => array(),
159
        );
160
    }
161
}
162