Throwable::getThrowableData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Dazzle\Throwable;
4
5
abstract class Throwable
6
{
7
    /**
8
     * Parse Throwable message to proper format.
9
     *
10
     * @param string[] $ex
11
     * @return string
12
     */
13 11
    public static function parseThrowableMessage($ex)
14
    {
15 11
        $message = $ex['message'];
16
17 11
        if ($ex['isError'] && strpos($message, ' in ') !== false)
18
        {
19
            $message = preg_replace('#([a-zA-Z0-9-_]+?)/#siU', '', $message);
20
            $message = preg_replace('#/#si', '', $message, 1);
21
        }
22
        else
23
        {
24 11
            $message = trim($message, '"');
25 11
            $file = str_replace('.php', '', basename($ex['file']));
26 11
            $line = $ex['line'];
27 11
            $message = '"' . $message . '" in ' . $file . ':' . $line;
28
        }
29
30 11
        return '[' . static::getBasename($ex['class']) . '] ' . $message;
31
    }
32
33
    /**
34
     * Return throwable stack in recursive array format.
35
     *
36
     * @param \Error|\Exception $ex
37
     * @param string[] &$data
38
     * @param int $offset
39
     * @return mixed
40
     */
41 19
    public static function getThrowableStack($ex, &$data = [], $offset = 0)
42
    {
43 19
        $data = static::getThrowableData($ex, $offset);
44
45 19
        if (($current = $ex->getPrevious()) !== null)
46
        {
47 19
            static::getThrowableStack($current, $data['prev'], count(static::getTraceElements($ex)));
0 ignored issues
show
Documentation introduced by
$data['prev'] is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        }
49
50 19
        return $data;
51
    }
52
53
    /**
54
     * Return throwable data in array format.
55
     *
56
     * @param \Error|\Exception $ex
57
     * @param int $offset
58
     * @return string[]
59
     */
60 21
    public static function getThrowableData($ex, $offset = 0)
61
    {
62
        return [
63 21
            'message'   => $ex->getMessage(),
64 21
            'class'     => get_class($ex),
65 21
            'file'      => $ex->getFile(),
66 21
            'line'      => $ex->getLine(),
67 21
            'code'      => $ex->getCode(),
68 21
            'trace'     => static::getTraceElements($ex, $offset),
69 21
            'isError'   => $ex instanceof \Error,
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
            'prev'      => null
71
        ];
72
    }
73
74
    /**
75
     * @param \Error|\Exception $ex
76
     * @param int $offset
77
     * @return string[]
78
     */
79 23
    protected static function getTraceElements($ex, $offset = 0)
80
    {
81 23
        $trace = $ex->getTrace();
82 23
        $file  = str_replace('.php', '', basename($ex->getFile()));
83
        $elements = [
84 23
            '[throwable] ' . get_class($ex) . '(...) in ' . $file .':' . $ex->getLine()
85
        ];
86
87 23
        foreach ($trace as $currentTrack)
88
        {
89 23
            $elements[] = static::parseTraceElement($currentTrack);
90
        }
91 23
        $elements[] = '[main]';
92
93 23
        array_splice($elements, -$offset+1, $offset);
94
95 23
        return $elements;
96
    }
97
98
    /**
99
     * @param mixed[] $element
100
     * @return string
101
     */
102 25
    protected static function parseTraceElement($element)
103
    {
104 25
        $element['class']    = isset($element['class'])    ? $element['class']    : 'Undefined';
105 25
        $element['file']     = isset($element['file'])     ? $element['file']     : 'unknown';
106 25
        $element['line']     = isset($element['line'])     ? $element['line']     : 0;
107 25
        $element['type']     = isset($element['type'])     ? $element['type']     : '';
108 25
        $element['function'] = isset($element['function']) ? $element['function'] : '::undefined';
109 25
        $element['args']     = isset($element['args'])     ? $element['args']     : [];
110
111 25
        return implode('', [
112 25
            '[call] ',
113 25
            $element['class'],
114 25
            $element['type'],
115 25
            $element['function'],
116 25
            '(',
117 25
            static::parseArgs($element['args']),
118 25
            ') in ',
119 25
            str_replace('.php', '', basename($element['file'])),
120 25
            ':',
121 25
            $element['line']
122
        ]);
123
    }
124
125
    /**
126
     * @param mixed[] $args
127
     * @return string
128
     */
129 26
    protected static function parseArgs($args)
130
    {
131 26
        $elements = [];
132
133 26
        foreach ($args as $element)
134
        {
135 25
            if (is_array($element))
136
            {
137 25
                $element = 'Array';
138
            }
139 25
            else if (is_object($element))
140
            {
141 24
                $element = get_class($element);
142
            }
143 25
            else if (is_string($element))
144
            {
145 24
                $element = (strlen($element) > 32) ? substr($element, 0, 32) . '...' : $element;
146 24
                $element = '"' . $element . '"';
147
            }
148
149 25
            $elements[] = $element;
150
        }
151
152 26
        return implode(', ', $elements);
153
    }
154
155
    /**
156
     * @param string $class
157
     * @return string
158
     */
159 13
    protected static function getBasename($class)
160
    {
161 13
        $tmp = explode('\\', $class);
162 13
        $className = end($tmp);
163
164 13
        return $className;
165
    }
166
}
167