Completed
Push — master ( 56f22b...5c0ad8 )
by Davide
01:07
created

Xml::parseString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * XML parser
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @author     Filip Š <[email protected]>
14
 * @link       https://github.com/noodlehaus/config
15
 * @license    MIT
16
 */
17
class Xml implements ParserInterface
18
{
19
    /**
20
     * {@inheritDoc}
21
     * Parses an XML file as an array
22
     *
23
     * @throws ParseException If there is an error parsing the XML file
24
     */
25 6
    public function parseFile($filename)
26
    {
27 6
        libxml_use_internal_errors(true);
28
        $data = simplexml_load_file($filename, null, LIBXML_NOERROR);
29 6
        return $this->parse($data, $filename);
0 ignored issues
show
Documentation introduced by
$data is of type object<SimpleXMLElement>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$filename is of type string, but the function expects a object<Noodlehaus\Parser\strring>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
    }
31 6
32 3
    /**
33 3
     * {@inheritDoc}
34
     * Parses an XML string as an array
35 3
     *
36 3
     * @throws ParseException If there is an error parsing the XML string
37 3
     */
38 3
    public function parseString($config)
39 3
    {
40 1
        libxml_use_internal_errors(true);
41 3
        $data = simplexml_load_string($config, null, LIBXML_NOERROR);
42
        return $this->parse($data);
0 ignored issues
show
Documentation introduced by
$data is of type object<SimpleXMLElement>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44 3
45
    /**
46 3
     * Completes parsing of XML data
47
     *
48
     * @param  array   $data
49
     * @param  strring $filename
50
     *
51
     * @throws ParseException If there is an error parsing the XML data
52 3
     */
53
    protected function parse($data = null, $filename = null)
54 3
    {
55
        if ($data === false) {
56
            $errors      = libxml_get_errors();
57
            $latestError = array_pop($errors);
58
            $error       = [
59
                'message' => $latestError->message,
60
                'type'    => $latestError->level,
61
                'code'    => $latestError->code,
62
                'file'    => $filename,
63
                'line'    => $latestError->line,
64
            ];
65
            throw new ParseException($error);
66
        }
67
68
        $data = json_decode(json_encode($data), true);
69
70
        return $data;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public static function getSupportedExtensions()
77
    {
78
        return ['xml'];
79
    }
80
}
81