Test Setup Failed
Push — test ( 528f91...abcbcc )
by Jonathan
03:20
created

SimpleXMLElementPlugin::parse()   F

Complexity

Conditions 16
Paths 265

Size

Total Lines 100
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 53
nc 265
nop 3
dl 0
loc 100
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
namespace Kint\Parser;
4
5
use Kint\Object\BasicObject;
6
use Kint\Object\BlobObject;
7
use Kint\Object\Representation\Representation;
8
use SimpleXMLElement;
9
10
class SimpleXMLElementPlugin extends Plugin
11
{
12
    /**
13
     * Show all properties and methods.
14
     *
15
     * @var bool
16
     */
17
    public static $verbose = false;
18
19
    public function getTypes()
20
    {
21
        return array('object');
22
    }
23
24
    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...
25
    {
26
        return Parser::TRIGGER_SUCCESS;
27
    }
28
29
    public function parse(&$var, BasicObject &$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...
30
    {
31
        if (!$var instanceof SimpleXMLElement) {
32
            return;
33
        }
34
35
        $o->hints[] = 'simplexml_element';
36
37
        if (!self::$verbose) {
38
            $o->removeRepresentation('properties');
39
            $o->removeRepresentation('iterator');
40
            $o->removeRepresentation('methods');
41
        }
42
43
        // Attributes
44
        $a = new Representation('Attributes');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. 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...
45
46
        $base_obj = new BasicObject();
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
47
        $base_obj->depth = $o->depth;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
48
49
        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...
50
            $base_obj->access_path = '(string) '.$o->access_path;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
51
        }
52
53
        if ($var->attributes()) {
54
            $attribs = iterator_to_array($var->attributes());
55
            $attribs = array_map('strval', $attribs);
56
        } else {
57
            $attribs = array();
58
        }
59
60
        // XML attributes are by definition strings and don't have children,
61
        // so up the depth limit in case we're just below the limit since
62
        // there won't be any recursive stuff anyway.
63
        $a->contents = $this->parser->parseDeep($attribs, $base_obj)->value->contents;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
64
65
        $o->addRepresentation($a, 0);
66
67
        // Children
68
        // We need to check children() separately from the values we already parsed because
69
        // text contents won't show up in children() but they will show up in properties.
70
        //
71
        // Why do we still need to check for attributes if we already have an attributes()
72
        // method? Hell if I know!
73
        $children = $var->children();
74
75
        if ($o->value) {
76
            $c = new Representation('Children');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. 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...
77
78
            foreach ($o->value->contents as $value) {
79
                if ($value->name === '@attributes') {
80
                    continue;
81
                } elseif (isset($children->{$value->name})) {
82
                    $i = 0;
83
84
                    while (isset($children->{$value->name}[$i])) {
85
                        $base_obj = new BasicObject();
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
86
                        $base_obj->depth = $o->depth + 1;
0 ignored issues
show
Coding Style introduced by
$base_obj 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
                        $base_obj->name = $value->name;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
88
                        if ($value->access_path) {
89
                            $base_obj->access_path = $value->access_path.'['.$i.']';
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
90
                        }
91
92
                        $value = $this->parser->parse($children->{$value->name}[$i], $base_obj);
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
93
94
                        if ($value->access_path && $value->type === 'string') {
95
                            $value->access_path = '(string) '.$value->access_path;
96
                        }
97
98
                        $c->contents[] = $value;
99
100
                        ++$i;
101
                    }
102
                }
103
            }
104
105
            $o->size = count($c->contents);
106
107
            if (!$o->size) {
108
                $o->size = null;
109
110
                if (strlen((string) $var)) {
111
                    $base_obj = new BlobObject();
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
112
                    $base_obj->depth = $o->depth + 1;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
113
                    $base_obj->name = $o->name;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
114
                    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...
115
                        $base_obj->access_path = '(string) '.$o->access_path;
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
116
                    }
117
118
                    $value = (string) $var;
119
120
                    $c = new Representation('Contents');
121
                    $c->implicit_label = true;
122
                    $c->contents = array($this->parser->parseDeep($value, $base_obj));
0 ignored issues
show
Coding Style introduced by
$base_obj 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...
123
                }
124
            }
125
126
            $o->addRepresentation($c, 0);
127
        }
128
    }
129
}
130