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 ( 9000d2...f19783 )
by Time
06:05
created

OrderedMap   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 41
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 293
rs 8.2769

20 Methods

Rating   Name   Duplication   Size   Complexity  
A keys() 0 4 1
A values() 0 4 1
A map() 0 14 2
A walk() 0 8 2
A groupBy() 0 22 3
B multisort() 0 42 6
A count() 0 4 1
A loadLargestIntKey() 0 9 4
A get() 0 12 2
A contains() 0 4 1
A set() 0 4 1
A setRef() 0 4 1
B setInternal() 0 19 5
B remove() 0 26 4
A clear() 0 8 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 7 2
A offsetUnset() 0 4 1
A isArrayCompatible() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like OrderedMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OrderedMap, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Pinq\Iterators\Common;
4
5
/**
6
 * Contains the common functionality for the IOrderedMap implementation.
7
 *
8
 * @author Elliot Levin <[email protected]>
9
 */
10
trait OrderedMap
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $keys = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $values = [];
21
22
    /**
23
     * @var array
24
     */
25
    protected $keyIdentityPositionMap = [];
26
27
    /**
28
     * @var int
29
     */
30
    protected $length = 0;
31
32
    /**
33
     * @var int
34
     */
35
    protected $largestIntKey = -1;
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function keys()
41
    {
42
        return $this->keys;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function values()
49
    {
50
        return $this->values;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function map(callable $function)
57
    {
58
        $function = Functions::allowExcessiveArguments($function);
59
60
        $clone = clone $this;
61
62
        foreach ($clone->keyIdentityPositionMap as $position) {
63
            $keyCopy                  = $this->keys[$position];
64
            $valueCopy                = $this->values[$position];
65
            $clone->values[$position] = $function($valueCopy, $keyCopy);
66
        }
67
68
        return $clone;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function walk(callable $function)
75
    {
76
        $function = Functions::allowExcessiveArguments($function);
77
78
        foreach ($this->keys as $position => $key) {
79
            $function($this->values[$position], $key);
80
        }
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function groupBy(callable $groupKeyFunction)
87
    {
88
        $groupedMap = new self();
89
90
        foreach ($this->keyIdentityPositionMap as $identityHash => $position) {
91
            $keyCopy   = $key = $this->keys[$position];
92
            $valueCopy = $value =& $this->values[$position];
93
94
            $groupKey = $groupKeyFunction($valueCopy, $keyCopy);
95
96
            if ($groupedMap->contains($groupKey)) {
97
                $groupMap = $groupedMap->get($groupKey);
98
            } else {
99
                $groupMap = new self();
100
                $groupedMap->set($groupKey, $groupMap);
101
            }
102
103
            $groupMap->setInternal($key, $value, $identityHash, true);
104
        }
105
106
        return $groupedMap;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function multisort(array $orderByFunctions, array $isAscending)
113
    {
114
        $positionKeyIdentityMap = [];
115
        $multisortArguments     = [];
116
117
        foreach ($this->keyIdentityPositionMap as $keyIdentityHash => $position) {
118
            $positionKeyIdentityMap['0' . $position] = $keyIdentityHash;
119
        }
120
121
        foreach ($orderByFunctions as $key => $function) {
122
            $orderByValues = [];
123
124
            foreach ($this->keyIdentityPositionMap as $position) {
125
                $orderByValues['0' . $position] = $function($this->values[$position], $this->keys[$position]);
126
            }
127
128
            $multisortArguments[] =& $orderByValues;
129
            $multisortArguments[] = $isAscending[$key] ? SORT_ASC : SORT_DESC;
130
            $multisortArguments[] = SORT_REGULAR;
131
132
            unset($orderByValues);
133
        }
134
135
        $multisortArguments[] =& $positionKeyIdentityMap;
136
137
        call_user_func_array('array_multisort', $multisortArguments);
138
139
        $sortedMap = new self();
140
141
        $newPosition = 0;
142
        foreach ($positionKeyIdentityMap as $stringPosition => $keyIdentityHash) {
143
            $originalPosition = (int) $stringPosition;
144
145
            $sortedMap->keyIdentityPositionMap[$keyIdentityHash] = $newPosition;
146
            $sortedMap->keys[$newPosition]                       = $this->keys[$originalPosition];
147
            $sortedMap->values[$newPosition]                     =& $this->values[$originalPosition];
148
149
            $newPosition++;
150
        }
151
152
        return $sortedMap;
153
    }
154
155
    public function count()
156
    {
157
        return $this->length;
158
    }
159
160
    private function loadLargestIntKey()
161
    {
162
        $this->largestIntKey = -1;
163
        foreach ($this->keys as $key) {
164
            if (is_int($key) && $key > $this->largestIntKey) {
165
                $this->largestIntKey = $key;
166
            }
167
        }
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function &get($key)
174
    {
175
        $identityHash = Identity::hash($key);
176
177
        if (isset($this->keyIdentityPositionMap[$identityHash])) {
178
            return $this->values[$this->keyIdentityPositionMap[$identityHash]];
179
        } else {
180
            $null = null;
181
182
            return $null;
183
        }
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189
    public function contains($key)
190
    {
191
        return isset($this->keyIdentityPositionMap[Identity::hash($key)]);
192
    }
193
194
    /**
195
     * {@inheritDoc}
196
     */
197
    public function set($key, $value)
198
    {
199
        $this->setInternal($key, $value, Identity::hash($key));
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205
    public function setRef($key, &$value)
206
    {
207
        $this->setInternal($key, $value, Identity::hash($key), true);
208
    }
209
210
    final protected function setInternal($key, &$value, $identityHash, $reference = false)
211
    {
212
        if (isset($this->keyIdentityPositionMap[$identityHash])) {
213
            $position = $this->keyIdentityPositionMap[$identityHash];
214
        } else {
215
            $position                                    = $this->length++;
216
            $this->keyIdentityPositionMap[$identityHash] = $position;
217
        }
218
        if (is_int($key) && $key > $this->largestIntKey) {
219
            $this->largestIntKey = $key;
220
        }
221
222
        $this->keys[$position] = $key;
223
        if ($reference) {
224
            $this->values[$position] =& $value;
225
        } else {
226
            $this->values[$position] = $value;
227
        }
228
    }
229
230
    /**
231
     * {@inheritDoc}
232
     */
233
    public function remove($key)
234
    {
235
        $identityHash = Identity::hash($key);
236
237
        if (isset($this->keyIdentityPositionMap[$identityHash])) {
238
            $position = $this->keyIdentityPositionMap[$identityHash];
239
240
            unset($this->keys[$position],
241
            $this->values[$position],
242
            $this->keyIdentityPositionMap[$identityHash]);
243
244
            if ($position !== $this->length) {
245
                $this->keys   = array_values($this->keys);
246
                $this->values = array_values($this->values);
247
            }
248
            $this->length--;
249
250
            if ($key === $this->largestIntKey) {
251
                $this->loadLargestIntKey();
252
            }
253
254
            return true;
255
        }
256
257
        return false;
258
    }
259
260
    /**
261
     * {@inheritDoc}
262
     */
263
    public function clear()
264
    {
265
        $this->keyIdentityPositionMap = [];
266
        $this->keys                   = [];
267
        $this->values                 = [];
268
        $this->length                 = 0;
269
        $this->largestIntKey          = -1;
270
    }
271
272
    public function offsetExists($offset)
273
    {
274
        return $this->contains($offset);
275
    }
276
277
    public function &offsetGet($offset)
278
    {
279
        return $this->get($offset);
280
    }
281
282
    public function offsetSet($offset, $value)
283
    {
284
        if ($offset === null) {
285
            $offset = ++$this->largestIntKey;
286
        }
287
        $this->set($offset, $value);
288
    }
289
290
    public function offsetUnset($offset)
291
    {
292
        return $this->remove($offset);
293
    }
294
295
    /**
296
     * @return bool
297
     */
298
    final public function isArrayCompatible()
299
    {
300
        return false;
301
    }
302
}
303