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 ( 99078a...9605c1 )
by Oanh
02:49
created

Environment   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 166
Duplicated Lines 8.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.49%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 27
c 3
b 1
f 1
lcom 1
cbo 1
dl 14
loc 166
ccs 55
cts 57
cp 0.9649
rs 10

10 Methods

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