Completed
Pull Request — master (#519)
by Julián
01:57
created

Formatter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 72.09%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 77
ccs 31
cts 43
cp 0.7209
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B formatExceptionAsDataArray() 0 30 3
C formatExceptionPlain() 0 29 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
class Formatter
11
{
12
    /**
13
     * Returns all basic information about the exception in a simple array
14
     * for further convertion to other languages
15
     * @param  Inspector $inspector
16
     * @param  bool      $shouldAddTrace
17
     * @return array
18
     */
19 1
    public static function formatExceptionAsDataArray(Inspector $inspector, $shouldAddTrace)
20
    {
21 1
        $exception = $inspector->getException();
22
        $response = [
23 1
            'type'    => get_class($exception),
24 1
            'message' => $exception->getMessage(),
25 1
            'file'    => $exception->getFile(),
26 1
            'line'    => $exception->getLine(),
27 1
        ];
28
29 1
        if ($shouldAddTrace) {
30
            $frames    = $inspector->getFrames();
31
            $frameData = [];
32
33
            foreach ($frames as $frame) {
34
                /** @var Frame $frame */
35
                $frameData[] = [
36
                    'file'     => $frame->getFile(),
37
                    'line'     => $frame->getLine(),
38
                    'function' => $frame->getFunction(),
39
                    'class'    => $frame->getClass(),
40
                    'args'     => $frame->getArgs(),
41
                ];
42
            }
43
44
            $response['trace'] = $frameData;
45
        }
46
47 1
        return $response;
48
    }
49
50
    /**
51
     * @param Inspector $inspector
52
     *
53
     * @return string
54
     *
55
     * @deprecated
56
     */
57 1
    public static function formatExceptionPlain(Inspector $inspector)
58
    {
59 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...
60 1
            'Using plain exception formatter is deprecated and will be removed in next releases',
61
            E_USER_DEPRECATED
62 1
        );
63
64 1
        $message = $inspector->getException()->getMessage();
65 1
        $frames = $inspector->getFrames();
66
67 1
        $plain = $inspector->getExceptionName();
68 1
        $plain .= ' thrown with message "';
69 1
        $plain .= $message;
70 1
        $plain .= '"'."\n\n";
71
72 1
        $plain .= "Stacktrace:\n";
73 1
        foreach ($frames as $i => $frame) {
74 1
            $plain .= "#". (count($frames) - $i - 1). " ";
75 1
            $plain .= $frame->getClass() ?: '';
76 1
            $plain .= $frame->getClass() && $frame->getFunction() ? ":" : "";
77 1
            $plain .= $frame->getFunction() ?: '';
78 1
            $plain .= ' in ';
79 1
            $plain .= ($frame->getFile() ?: '<#unknown>');
80 1
            $plain .= ':';
81 1
            $plain .= (int) $frame->getLine(). "\n";
82 1
        }
83
84 1
        return $plain;
85
    }
86
}
87