Completed
Pull Request — develop (#120)
by Matthew
02:45
created

Xml::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 6
        $data = simplexml_load_file($filename, null, LIBXML_NOERROR);
29 6
        $parsed = $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...
30
31 3
        return $parsed === null ? [] : $parsed;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     * Parses an XML string as an array
37
     *
38
     * @throws ParseException If there is an error parsing the XML string
39
     */
40 3
    public function parseString($config)
41
    {
42 3
        libxml_use_internal_errors(true);
43 3
        $data = simplexml_load_string($config, null, LIBXML_NOERROR);
44 3
        $parsed = $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...
45
46 3
        return $parsed === null ? [] : $parsed;
47
    }
48
49
    /**
50
     * Completes parsing of XML data
51
     *
52
     * @param  array $data
53
     * @param  string $filename
54
     *
55
     * @return array|null
56
     * @throws ParseException If there is an error parsing the XML data
57
     */
58 6
    protected function parse($data = null, $filename = null)
59
    {
60 6
        if ($data === false) {
61 3
            $errors      = libxml_get_errors();
62 3
            $latestError = array_pop($errors);
63
            $error       = [
64 3
                'message' => $latestError->message,
65 3
                'type'    => $latestError->level,
66 3
                'code'    => $latestError->code,
67 3
                'file'    => $filename,
68 3
                'line'    => $latestError->line,
69 2
            ];
70 3
            throw new ParseException($error);
71
        }
72
73 3
        $data = json_decode(json_encode($data), true);
74
75 3
        return $data;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 3
    public static function getSupportedExtensions()
82
    {
83 3
        return ['xml'];
84
    }
85
}
86