Completed
Pull Request — master (#519)
by Julián
02:04
created

Formatter::formatExceptionAsDataArray()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
ccs 9
cts 21
cp 0.4286
rs 8.8571
cc 3
eloc 19
nc 2
nop 2
crap 4.679
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
10
use Whoops\Handler\PlainTextHandler;
11
12
class Formatter
13
{
14
    /**
15
     * Returns all basic information about the exception in a simple array
16
     * for further convertion to other languages
17
     * @param  Inspector $inspector
18
     * @param  bool      $shouldAddTrace
19
     * @return array
20
     */
21 1
    public static function formatExceptionAsDataArray(Inspector $inspector, $shouldAddTrace)
22
    {
23 1
        $exception = $inspector->getException();
24
        $response = [
25 1
            'type'    => get_class($exception),
26 1
            'message' => $exception->getMessage(),
27 1
            'file'    => $exception->getFile(),
28 1
            'line'    => $exception->getLine(),
29 1
        ];
30
31 1
        if ($shouldAddTrace) {
32
            $frames    = $inspector->getFrames();
33
            $frameData = [];
34
35
            foreach ($frames as $frame) {
36
                /** @var Frame $frame */
37
                $frameData[] = [
38
                    'file'     => $frame->getFile(),
39
                    'line'     => $frame->getLine(),
40
                    'function' => $frame->getFunction(),
41
                    'class'    => $frame->getClass(),
42
                    'args'     => $frame->getArgs(),
43
                ];
44
            }
45
46
            $response['trace'] = $frameData;
47
        }
48
49 1
        return $response;
50
    }
51
52
    /**
53
     * @param Inspector $inspector
54
     *
55
     * @return string
56
     *
57
     * @deprecated
58
     */
59 1
    public static function formatExceptionPlain(Inspector $inspector)
60
    {
61 1
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62 1
            sprintf(
63 1
            'Plain exception formatter is deprecated and will be removed in next releases. Use %s instead',
64
                PlainTextHandler::class . '::generateResponse'
65 1
            ),
66
            E_USER_DEPRECATED
67 1
        );
68
69 1
        $message = $inspector->getException()->getMessage();
70 1
        $frames = $inspector->getFrames();
71
72 1
        $plain = $inspector->getExceptionName();
73 1
        $plain .= ' thrown with message "';
74 1
        $plain .= $message;
75 1
        $plain .= '"'."\n\n";
76
77 1
        $plain .= "Stacktrace:\n";
78 1
        foreach ($frames as $i => $frame) {
79 1
            $plain .= "#". (count($frames) - $i - 1). " ";
80 1
            $plain .= $frame->getClass() ?: '';
81 1
            $plain .= $frame->getClass() && $frame->getFunction() ? ":" : "";
82 1
            $plain .= $frame->getFunction() ?: '';
83 1
            $plain .= ' in ';
84 1
            $plain .= ($frame->getFile() ?: '<#unknown>');
85 1
            $plain .= ':';
86 1
            $plain .= (int) $frame->getLine(). "\n";
87 1
        }
88
89 1
        return $plain;
90
    }
91
}
92