Completed
Pull Request — develop (#128)
by
unknown
13:33
created

Properties::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
/**
6
 * Properties 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 Properties extends AbstractWriter
17
{
18
    /**
19
     * {@inheritdoc}
20
     * Writes an array to a Properties string.
21
     */
22
    public function toString($config, $pretty = true)
23
    {
24
        return $this->toProperties($config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->toProperties($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
    public static function getSupportedExtensions()
31
    {
32
        return ['properties'];
33
    }
34
35
    /**
36
     * Converts array to Properties string.
37
     * @param array $arr    Array to be converted
38
     *
39
     * @return string Converted array as Properties
40
     */
41
    protected function toProperties(array $arr)
42
    {
43
        $converted = '';
44
45
        foreach ($arr as $key => $value) {
46
            if (is_array($value)) {
47
                continue;
48
            }
49
50
            // Escape all space, ; and = characters in the key:
51
            $key = addcslashes($key, ' :=');
52
53
            // Escape all backslashes and newlines in the value:
54
            $value = preg_replace('/([\r\n\t\f\v\\\])/', '\\\$1', $value);
55
56
            $converted .= $key.' = '.$value.PHP_EOL;
57
        }
58
59
        return $converted;
60
    }
61
}
62