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.
Completed
Push — master ( 8a3c78...eab095 )
by Anton
03:54
created

Environment::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
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\Server;
9
10
use Deployer\Collection\Collection;
11
use Deployer\Deployer;
12
13
class Environment
14
{
15
    const DEPLOY_PATH = 'deploy_path';
16
17
    /**
18
     * Array of set values.
19
     * @var Collection
20
     */
21
    private $values = null;
22
23
    /**
24
     * Values represented by their keys here are protected, and cannot be
25
     * changed by calling the `set` method.
26
     * @var array
27
     */
28
    private $protectedNames = [];
29
30
    /**
31
     * Constructor
32
     */
33
    public function __construct()
34
    {
35
        $this->values = new Collection();
36
    }
37
38
    /**
39 53
     * @return Collection
40
     */
41 53
    public function getValues()
42 53
    {
43
        return $this->values;
44
    }
45
46
    /**
47
     * @param string $name
48 37
     * @param bool|int|string|array $value
49
     */
50 37
    public function set($name, $value)
51 37
    {
52 37
        $this->checkIfNameIsProtected($name);
53
        $this->values[$name] = $value;
54
    }
55
56
    /**
57
     * @param string $name
58 25
     * @param bool|int|string|array $value
59
     */
60 25
    public function setAsProtected($name, $value)
61 25
    {
62 25
        $this->set($name, $value);
63
        $this->protectedNames[] = $name;
64
    }
65
66
    /**
67
     * Checks whether the given name was registered as protected, or if there is
68
     * a protected parameter which would be overwritten.
69
     * @param string $name
70
     * @throws \RuntimeException if the value already exists and is protected.
71
     * @throws \RuntimeException if there's a protected parameter which would
72 37
     * be overwritten.
73
     */
74 37
    private function checkIfNameIsProtected($name)
75
    {
76 37
        $length = strlen($name);
77 21
        
78 21
        foreach ($this->protectedNames as $protectedName) {
79 2
            $len = strlen($protectedName);
80 19
            if ($name === $protectedName) {
81 2
                throw new \RuntimeException("The parameter `$name` cannot be set, because it's protected.");
82 17
            } elseif ($len < $length && '.' === $name[$len] && 0 === strpos($name, $protectedName)) {
83 1
                throw new \RuntimeException("The parameter `$name` cannot be set, because `$protectedName` is protected.");
84
            } elseif ($len > $length && '.' === $protectedName[$length] && 0 === strpos($protectedName, $name)) {
85 37
                throw new \RuntimeException("The parameter `$name` could not be set, because a protected parameter named `$protectedName` already exists.");
86 37
            }
87
        }
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param array $array
93
     */
94 24
    public function add($name, $array)
95
    {
96 24
        if ($this->has($name)) {
97 22
            $config = $this->get($name);
98 22
            if (!is_array($config)) {
99 18
                throw new \RuntimeException("Configuration parameter `$name` isn't array.");
100 11
            }
101 11
            $this->set($name, array_merge($config, $array));
102 11
        } else {
103 5
            $this->set($name, $array);
104
        }
105 11
    }
106 18
107 1
    /**
108
     * @param string $name
109 18
     * @param bool|int|string|array $default
110
     * @return bool|int|string|array
111
     * @throws \RuntimeException
112
     */
113
    public function get($name, $default = null)
114 24
    {
115
        if ($this->values->has($name)) {
116
            if ($this->isClosure($this->values[$name])) {
117
                $value = $this->values[$name] = call_user_func($this->values[$name]);
118
            } else {
119
                $value = $this->values[$name];
120
            }
121
        } else {
122
            $config = Deployer::get()->config;
123 16
124
            if (isset($config[$name])) {
125 16
                if ($this->isClosure($config[$name])) {
126
                    $value = $this->values[$name] = call_user_func($config[$name]);
127
                } else {
128
                    $value = $this->values[$name] = $config[$name];
129
                }
130
            } else {
131
                if (null === $default) {
132 2
                    throw new \RuntimeException("Configuration parameter `$name` does not exists.");
133
                } else {
134 2
                    $value = $default;
135
                }
136
            }
137 2
        }
138
139
        return $this->parse($value);
140
    }
141
142
    /**
143
     * Checks if set var exists.
144 15
     *
145
     * @param string $name
146 15
     * @return bool
147 1
     */
148 1
    public function has($name)
149 15
    {
150 15
        return $this->values->has($name);
151
    }
152
153
    /**
154
     * Parse set values.
155
     *
156
     * @param string $value
157
     * @return string
158 25
     */
159
    public function parse($value)
160 25
    {
161 22
        if (is_string($value)) {
162 22
            $value = preg_replace_callback('/\{\{\s*([\w\.\/]+)\s*\}\}/', [$this, 'parseCallback'], $value);
163
        }
164 25
165
        return $value;
166
    }
167
168
    /**
169
     * Replace set values callback for parse
170
     *
171
     * @param array $matches
172
     * @return mixed
173 16
     */
174
    private function parseCallback($matches)
175 16
    {
176
        return isset($matches[1]) ? $this->get($matches[1]) : null;
177
    }
178
179
    /**
180
     * @param mixed $t
181
     * @return bool
182
     */
183
    private function isClosure($t)
184
    {
185
        return is_object($t) && ($t instanceof \Closure);
186
    }
187
}
188