ImmutableContainer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 78
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A set() 0 7 1
A get() 0 4 1
A remove() 0 11 2
A setParameter() 0 4 1
A removeParameter() 0 4 1
1
<?php
2
/*
3
 * This file is part of Graze DataStructure
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/data-structure/blob/master/LICENSE
11
 * @link http://github.com/graze/data-structure
12
 */
13
14
namespace Graze\DataStructure\Container;
15
16
class ImmutableContainer extends Container
17
{
18
    /**
19
     * @param array $params
20
     */
21 18
    public function __construct(array $params = [])
22
    {
23 18
        parent::__construct([]);
24
25 18
        foreach ($params as $key => $value) {
26 15
            $this->setParameter($key, $value);
27
        }
28 18
    }
29
30
    /**
31
     * @param string $key
32
     * @param mixed  $value
33
     *
34
     * @return ContainerInterface
35
     */
36 6
    public function set($key, $value)
37
    {
38 6
        $cont = clone $this;
39 6
        $cont->setParameter($key, $value);
40
41 6
        return $cont;
42
    }
43
44
    /**
45
     * Clone the returned value to ensure any modification does not change our version
46
     *
47
     * @param string $key
48
     *
49
     * @return mixed
50
     */
51 4
    public function get($key)
52
    {
53 4
        return $this->recursiveClone(parent::get($key));
54
    }
55
56
    /**
57
     * @param string $key
58
     *
59
     * @return ContainerInterface
60
     */
61 5
    public function remove($key)
62
    {
63 5
        if ($this->has($key)) {
64 4
            $cont = clone $this;
65 4
            $cont->removeParameter($key);
66
67 4
            return $cont;
68
        }
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param mixed  $value
76
     *
77
     * @return ContainerInterface
78
     */
79 17
    protected function setParameter($key, $value)
80
    {
81 17
        return parent::set($key, $this->recursiveClone($value));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (set() instead of setParameter()). Are you sure this is correct? If so, you might want to change this to $this->set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
82
    }
83
84
    /**
85
     * @param string $key
86
     *
87
     * @return ContainerInterface
88
     */
89 4
    protected function removeParameter($key)
90
    {
91 4
        return parent::remove($key);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (remove() instead of removeParameter()). Are you sure this is correct? If so, you might want to change this to $this->remove().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
92
    }
93
}
94