GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#2037)
by Elan
02:09
created

Configuration   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 145
Duplicated Lines 14.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 21
loc 145
ccs 76
cts 76
cp 1
rs 9.68
c 0
b 0
f 0
wmc 34
lcom 1
cbo 1

16 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 8 2
A __construct() 0 4 1
A set() 0 4 1
A has() 11 11 3
A add() 0 12 3
B get() 0 27 7
A fetch() 10 10 3
A parseCallback() 0 4 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A persist() 0 14 4
A load() 0 7 2
A save() 0 4 1
A configFile() 0 3 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 declare(strict_types=1);
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Configuration;
9
10
use Deployer\Collection\Collection;
11
use Deployer\Deployer;
12
use Deployer\Exception\ConfigurationException;
13
use function Deployer\Support\array_merge_alternate;
14
use function Deployer\Support\is_closure;
15
16
class Configuration implements \ArrayAccess
17
{
18
    private $parent;
19
    private $values = [];
20
21 45
    public function __construct(Configuration $parent = null)
22
    {
23 45
        $this->parent = $parent;
24 45
    }
25
26 45
    public function set(string $name, $value)
27
    {
28 45
        $this->values[$name] = $value;
29 45
    }
30
31 19 View Code Duplication
    public function has(string $name): bool
32
    {
33 19
        $ok = array_key_exists($name, $this->values);
34 19
        if ($ok) {
35 16
            return true;
36
        }
37 6
        if ($this->parent) {
38 3
            return $this->parent->has($name);
39
        }
40 4
        return false;
41
    }
42
43 8
    public function add(string $name, array $array)
44
    {
45 8
        if ($this->has($name)) {
46 7
            $config = $this->get($name);
47 7
            if (!is_array($config)) {
48 2
                throw new ConfigurationException("Config option \"$name\" isn't array.");
49
            }
50 5
            $this->set($name, array_merge_alternate($config, $array));
51
        } else {
52 1
            $this->set($name, $array);
53
        }
54 6
    }
55
56 35
    public function get(string $name, $default = null)
57
    {
58 35
        if (array_key_exists($name, $this->values)) {
59 33
            if (is_closure($this->values[$name])) {
60 2
                return $this->values[$name] = $this->parse(call_user_func($this->values[$name]));
61
            } else {
62 33
                return $this->parse($this->values[$name]);
63
            }
64
        }
65
66 21
        if ($this->parent) {
67 16
            $rawValue = $this->parent->fetch($name);
68 16
            if ($rawValue !== null) {
69 14
                if (is_closure($rawValue)) {
70 7
                    return $this->values[$name] = $this->parse(call_user_func($rawValue));
71
                } else {
72 13
                    return $this->values[$name]= $this->parse($rawValue);
73
                }
74
            }
75
        }
76
77 17
        if ($default !== null) {
78 16
            return $this->parse($default);
79
        }
80
81 2
        throw new ConfigurationException("Config option \"$name\" does not exist.");
82
    }
83
84 16 View Code Duplication
    protected function fetch($name)
85
    {
86 16
        if (array_key_exists($name, $this->values)) {
87 14
            return $this->values[$name];
88
        }
89 13
        if ($this->parent) {
90 1
            return $this->parent->fetch($name);
91
        }
92 12
        return null;
93
    }
94
95 34
    public function parse($value)
96
    {
97 34
        if (is_string($value)) {
98 29
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value);
99
        }
100
101 23
        return $value;
102
    }
103
104 13
    private function parseCallback(array $matches)
105
    {
106 13
        return isset($matches[1]) ? $this->get($matches[1]) : null;
107
    }
108
109 11
    public function offsetExists($offset)
110
    {
111 11
        return $this->has($offset);
112
    }
113
114 12
    public function offsetGet($offset)
115
    {
116 12
        return $this->get($offset);
117
    }
118
119 20
    public function offsetSet($offset, $value)
120
    {
121 20
        $this->set($offset, $value);
122 20
    }
123
124 1
    public function offsetUnset($offset)
125
    {
126 1
        unset($this->values[$offset]);
127 1
    }
128
129 10
    public function persist()
130
    {
131 10
        $values = [];
132 10
        if ($this->parent !== null) {
133 10
            $values = $this->parent->persist();
134
        }
135 10
        foreach ($this->values as $key => $value) {
136 10
            if (is_closure($value)) {
137 10
                continue;
138
            }
139 10
            $values[$key] = $value;
140
        }
141 10
        return $values;
142
    }
143
144 9
    public function load()
145
    {
146 9
        $file = $this->configFile();
147 9
        if (file_exists($file)) {
148 8
            $this->values = json_decode(file_get_contents($file), true);
149
        }
150 9
    }
151
152 9
    public function save()
153
    {
154 9
        file_put_contents($this->configFile(), json_encode($this->persist()));
155 9
    }
156
157 9
    private function configFile() {
158 9
        return sprintf('%s/%s.dep', $this->get('config_directory'), str_replace(DIRECTORY_SEPARATOR, "_", $this->get('alias')));
159
    }
160
}
161