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

DotArray   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 159
Duplicated Lines 7.55 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 98.39%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 21
c 2
b 1
f 1
lcom 1
cbo 0
dl 12
loc 159
ccs 61
cts 62
cp 0.9839
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 4 1
A offsetGet() 6 14 4
A validateKey() 0 4 1
A offsetExists() 0 4 1
A offsetSet() 0 19 4
A __construct() 0 6 2
A explodeKey() 0 8 2
A hasKey() 0 15 3
A offsetUnset() 6 13 3

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\Type;
9
10
/**
11
 * DotArray
12
 * Allow access $array['abc.xyz'] same $array['abc']['xyz']
13
 *
14
 * Thank Glynn Forrest with https://github.com/glynnforrest/Crutches
15
 *
16
 * @author OanhNN <[email protected]>
17
 * @version 1.0
18
 */
19
class DotArray implements \ArrayAccess
20
{
21
    /**
22
     * Storage array
23
     *
24
     * @var array
25
     */
26
    protected $array = [];
27
28
    /**
29
     * @param array $array
30
     */
31 53
    public function __construct($array = [])
32
    {
33 53
        foreach ($array as $key => $value) {
34 26
            $this->offsetSet($key, $value);
35 53
        }
36 53
    }
37
38
    /**
39
     * Validate key
40
     *
41
     * @param string $key
42
     * @return bool
43
     */
44 39
    public static function validateKey($key)
45
    {
46 39
        return (bool) preg_match('/^(\w|\.)+$/', $key);
47
    }
48
49
    /**
50
     * Explode key by separator character (a dot character)
51
     *
52
     * @param string $key
53
     * @return array
54
     * @throws \RuntimeException
55
     */
56 39
    protected function explodeKey($key)
57
    {
58 39
        if (!self::validateKey($key)) {
59 1
            throw new \RuntimeException("Key `$key` is invalid");
60
        }
61
62 38
        return explode('.', $key);
63
    }
64
65
    /**
66
     * Check has key
67
     *
68
     * @param string $key
69
     * @return bool
70
     */
71 25
    public function hasKey($key)
72
    {
73 25
        $parts = $this->explodeKey($key);
74 25
        $count = count($parts) - 1;
75 25
        $cKey = array_pop($parts);
76
        
77 25
        if (0 == $count) {
78 24
            $array = $this->array;
79 24
        } else {
80 1
            $pKey = implode('.', $parts);
81 1
            $array = $this->offsetGet($pKey);
82
        }
83
        
84 25
        return is_array($array) && array_key_exists($cKey, $array);
85
    }
86
87
    /**
88
     * Get all value as array
89
     *
90
     * @return array
91
     */
92 28
    public function toArray()
93
    {
94 28
        return $this->array;
95
    }
96
97
    /**
98
     * Check exist key . Like isset(), a value of null is considered not set.
99
     * isset($array['abc.xyz']) same isset($array['abc']['xyz'])
100
     *
101
     * @param string $key
102
     * @return bool
103
     */
104 17
    public function offsetExists($key)
105
    {
106 17
        return null !== $this->offsetGet($key);
107
    }
108
109
    /**
110
     * Get an array value
111
     * $array['abc.xyz'] same $array['abc']['xyz']
112
     *
113
     * @param string $key
114
     * @return mixed NULL will be returned if the key is not found.
115
     */
116 25
    public function offsetGet($key)
117
    {
118 25
        $parts = $this->explodeKey($key);
119 25
        $scope = &$this->array;
120 25
        $count = count($parts) - 1;
121 25 View Code Duplication
        for ($i = 0; $i < $count; $i++) {
122 2
            if (!isset($scope[$parts[$i]])) {
123
                return null;
124
            }
125 2
            $scope = &$scope[$parts[$i]];
126 2
        }
127
        
128 25
        return isset($scope[$parts[$i]]) ? $scope[$parts[$i]] : null;
129
    }
130
131
    /**
132
     * Set an array value
133
     * $array['abc.xyz'] = 'value' same $array['abc']['xyz'] = 'value'
134
     *
135
     * @param string $key
136
     * @param mixed $value
137
     */
138 37
    public function offsetSet($key, $value)
139
    {
140 37
        $parts = $this->explodeKey($key);
141 36
        $scope = &$this->array;
142 36
        $count = count($parts) - 1;
143
        //loop through each part, create it if not present.
144 36
        for ($i = 0; $i < $count; $i++) {
145 5
            if (!isset($scope[$parts[$i]])) {
146 2
                $scope[$parts[$i]] = [];
147 2
            }
148 5
            $scope = &$scope[$parts[$i]];
149 5
        }
150 36
        if (is_array($value)) {
151 28
            $tmp = new static($value);
152 28
            $scope[$parts[$i]] = $tmp->toArray();
153 28
        } else {
154 36
            $scope[$parts[$i]] = $value;
155
        }
156 36
    }
157
158
    /**
159
     * Unset an array value
160
     * using unset($array['abc.xyz']) to unset($array['abc']['xyz'])
161
     *
162
     * @param string $key
163
     */
164 2
    public function offsetUnset($key)
165
    {
166 2
        $parts = $this->explodeKey($key);
167 2
        $scope = &$this->array;
168 2
        $count = count($parts) - 1;
169 2 View Code Duplication
        for ($i = 0; $i < $count; $i++) {
170 1
            if (!isset($scope[$parts[$i]])) {
171 1
                return;
172
            }
173 1
            $scope = &$scope[$parts[$i]];
174 1
        }
175 2
        unset($scope[$parts[$i]]);
176 2
    }
177
}
178