Kint_Parser_Xml::xmlToDOMDocument()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 5
Ratio 23.81 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 5
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
class Kint_Parser_Xml extends Kint_Parser_Plugin
0 ignored issues
show
Coding Style introduced by
Kint_Parser_Xml 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...
Coding Style introduced by
The property $parse_method is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
4
{
5
    /**
6
     * Which method to parse the variable with.
7
     *
8
     * DOMDocument provides more information including the text between nodes,
9
     * however it's memory usage is very high and it takes longer to parse and
10
     * render. Plus it's a pain to work with. So SimpleXML is the default.
11
     *
12
     * @var string
13
     */
14
    public static $parse_method = 'SimpleXML';
0 ignored issues
show
Coding Style introduced by
$parse_method 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...
15
16
    public function getTypes()
17
    {
18
        return array('string');
19
    }
20
21
    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...
22
    {
23
        return Kint_Parser::TRIGGER_SUCCESS;
24
    }
25
26
    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...
27
    {
28
        if (substr($var, 0, 5) !== '<?xml') {
29
            return;
30
        }
31
32
        if (!method_exists(get_class($this), 'xmlTo'.self::$parse_method)) {
33
            return;
34
        }
35
36
        $xml = call_user_func(array(get_class($this), 'xmlTo'.self::$parse_method), $var, $o->access_path);
37
38
        if (empty($xml)) {
39
            return;
40
        }
41
42
        list($xml, $access_path, $name) = $xml;
0 ignored issues
show
Coding Style introduced by
$access_path 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...
43
44
        $base_obj = new Kint_Object();
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...
45
        $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...
46
        $base_obj->name = $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...
47
        $base_obj->access_path = $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...
48
49
        $r = new Kint_Object_Representation('XML');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. 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...
50
        $r->contents = $this->parser->parse($xml, $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...
Documentation Bug introduced by
It seems like $this->parser->parse($xml, $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...
51
52
        $o->addRepresentation($r, 0);
53
    }
54
55
    protected static function xmlToSimpleXML($var, $parent_path)
0 ignored issues
show
Coding Style introduced by
$parent_path 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...
Coding Style Naming introduced by
The parameter $parent_path is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
56
    {
57
        try {
58
            $errors = libxml_use_internal_errors(true);
59
            $xml = simplexml_load_string($var);
60
            libxml_use_internal_errors($errors);
61
        } catch (Exception $e) {
62
            if (isset($errors)) {
63
                libxml_use_internal_errors($errors);
64
            }
65
66
            return;
67
        }
68
69
        if (!$xml) {
70
            return;
71
        }
72
73 View Code Duplication
        if ($parent_path === null) {
0 ignored issues
show
Coding Style introduced by
$parent_path 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...
Duplication introduced by
This code seems to be duplicated across 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...
74
            $access_path = null;
0 ignored issues
show
Coding Style introduced by
$access_path 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...
75
        } else {
76
            $access_path = 'simplexml_load_string('.$parent_path.')';
0 ignored issues
show
Coding Style introduced by
$access_path 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...
77
        }
78
79
        $name = $xml->getName();
80
81
        return array($xml, $access_path, $name);
0 ignored issues
show
Coding Style introduced by
$access_path 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...
82
    }
83
84
    /**
85
     * Get the DOMDocument info.
86
     *
87
     * The documentation of DOMDocument::loadXML() states that while you can
88
     * call it statically, it will give an E_STRICT warning. On my system it
89
     * actually gives an E_DEPRECATED warning, but it works so we'll just add
90
     * an error-silencing '@' to the access path.
91
     *
92
     * If it errors loading then we wouldn't have gotten this far in the first place.
93
     *
94
     * @param string $var         The XML string
95
     * @param string $parent_path The path to the parent, in this case the XML string
96
     *
97
     * @return array The root element DOMNode, the access path, and the root element name
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    protected static function xmlToDOMDocument($var, $parent_path)
0 ignored issues
show
Coding Style introduced by
$parent_path 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...
Coding Style Naming introduced by
The parameter $parent_path is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
100
    {
101
        // There's no way to check validity in DOMDocument without making errors. For shame!
102
        if (!self::xmlToSimpleXML($var, $parent_path)) {
0 ignored issues
show
Coding Style introduced by
$parent_path 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...
103
            return;
104
        }
105
106
        $xml = new DOMDocument();
107
        $xml->loadXML($var);
108
        $xml = $xml->firstChild;
109
110 View Code Duplication
        if ($parent_path === null) {
0 ignored issues
show
Coding Style introduced by
$parent_path 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...
Duplication introduced by
This code seems to be duplicated across 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...
111
            $access_path = null;
0 ignored issues
show
Coding Style introduced by
$access_path 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
        } else {
113
            $access_path = '@DOMDocument::loadXML('.$parent_path.')->firstChild';
0 ignored issues
show
Coding Style introduced by
$access_path 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
        }
115
116
        $name = $xml->nodeName;
117
118
        return array($xml, $access_path, $name);
0 ignored issues
show
Coding Style introduced by
$access_path 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...
119
    }
120
}
121