Completed
Push — master ( d30cdc...16fa4d )
by Filip
05:56 queued 05:01
created

Ini   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 45
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 4 1
A getSupportedExtensions() 0 4 1
A toINI() 0 16 3
1
<?php
2
3
namespace Noodlehaus\Writer;
4
5
/**
6
 * Ini Writer.
7
 *
8
 * @package    Config
9
 * @author     Jesus A. Domingo <[email protected]>
10
 * @author     Hassan Khan <[email protected]>
11
 * @author     Filip Š <[email protected]>
12
 * @author     Mark de Groot <[email protected]>
13
 * @link       https://github.com/noodlehaus/config
14
 * @license    MIT
15
 */
16
class Ini extends AbstractWriter
17
{
18
    /**
19
     * {@inheritdoc}
20
     * Writes an array to a Ini string.
21
     */
22 9
    public function toString($config, $pretty = true)
23
    {
24 9
        return $this->toINI($config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->toINI($config); (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...
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public static function getSupportedExtensions()
31
    {
32 3
        return ['ini', 'properties'];
33
    }
34
35
    /**
36
     * Converts array to INI string.
37
     * @param array $arr    Array to be converted
38
     * @param array $parent Parent array
39
     *
40
     * @return string Converted array as INI
41
     *
42
     * @see https://stackoverflow.com/a/17317168/6523409/
43
     */
44 9
    protected function toINI(array $arr, array $parent = [])
45
    {
46 9
        $converted = '';
47
48 9
        foreach ($arr as $k => $v) {
49 9
            if (is_array($v)) {
50 9
                $sec = array_merge((array) $parent, (array) $k);
51 9
                $converted .= '['.implode('.', $sec).']'.PHP_EOL;
52 9
                $converted .= $this->toINI($v, $sec);
53
            } else {
54 9
                $converted .= $k.'='.$v.PHP_EOL;
55
            }
56
        }
57
58 9
        return $converted;
59
    }
60
}
61