Completed
Pull Request — master (#546)
by Richard
09:47
created

Kint_Parser_SimpleXMLElement::parse()   F

Complexity

Conditions 16
Paths 265

Size

Total Lines 110
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 61
nc 265
nop 3
dl 0
loc 110
rs 3.7109
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class Kint_Parser_SimpleXMLElement extends Kint_Parser_Plugin
4
{
5
    /**
6
     * Show all properties and methods.
7
     *
8
     * @var bool
9
     */
10
    public static $verbose = false;
11
12
    public function getTypes()
13
    {
14
        return array('object');
15
    }
16
17
    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...
18
    {
19
        return Kint_Parser::TRIGGER_SUCCESS;
20
    }
21
22
    public function parse(&$var, Kint_Object &$o, $trigger)
23
    {
24
        if (!$var instanceof SimpleXMLElement) {
25
            return;
26
        }
27
28
        $o->hints[] = 'simplexml_element';
29
30
        if (!self::$verbose) {
31
            $o->removeRepresentation('properties');
32
            $o->removeRepresentation('iterator');
33
            $o->removeRepresentation('methods');
34
        }
35
36
        // Attributes
37
        $a = new Kint_Object_Representation('Attributes');
38
39
        $base_obj = new Kint_Object();
40
        $base_obj->depth = $o->depth;
41
42
        if ($o->access_path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $o->access_path of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
43
            $base_obj->access_path = '(string) '.$o->access_path;
44
        }
45
46
        if ($var->attributes()) {
47
            $attribs = iterator_to_array($var->attributes());
48
            $attribs = array_map('strval', $attribs);
49
        } else {
50
            $attribs = array();
51
        }
52
53
        // XML attributes are by definition strings and don't have children,
54
        // so up the depth limit in case we're just below the limit since
55
        // there won't be any recursive stuff anyway.
56
        $depth_stash = $this->parser->max_depth;
57
        $this->parser->max_depth = 0;
58
        $a->contents = $this->parser->parse($attribs, $base_obj);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parser->parse($attribs, $base_obj) of type object<Kint_Object> is incompatible with the declared type array of property $contents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->parser->max_depth = $depth_stash;
60
61
        $a->contents = $a->contents->value->contents;
62
63
        $o->addRepresentation($a, 0);
64
65
        // Children
66
        // We need to check children() separately from the values we already parsed because
67
        // text contents won't show up in children() but they will show up in properties.
68
        //
69
        // Why do we still need to check for attributes if we already have an attributes()
70
        // method? Hell if I know!
71
        $children = $var->children();
72
73
        if ($o->value) {
74
            $c = new Kint_Object_Representation('Children');
75
76
            foreach ($o->value->contents as $value) {
77
                if ($value->name === '@attributes') {
78
                    continue;
79
                } elseif (isset($children->{$value->name})) {
80
                    $i = 0;
81
82
                    while (isset($children->{$value->name}[$i])) {
83
                        $base_obj = new Kint_Object();
84
                        $base_obj->depth = $o->depth + 1;
85
                        $base_obj->name = $value->name;
86
                        if ($value->access_path) {
87
                            $base_obj->access_path = $value->access_path.'['.$i.']';
88
                        }
89
90
                        $value = $this->parser->parse($children->{$value->name}[$i], $base_obj);
91
92
                        if ($value->access_path && $value->type === 'string') {
93
                            $value->access_path = '(string) '.$value->access_path;
94
                        }
95
96
                        $c->contents[] = $value;
97
98
                        ++$i;
99
                    }
100
                }
101
            }
102
103
            $o->size = count($c->contents);
104
105
            if (!$o->size) {
106
                $o->size = null;
107
108
                if (strlen((string) $var)) {
109
                    $base_obj = new Kint_Object_Blob();
110
                    $base_obj->depth = $o->depth + 1;
111
                    $base_obj->name = $o->name;
112
                    if ($o->access_path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $o->access_path of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113
                        $base_obj->access_path = '(string) '.$o->access_path;
114
                    }
115
116
                    $value = (string) $var;
117
118
                    $depth_stash = $this->parser->max_depth;
119
                    $this->parser->max_depth = 0;
120
                    $value = $this->parser->parse($value, $base_obj);
121
                    $this->parser->max_depth = $depth_stash;
122
123
                    $c = new Kint_Object_Representation('Contents');
124
                    $c->implicit_label = true;
125
                    $c->contents = array($value);
126
                }
127
            }
128
129
            $o->addRepresentation($c, 0);
130
        }
131
    }
132
}
133