Passed
Pull Request — master (#1252)
by Keal
02:21
created

XML::build()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 5
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
ccs 0
cts 20
cp 0
crap 20
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Support;
13
14
use SimpleXMLElement;
15
16
/**
17
 * Class XML.
18
 */
19
class XML
20
{
21
    /**
22
     * XML to array.
23
     *
24
     * @param string $xml XML string
25
     *
26
     * @return array
27
     */
28
    public static function parse($xml)
29
    {
30
        return self::normalize(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 EasyWeChat\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

30
        return self::normalize(/** @scrutinizer ignore-type */ simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS));
Loading history...
31
    }
32
33
    /**
34
     * XML encode.
35
     *
36
     * @param mixed  $data
37
     * @param string $root
38
     * @param string $item
39
     * @param string $attr
40
     * @param string $id
41
     *
42
     * @return string
43
     */
44
    public static function build(
45
        $data,
46
        $root = 'xml',
47
        $item = 'item',
48
        $attr = '',
49
        $id = 'id'
50
    ) {
51
        if (is_array($attr)) {
0 ignored issues
show
introduced by
The condition is_array($attr) is always false.
Loading history...
52
            $_attr = [];
53
54
            foreach ($attr as $key => $value) {
55
                $_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...
56
            }
57
58
            $attr = implode(' ', $_attr);
59
        }
60
61
        $attr = trim($attr);
62
        $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...
63
        $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...
64
        $xml .= self::data2Xml($data, $item, $id);
65
        $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...
66
67
        return $xml;
68
    }
69
70
    /**
71
     * Build CDATA.
72
     *
73
     * @param string $string
74
     *
75
     * @return string
76
     */
77
    public static function cdata($string)
78
    {
79
        return sprintf('<![CDATA[%s]]>', $string);
80
    }
81
82
    /**
83
     * Object to array.
84
     *
85
     *
86
     * @param SimpleXMLElement $obj
87
     *
88
     * @return array
89
     */
90
    protected static function normalize($obj)
91
    {
92
        $result = null;
93
94
        if (is_object($obj)) {
95
            $obj = (array) $obj;
96
        }
97
98
        if (is_array($obj)) {
99
            foreach ($obj as $key => $value) {
100
                $res = self::normalize($value);
101
                if (('@attributes' === $key) && ($key)) {
102
                    $result = $res; // @codeCoverageIgnore
103
                } else {
104
                    $result[$key] = $res;
105
                }
106
            }
107
        } else {
108
            $result = $obj;
109
        }
110
111
        return $result;
112
    }
113
114
    /**
115
     * Array to XML.
116
     *
117
     * @param array  $data
118
     * @param string $item
119
     * @param string $id
120
     *
121
     * @return string
122
     */
123
    protected static function data2Xml($data, $item = 'item', $id = 'id')
124
    {
125
        $xml = $attr = '';
126
127
        foreach ($data as $key => $val) {
128
            if (is_numeric($key)) {
129
                $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...
130
                $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...
131
            }
132
133
            $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...
134
135
            if ((is_array($val) || is_object($val))) {
136
                $xml .= self::data2Xml((array) $val, $item, $id);
137
            } else {
138
                $xml .= is_numeric($val) ? $val : self::cdata($val);
139
            }
140
141
            $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...
142
        }
143
144
        return $xml;
145
    }
146
}
147