Completed
Push — master ( 65b14f...519e8d )
by Kamil
02:29
created

Exception   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 146
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 146
loc 146
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A __toString() 4 4 1
A toString() 9 9 1
A toTrace() 4 4 1
A toStackTrace() 10 10 2
A toThrowableTrace() 10 10 2
A toStackString() 15 15 3
A toThrowableString() 15 15 3
A setContext() 4 4 1
A getContext() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dazzle\Throwable;
4
5 View Code Duplication
class Exception extends \Exception
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    /**
8
     * @var mixed[]
9
     */
10
    protected $context = [];
11
12
    /**
13
     * @param string $message
14
     * @param int $code
15
     * @param \Error|\Exception|null $previous
16
     */
17 20
    public function __construct($message = 'Unknown exception', $code = 0, $previous = null)
18
    {
19 20
        parent::__construct($message, $code, $previous);
20 20
    }
21
22
    /**
23
     * @param mixed[] $context
24
     */
25 2
    public function setContext(array $context = [])
26
    {
27 2
        $this->context = $context;
28 2
    }
29
30
    /**
31
     * @return mixed[]
32
     */
33 1
    public function getContext()
34
    {
35 1
        return $this->context;
36
    }
37
38
    /**
39
     * @return string
40
     */
41 2
    public function __toString()
42
    {
43 2
        return static::toString($this);
44
    }
45
46
    /**
47
     * Return Exception full trace in string format.
48
     *
49
     * @param \Error|\Exception $ex
50
     * @return string
51
     */
52 3
    public static function toString($ex)
53
    {
54 3
        return implode("\n", [
55 3
            "\t" . 'Throwable trace:',
56 3
            static::toThrowableString($ex),
57
            "\t" . 'Stack trace:',
58 3
            static::toStackString($ex)
59
        ]);
60
    }
61
62
    /**
63
     * Return Exception full trace in array format.
64
     *
65
     * @param \Error|\Exception $ex
66
     * @return mixed
67
     */
68 1
    public static function toTrace($ex)
69
    {
70 1
        return Throwable::getThrowableStack($ex);
71
    }
72
73
    /**
74
     * Return Exception stack trace in array format.
75
     *
76
     * @param \Error|\Exception $ex
77
     * @return string[]
78
     */
79 5
    public static function toStackTrace($ex)
80
    {
81 5
        $list = [];
82 5
        for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev'])
83
        {
84 5
            $list = array_merge($stack['trace'], $list);
85
        }
86
87 5
        return $list;
88
    }
89
90
    /**
91
     * Return Exception throwable trace in array format.
92
     *
93
     * @param \Error|\Exception $ex
94
     * @return string[]
95
     */
96 5
    public static function toThrowableTrace($ex)
97
    {
98 5
        $list = [];
99 5
        for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev'])
100
        {
101 5
            $list[] = Throwable::parseThrowableMessage($stack);
102
        }
103
104 5
        return array_reverse($list);
105
    }
106
107
    /**
108
     * Return Exception stack trace in string format.
109
     *
110
     * @param \Error|\Exception $ex
111
     * @return string
112
     */
113 4
    public static function toStackString($ex)
114
    {
115 4
        $stack = [];
116 4
        $i = 0;
117 4
        $trace = static::toStackTrace($ex);
118 4
        $pad = strlen(count($trace)) > 2 ?: 2;
119
120 4
        foreach ($trace as $element)
121
        {
122 4
            $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element;
123 4
            ++$i;
124
        }
125
126 4
        return implode("\n", $stack);
127
    }
128
129
    /**
130
     * Return Exception throwable trace in string format.
131
     *
132
     * @param \Error|\Exception $ex
133
     * @return string
134
     */
135 4
    public static function toThrowableString($ex)
136
    {
137 4
        $stack = [];
138 4
        $i = 0;
139 4
        $trace = static::toThrowableTrace($ex);
140 4
        $pad = strlen(count($trace)) > 2 ?: 2;
141
142 4
        foreach ($trace as $element)
143
        {
144 4
            $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element;
145 4
            ++$i;
146
        }
147
148 4
        return implode("\n", $stack);
149
    }
150
}
151