Settings   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 22.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 24
loc 106
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 12 12 3
A tryGet() 12 12 3
A has() 0 8 3
A set() 0 5 1
A setDefault() 0 5 1
A assertWriteable() 0 6 2
A preventChanges() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace PSB\Core\Util;
3
4
5
use PSB\Core\Exception\RuntimeException;
6
use PSB\Core\Exception\OutOfBoundsException;
7
8
class Settings implements ReadOnlySettingsInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $defaults = [];
14
15
    /**
16
     * @var array
17
     */
18
    private $overrides = [];
19
20
    /**
21
     * @var bool
22
     */
23
    private $locked = false;
24
25
    /**
26
     * Returns the value for the $key. Throws an exception is value is not found in overrides.
27
     *
28
     * @param string $key
29
     *
30
     * @throws OutOfBoundsException
31
     * @return mixed
32
     */
33 3 View Code Duplication
    public function get($key)
34
    {
35 3
        if (isset($this->overrides[$key])) {
36 1
            return $this->overrides[$key];
37
        }
38
39 2
        if (isset($this->defaults[$key])) {
40 1
            return $this->defaults[$key];
41
        }
42
43 1
        throw new OutOfBoundsException("The given key '$key' was not present in the settings.");
44
    }
45
46
    /**
47
     * Returns the value for the $key searching through overrides and defaults. Returns null if not found.
48
     *
49
     * @param string $key
50
     *
51
     * @return mixed
52
     */
53 4 View Code Duplication
    public function tryGet($key)
54
    {
55 4
        if (isset($this->overrides[$key])) {
56 1
            return $this->overrides[$key];
57
        }
58
59 3
        if (isset($this->defaults[$key])) {
60 2
            return $this->defaults[$key];
61
        }
62
63 2
        return null;
64
    }
65
66
    /**
67
     * Returns true if there is an override or a default value for $key
68
     *
69
     * @param string $key
70
     *
71
     * @return bool
72
     */
73 3
    public function has($key)
74
    {
75 3
        if (isset($this->overrides[$key]) || isset($this->defaults[$key])) {
76 2
            return true;
77
        }
78
79 1
        return false;
80
    }
81
82
    /**
83
     * @param string $key
84
     * @param mixed  $value
85
     */
86 5
    public function set($key, $value)
87
    {
88 5
        $this->assertWriteable();
89 4
        $this->overrides[$key] = $value;
90 4
    }
91
92
    /**
93
     * @param string $key
94
     * @param mixed  $value
95
     */
96 5
    public function setDefault($key, $value)
97
    {
98 5
        $this->assertWriteable();
99 4
        $this->defaults[$key] = $value;
100 4
    }
101
102 9
    private function assertWriteable()
103
    {
104 9
        if ($this->locked) {
105 2
            throw new RuntimeException("Settings container has been write private.");
106
        }
107 7
    }
108
109 2
    public function preventChanges()
110
    {
111 2
        $this->locked = true;
112
    }
113
}