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

Formatter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 80
ccs 33
cts 45
cp 0.7332
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B formatExceptionAsDataArray() 0 30 3
C formatExceptionPlain() 0 32 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