Completed
Pull Request — develop (#121)
by
unknown
12:28
created

Xml::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Noodlehaus\Writer;
4
5
use DOMDocument;
6
use SimpleXMLElement;
7
8
/**
9
 * Xml Writer.
10
 *
11
 * @package    Config
12
 * @author     Jesus A. Domingo <[email protected]>
13
 * @author     Hassan Khan <[email protected]>
14
 * @author     Filip Š <[email protected]>
15
 * @author     Mark de Groot <[email protected]>
16
 * @link       https://github.com/noodlehaus/config
17
 * @license    MIT
18
 */
19
class Xml extends AbstractWriter
20
{
21
    /**
22
     * {@inheritdoc}
23
     * Writes an array to a Xml string.
24
     */
25
    public function toString($config, $pretty = true)
26
    {
27
        $xml = $this->toXML($config);
28
        if ($pretty == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
29
            return $xml;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $xml; (string|false) is incompatible with the return type declared by the interface Noodlehaus\Writer\WriterInterface::toString 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...
30
        }
31
32
        $dom = new DOMDocument();
33
        $dom->preserveWhiteSpace = false;
34
        $dom->formatOutput = true;
35
        $dom->loadXML($xml);
36
37
        return $dom->saveXML();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $dom->saveXML(); (string) is incompatible with the return type declared by the interface Noodlehaus\Writer\WriterInterface::toString 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...
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function getSupportedExtensions()
44
    {
45
        return ['xml'];
46
    }
47
48
    /**
49
     * Converts array to XML string.
50
     * @param array             $arr       Array to be converted
51
     * @param string            $rootElement I specified will be taken as root element
52
     * @param SimpleXMLElement  $xml         If specified content will be appended
53
     *
54
     * @return string Converted array as XML
55
     *
56
     * @see https://www.kerstner.at/2011/12/php-array-to-xml-conversion/
57
     */
58
    protected function toXML(array $arr, $rootElement = '<config/>', $xml = null)
59
    {
60
        if ($xml === null) {
61
            $xml = new SimpleXMLElement($rootElement);
62
        }
63
        foreach ($arr as $k => $v) {
64
            if (is_array($v)) {
65
                $this->toXML($v, $k, $xml->addChild($k));
66
            } else {
67
                $xml->addChild($k, $v);
68
            }
69
        }
70
71
        return $xml->asXML();
72
    }
73
}
74