Test Failed
Push — master ( b5ddc7...73c405 )
by Konstantins
05:51
created

Config::push()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Config;
4
5
use ArrayIterator;
6
use RuntimeException;
7
use Traversable;
8
use Venta\Contracts\Config\Config as ConfigContract;
9
10
/**
11
 * Class Config
12
 *
13
 * @package Venta\Config
14
 */
15
final class Config implements ConfigContract
16
{
17
    /**
18
     * Array of configuration items.
19
     *
20
     * @var array
21
     */
22
    private $items = [];
23
24
    /**
25
     * Construct function.
26
     *
27
     * @param array $items
28
     */
29
    public function __construct(array $items)
30
    {
31
        $this->items = $items;
32
    }
33
34
    /**
35
     * Returns config value for provided key.
36
     *
37
     * @param string $key
38
     * @return mixed
39
     */
40
    public function __get(string $key)
41
    {
42
        if ($this->__isset($key)) {
43 47
            return is_array($this->items[$key]) ? new self($this->items[$key]) : $this->items[$key];
44
        }
45 47
46 47
        return null;
47 36
    }
48
49 47
    /**
50
     * Checks if config contains value for provided key.
51
     *
52
     * @param $key
53
     * @return bool
54 19
     */
55
    public function __isset(string $key): bool
56 19
    {
57
        return array_key_exists($key, $this->items);
58 19
    }
59 19
60 13
    /**
61
     * @inheritDoc
62 19
     */
63
    public function count()
64
    {
65
        return count($this->items);
66 19
    }
67 19
68 19
    /**
69
     * @inheritDoc
70
     */
71
    public function getIterator()
72
    {
73 1
        return $this->items;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->items; (array) is incompatible with the return type declared by the interface IteratorAggregate::getIterator of type Traversable.

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...
74
    }
75 1
76
    /**
77
     * @inheritDoc
78
     */
79
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
    {
81 2
        return $this->items;
82
    }
83 2
84 1
    /**
85
     * Returns array representation of config.
86
     *
87
     * @return array
88
     */
89 2
    public function toArray(): array
90
    {
91 2
        return $this->items;
92
    }
93
}