Completed
Push — master ( 33953d...4a13d7 )
by Xeriab
03:37
created

Xml   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 99
ccs 15
cts 16
cp 0.9375
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 22 3
A getSupportedFileExtensions() 0 4 1
A loadFile() 0 11 1
A exportFormat() 0 6 1
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exception;
20
use Exen\Konfig\Exception\ParseException;
21
use Exen\Konfig\Utils;
22
23
/**
24
 * Xml
25
 * Konfig's XML parser class.
26
 *
27
 * @category FileParser
28
 * @package  Konfig
29
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
30
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
31
 * @link     https://xeriab.github.io/projects/konfig
32
 *
33
 * @implements Exen\Konfig\FileParser\AbstractFileParser
34
 */
35
class Xml extends AbstractFileParser
36
{
37
    /**
38
     * Loads a XML file as an array.
39
     *
40
     * @param string $path File path
41
     *
42
     * @throws ParseException If there is an error parsing XML file
43
     *
44
     * @return array The parsed data
45
     *
46
     * @since 0.1.0
47
     */
48 6
    public function parse($path)
49
    {
50 6
        $data = $this->loadFile($path);
51
52 6
        if ($data === false) {
53 3
            $lastError = libxml_get_last_error();
54
55 3
            if ($lastError !== false) {
56 3
                throw new ParseException(
57
                    [
58 3
                    'message' => $lastError->message,
59 3
                    'type' => $lastError->level,
60 3
                    'code' => $lastError->code,
61 3
                    'file' => $lastError->file,
62 3
                    'line' => $lastError->line,
63
                    ]
64 2
                );
65
            }
66
        }
67
68 3
        return json_decode(json_encode($data), true);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @return array Supported extensions
75
     *
76
     * @since 0.1.0
77
     */
78 3
    public function getSupportedFileExtensions()
79
    {
80 3
        return ['xml'];
81
    }
82
83
    /**
84
     * Loads in the given file and parses it.
85
     *
86
     * @param string $file File to load
87
     *
88
     * @return array|SimpleXMLElement The parsed file data
89
     *
90
     * @since              0.2.4
91
     * @codeCoverageIgnore
92
     */
93
    protected function loadFile($file = null)
94
    {
95
        $this->file = $file;
96
        $contents = $this->parseVars(Utils::getContent($file));
97
98
        return simplexml_load_string(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return simplexml_load_st...NING | LIBXML_NOERROR); (SimpleXMLElement) is incompatible with the return type declared by the abstract method Exen\Konfig\FileParser\A...actFileParser::loadFile of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
            $contents,
100
            'SimpleXMLElement',
101
            LIBXML_NOWARNING | LIBXML_NOERROR
102
        );
103
    }
104
105
    /**
106
     * Returns the formatted configuration file contents.
107
     *
108
     * @param array $contents configuration array
109
     *
110
     * @return string formatted configuration file contents
111
     *
112
     * @since              0.2.4
113
     * @codeCoverageIgnore
114
     */
115
    protected function exportFormat($contents = null)
116
    {
117
        throw new Exception(
118
            'Saving configuration to `XML` is not supported at this time'
119
        );
120
    }
121
122
    /**
123
     * __toString.
124
     *
125
     * @return             string
126
     * @since              0.1.2
127
     * @codeCoverageIgnore
128
     */
129
    public function __toString()
130
    {
131
        return 'Exen\Konfig\FileParser\Xml' . PHP_EOL;
132
    }
133
}
134
135
// END OF ./src/FileParser/Xml.php FILE
136