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 (#1904)
by Anton
02:17
created

Configuration::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
15
class Configuration implements \ArrayAccess
16
{
17
    public $parent;
18
    private $collection;
19
20 33
    public function __construct(Configuration $parent = null)
21
    {
22 33
        $this->parent = $parent;
23 33
        $this->collection = new Collection();
24 33
    }
25
26
    public function getCollection(): Collection
27
    {
28
        return $this->collection;
29
    }
30
31
    public function setCollection(Collection $collection)
32
    {
33
        $this->collection = $collection;
34
    }
35
36 33
    public function set(string $name, $value)
37
    {
38 33
        $this->collection[$name] = $value;
39 33
    }
40
41 5
    public function add(string $name, array $array)
42
    {
43 5
        if ($this->has($name)) {
44 5
            $config = $this->get($name);
45 5
            if (!is_array($config)) {
46 2
                throw new ConfigurationException("Configuration parameter `$name` isn't array.");
47
            }
48 3
            $this->set($name, array_merge_alternate($config, $array));
49
        } else {
50
            $this->set($name, $array);
51
        }
52 3
    }
53
54 23
    public function get(string $name, $default = null)
55
    {
56 23
        if ($this->collection->has($name)) {
57 22
            if ($this->isClosure($this->collection[$name])) {
58 1
                $value = $this->collection[$name] = call_user_func($this->collection[$name]);
59
            } else {
60 22
                $value = $this->collection[$name];
61
            }
62 6
        } else if ($this->parent && $this->parent->has($name)) {
63 1
            $value = $this->collection[$name] = $this->parent->get($name, $default);
64
        } else {
65 5
            if (null === $default) {
66 1
                throw new ConfigurationException("Configuration parameter `$name` does not exist.");
67
            } else {
68 5
                $value = $default;
69
            }
70
        }
71
72 23
        return $this->parse($value);
73
    }
74
75 8
    public function has(string $name): bool
76
    {
77 8
        return $this->collection->has($name);
78
    }
79
80 23
    public function parse($value)
81
    {
82 23
        if (is_string($value)) {
83 18
            return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value);
84
        }
85
86 14
        return $value;
87
    }
88
89 1
    public function offsetExists($offset)
90
    {
91 1
        return $this->has($offset);
92
    }
93
94 3
    public function offsetGet($offset)
95
    {
96 3
        return $this->get($offset);
97
    }
98
99 10
    public function offsetSet($offset, $value)
100
    {
101 10
        $this->set($offset, $value);
102 10
    }
103
104
    public function offsetUnset($offset)
105
    {
106
        unset($this->collection[$offset]);
107
    }
108
109
    public function persist()
110
    {
111
        $values = [];
112
        if ($this->parent !== null) {
113
            $values = $this->parent->persist();
114
        }
115
        foreach ($this->collection as $key => $value) {
116
            if ($this->isClosure($value)) {
117
                continue;
118
            }
119
            $values[$key] = $value;
120
        }
121
        return $values;
122
    }
123
124 3
    private function parseCallback(array $matches)
125
    {
126 3
        return isset($matches[1]) ? $this->get($matches[1]) : null;
127
    }
128
129 22
    private function isClosure($var)
130
    {
131 22
        return is_object($var) && ($var instanceof \Closure);
132
    }
133
}
134