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.

Configuration   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 192
rs 9.6
c 0
b 0
f 0
wmc 35

18 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 4 1
B get() 0 26 7
A offsetUnset() 0 4 1
A bind() 0 3 1
A load() 0 14 2
A offsetSet() 0 4 1
A keys() 0 3 1
A offsetExists() 0 4 1
A __construct() 0 3 1
A set() 0 3 1
A hasOwn() 0 3 1
A has() 0 10 3
A save() 0 14 2
A fetch() 0 9 3
A update() 0 3 1
A add() 0 10 3
A persist() 0 10 3
A parse() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* (c) Anton Medvedev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Deployer;
12
13
use Deployer\Exception\ConfigurationException;
14
use Deployer\Utility\Httpie;
15
16
use function Deployer\Support\array_merge_alternate;
17
use function Deployer\Support\is_closure;
18
use function Deployer\Support\normalize_line_endings;
19
20
class Configuration implements \ArrayAccess
21
{
22
    private ?Configuration $parent;
23
    private array $values = [];
24
25
    public function __construct(?Configuration $parent = null)
26
    {
27
        $this->parent = $parent;
28
    }
29
30
    public function update(array $values): void
31
    {
32
        $this->values = array_merge($this->values, $values);
33
    }
34
35
    public function bind(Configuration $parent): void
36
    {
37
        $this->parent = $parent;
38
    }
39
40
    public function set(string $name, mixed $value): void
41
    {
42
        $this->values[$name] = $value;
43
    }
44
45
    public function has(string $name): bool
46
    {
47
        $ok = array_key_exists($name, $this->values);
48
        if ($ok) {
49
            return true;
50
        }
51
        if ($this->parent) {
52
            return $this->parent->has($name);
53
        }
54
        return false;
55
    }
56
57
    public function hasOwn(string $name): bool
58
    {
59
        return array_key_exists($name, $this->values);
60
    }
61
62
    public function add(string $name, array $array): void
63
    {
64
        if ($this->has($name)) {
65
            $config = $this->get($name);
66
            if (!is_array($config)) {
67
                throw new ConfigurationException("Config option \"$name\" isn't array.");
68
            }
69
            $this->set($name, array_merge_alternate($config, $array));
70
        } else {
71
            $this->set($name, $array);
72
        }
73
    }
74
75
    public function get(string $name, mixed $default = null): mixed
76
    {
77
        if (array_key_exists($name, $this->values)) {
78
            if (is_closure($this->values[$name])) {
79
                return $this->values[$name] = $this->parse(call_user_func($this->values[$name]));
80
            } else {
81
                return $this->parse($this->values[$name]);
82
            }
83
        }
84
85
        if ($this->parent) {
86
            $rawValue = $this->parent->fetch($name);
87
            if ($rawValue !== null) {
88
                if (is_closure($rawValue)) {
89
                    return $this->values[$name] = $this->parse(call_user_func($rawValue));
90
                } else {
91
                    return $this->values[$name] = $this->parse($rawValue);
92
                }
93
            }
94
        }
95
96
        if (func_num_args() >= 2) {
97
            return $this->parse($default);
98
        }
99
100
        throw new ConfigurationException("Config option \"$name\" does not exist.");
101
    }
102
103
    protected function fetch(string $name): mixed
104
    {
105
        if (array_key_exists($name, $this->values)) {
106
            return $this->values[$name];
107
        }
108
        if ($this->parent) {
109
            return $this->parent->fetch($name);
110
        }
111
        return null;
112
    }
113
114
    public function parse(mixed $value): mixed
115
    {
116
        if (is_string($value)) {
117
            $normalizedValue = normalize_line_endings($value);
118
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', function (array $matches) {
119
                return $this->get($matches[1]);
120
            }, $normalizedValue);
121
        }
122
123
        return $value;
124
    }
125
126
    public function keys(): array
127
    {
128
        return array_keys($this->values);
129
    }
130
131
    /**
132
     * @param string $offset
133
     * @return bool
134
     */
135
    #[\ReturnTypeWillChange]
136
    public function offsetExists($offset)
137
    {
138
        return $this->has($offset);
139
    }
140
141
    /**
142
     * @param string $offset
143
     * @return mixed
144
     */
145
    #[\ReturnTypeWillChange]
146
    public function offsetGet($offset)
147
    {
148
        return $this->get($offset);
149
    }
150
151
    /**
152
     * @param string $offset
153
     * @param mixed $value
154
     */
155
    #[\ReturnTypeWillChange]
156
    public function offsetSet($offset, $value): void
157
    {
158
        $this->set($offset, $value);
159
    }
160
161
    /**
162
     * @param mixed $offset
163
     */
164
    #[\ReturnTypeWillChange]
165
    public function offsetUnset($offset): void
166
    {
167
        unset($this->values[$offset]);
168
    }
169
170
    public function load(): void
171
    {
172
        if (!Deployer::isWorker()) {
173
            return;
174
        }
175
176
        $values = Httpie::get(MASTER_ENDPOINT . '/load')
177
            ->setopt(CURLOPT_CONNECTTIMEOUT, 0)
178
            ->setopt(CURLOPT_TIMEOUT, 0)
179
            ->jsonBody([
180
                'host' => $this->get('alias'),
181
            ])
182
            ->getJson();
183
        $this->update($values);
184
    }
185
186
    public function save(): void
187
    {
188
        if (!Deployer::isWorker()) {
189
            return;
190
        }
191
192
        Httpie::get(MASTER_ENDPOINT . '/save')
193
            ->setopt(CURLOPT_CONNECTTIMEOUT, 0)
194
            ->setopt(CURLOPT_TIMEOUT, 0)
195
            ->jsonBody([
196
                'host' => $this->get('alias'),
197
                'config' => $this->persist(),
198
            ])
199
            ->getJson();
200
    }
201
202
    public function persist(): array
203
    {
204
        $values = [];
205
        foreach ($this->values as $key => $value) {
206
            if (is_closure($value)) {
207
                continue;
208
            }
209
            $values[$key] = $value;
210
        }
211
        return $values;
212
    }
213
}
214