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

Formatter::formatExceptionPlain()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 24
cts 24
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 33
nop 1
crap 7
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