XML::build()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 5
dl 0
loc 24
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Support;
10
11
use SimpleXMLElement;
12
13
class XML
14
{
15
    public static function parse($xml)
16
    {
17
        $backup = libxml_disable_entity_loader(true);
18
19
        $result = self::normalize(
20
            simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS)
0 ignored issues
show
Bug introduced by
It seems like simplexml_load_string($x...upport\LIBXML_NOBLANKS) can also be of type false; however, parameter $obj of Aplisin\DingTalk\Kernel\Support\XML::normalize() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
            /** @scrutinizer ignore-type */ simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS)
Loading history...
21
        );
22
23
        libxml_disable_entity_loader($backup);
24
25
        return $result;
26
    }
27
28
    /**
29
     * @param $data
30
     * @param string $root
31
     * @param string $item
32
     * @param string $attr
33
     * @param string $id
34
     * @return string
35
     */
36
    public static function build(
37
        $data,
38
        $root = 'xml',
39
        $item = 'item',
40
        $attr = '',
41
        $id = 'id'
42
    ) {
43
        if (is_array($attr)) {
0 ignored issues
show
introduced by
The condition is_array($attr) is always false.
Loading history...
44
            $_attr = [];
45
46
            foreach ($attr as $key => $value) {
47
                $_attr[] = "{$key}=\"{$value}\"";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $key instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $value instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
48
            }
49
50
            $attr = implode(' ', $_attr);
51
        }
52
53
        $attr = trim($attr);
54
        $attr = empty($attr) ? '' : " {$attr}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $attr instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
55
        $xml = "<{$root}{$attr}>";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $root instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $attr instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
56
        $xml .= self::data2Xml($data, $item, $id);
57
        $xml .= "</{$root}>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $root instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
58
59
        return $xml;
60
    }
61
62
    /**
63
     * @param $string
64
     * @return string
65
     */
66
    public static function cdata($string)
67
    {
68
        return sprintf('<![CDATA[%s]]>', $string);
69
    }
70
71
    /**
72
     * Object to array.
73
     *
74
     *
75
     * @param SimpleXMLElement $obj
76
     *
77
     * @return array
78
     */
79
    protected static function normalize($obj)
80
    {
81
        $result = null;
82
83
        if (is_object($obj)) {
84
            $obj = (array) $obj;
85
        }
86
87
        if (is_array($obj)) {
88
            foreach ($obj as $key => $value) {
89
                $res = self::normalize($value);
90
                if (('@attributes' === $key) && ($key)) {
91
                    $result = $res; // @codeCoverageIgnore
92
                } else {
93
                    $result[$key] = $res;
94
                }
95
            }
96
        } else {
97
            $result = $obj;
98
        }
99
100
        return $result;
101
    }
102
103
    protected static function data2Xml($data, $item = 'item', $id = 'id')
104
    {
105
        $xml = $attr = '';
106
107
        foreach ($data as $key => $val) {
108
            if (is_numeric($key)) {
109
                $id && $attr = " {$id}=\"{$key}\"";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $key instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
110
                $key = $item;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
111
            }
112
113
            $xml .= "<{$key}{$attr}>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $key instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $attr instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
114
115
            if ((is_array($val) || is_object($val))) {
116
                $xml .= self::data2Xml((array) $val, $item, $id);
117
            } else {
118
                $xml .= is_numeric($val) ? $val : self::cdata($val);
119
            }
120
121
            $xml .= "</{$key}>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $key instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
122
        }
123
124
        return $xml;
125
    }
126
}
127