Kint_Parser_Trace::isTrace()   D
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 8
nop 1
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
class Kint_Parser_Trace extends Kint_Parser_Plugin
0 ignored issues
show
Coding Style introduced by
Kint_Parser_Trace does not seem to conform to the naming convention (^[A-Z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
4
{
5
    public static $blacklist = array('spl_autoload_call');
6
7
    public function getTypes()
8
    {
9
        return array('array');
10
    }
11
12
    public function getTriggers()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
13
    {
14
        return Kint_Parser::TRIGGER_SUCCESS;
15
    }
16
17
    public function parse(&$var, Kint_Object &$o, $trigger)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
18
    {
19
        if (!$o->value) {
20
            return;
21
        }
22
23
        $trace = $this->parser->getCleanArray($var);
24
25
        if (count($trace) !== count($o->value->contents) || !self::isTrace($trace)) {
26
            return;
27
        }
28
29
        $o = $o->transplant(new Kint_Object_Trace());
30
        $rep = $o->value;
31
32
        $old_trace = $rep->contents;
0 ignored issues
show
Coding Style introduced by
$old_trace does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
33
34
        self::normalizeAliases(self::$blacklist);
35
36
        $rep->contents = array();
37
38
        foreach ($old_trace as $frame) {
0 ignored issues
show
Coding Style introduced by
$old_trace does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
39
            $index = $frame->name;
40
41
            if (!isset($trace[$index]['function'])) {
42
                // Something's very very wrong here, but it's probably a plugin's fault
43
                continue;
44
            }
45
46
            if (self::frameIsListed($trace[$index], self::$blacklist)) {
47
                continue;
48
            }
49
50
            $rep->contents[$index] = $frame->transplant(new Kint_Object_TraceFrame());
51
            $rep->contents[$index]->assignFrame($trace[$index]);
52
        }
53
54
        ksort($rep->contents);
55
        $rep->contents = array_values($rep->contents);
56
57
        $o->clearRepresentations();
58
        $o->addRepresentation($rep);
59
        $o->size = count($rep->contents);
60
    }
61
62
    public static function isTrace(array $trace)
63
    {
64
        if (!Kint_Object::isSequential($trace)) {
65
            return false;
66
        }
67
68
        static $bt_structure = array(
69
            'function' => 'string',
70
            'line' => 'integer',
71
            'file' => 'string',
72
            'class' => 'string',
73
            'object' => 'object',
74
            'type' => 'string',
75
            'args' => 'array',
76
        );
77
78
        $file_found = false;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
79
80
        foreach ($trace as $frame) {
81
            if (!is_array($frame) || !isset($frame['function'])) {
82
                return false;
83
            }
84
85
            foreach ($frame as $key => $val) {
86
                if (!isset($bt_structure[$key])) {
0 ignored issues
show
Coding Style introduced by
$bt_structure does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
87
                    return false;
88
                } elseif (gettype($val) !== $bt_structure[$key]) {
0 ignored issues
show
Coding Style introduced by
$bt_structure does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
89
                    return false;
90
                } elseif ($key === 'file') {
91
                    $file_found = true;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
92
                }
93
            }
94
        }
95
96
        return $file_found;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
97
    }
98
99
    public static function frameIsListed(array $frame, array $matches)
0 ignored issues
show
Coding Style introduced by
function frameIsListed() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
100
    {
101
        if (isset($frame['class'])) {
102
            $called = array(strtolower($frame['class']), strtolower($frame['function']));
103
        } else {
104
            $called = strtolower($frame['function']);
105
        }
106
107
        return in_array($called, $matches, true);
108
    }
109
110
    public static function normalizeAliases(array &$aliases)
111
    {
112
        foreach ($aliases as $index => &$alias) {
113
            if (is_array($alias) && count($alias) === 2) {
114
                $alias = array_values(array_filter($alias, 'is_string'));
115
116
                if (count($alias) === 2 &&
117
                    preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $alias[1]) &&
118
                    preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*$/', $alias[0])
119
                ) {
120
                    $alias = array(
121
                        strtolower(ltrim($alias[0], '\\')),
122
                        strtolower($alias[1]),
123
                    );
124
                } else {
125
                    unset($aliases[$index]);
126
                    continue;
127
                }
128
            } elseif (is_string($alias)) {
129
                if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $alias)) {
130
                    $alias = strtolower($alias);
131
                } else {
132
                    unset($aliases[$index]);
133
                    continue;
134
                }
135
            } else {
136
                unset($aliases[$index]);
137
            }
138
        }
139
140
        $aliases = array_values($aliases);
141
    }
142
}
143