Completed
Push — master ( 8d39d6...58e1d9 )
by Xeriab
02:50
created

Xml::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 15
cts 16
cp 0.9375
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 3.0021
1
<?php
2
3
/**
4
 * Konfig
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
9
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
10
 * @link    https://xeriab.github.io/projects/konfig
11
 */
12
13
namespace Exen\Konfig\FileParser;
14
15
use Exen\Konfig\Utils;
16
use Exen\Konfig\Exception\Exception;
17
use Exen\Konfig\Exception\ParseException;
18
19
class Xml extends AbstractFileParser
20
{
21
    /**
22
     * {@inheritDoc}
23
     * Loads a XML file as an array
24
     *
25
     * @throws ParseException If there is an error parsing XML file
26 6
     * @since 0.1.0
27
     */
28 6
    public function parse($path)
29 4
    {
30 6
        $data = $this->loadFile($path);
31 6
32 4
        if ($data === false) {
33
            $lastError = libxml_get_last_error();
34 6
35 3
            if ($lastError !== false) {
36
                throw new ParseException([
37 3
                    'message' => $lastError->message,
38 3
                    'type' => $lastError->level,
39 3
                    'code' => $lastError->code,
40 3
                    'file' => $lastError->file,
41 3
                    'line' => $lastError->line,
42 3
                ]);
43 3
            }
44 2
        }
45
46
        return json_decode(json_encode($data), true);
47
    }
48 3
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function getSupportedFileExtensions()
53
    {
54 3
        return ['xml'];
55
    }
56 3
57
    /**
58
     * Loads in the given file and parses it.
59
     *
60
     * @param   string  $file File to load
61
     * @return  array
62
     * @since 0.2.4
63
     * @codeCoverageIgnore
64
     */
65
    protected function loadFile($file = null)
66
    {
67
        $this->file = $file;
68
        $contents = $this->parseVars(Utils::getContent($file));
69
        return simplexml_load_string(
70
            $contents,
71
            'SimpleXMLElement',
72
            LIBXML_NOWARNING | LIBXML_NOERROR
73
        );
74
    }
75
76
    /**
77
     * Returns the formatted configuration file contents.
78
     *
79
     * @param   array   $content  configuration array
0 ignored issues
show
Documentation introduced by
There is no parameter named $content. Did you maybe mean $contents?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
     * @return  string  formatted configuration file contents
81
     * @since 0.2.4
82
     * @codeCoverageIgnore
83
     */
84
    protected function exportFormat($contents = null)
85
    {
86
        throw new \Exception('Saving configuration to `XML` is not supported at this time');
87
    }
88
}
89
90
// END OF ./src/FileParser/Xml.php FILE
91