Completed
Pull Request — master (#5)
by Carlos C
13:10
created

AbstractMap::put()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 27
ccs 22
cts 22
cp 1
rs 8.439
cc 5
eloc 19
nc 4
nop 2
crap 5
1
<?php namespace GenericCollections\Abstracts;
2
3
use GenericCollections\Collection;
4
use GenericCollections\Interfaces\MapInterface;
5
use GenericCollections\Internal\DataArray;
6
use GenericCollections\Set;
7
8
abstract class AbstractMap extends DataArray implements MapInterface, \ArrayAccess
9
{
10 69
    public function containsKey($key)
11
    {
12 69
        return $this->checkKeyType($key) && array_key_exists($key, $this->data);
13
    }
14
15 9
    public function containsValue($value)
16
    {
17 9
        return in_array($value, $this->data, $this->optionComparisonIsIdentical());
18
    }
19
20 69
    public function get($key)
21
    {
22 69
        return $this->containsKey($key) ? $this->data[$key] : null;
23
    }
24
25 3
    public function getOrDefault($key, $default)
26
    {
27 3
        if ($this->containsKey($key)) {
28 3
            return $this->data[$key];
29
        }
30 3
        if (null !== $default && ! $this->checkValueType($default)) {
31 3
            throw new \InvalidArgumentException(
32
                'The default value provided for '
33 3
                . get_class($this) . '::getOrDefault is not a valid type,'
34 3
                . ' expected ' . $this->getKeyType() . '.'
35 3
            );
36
        }
37 3
        return $default;
38
    }
39
40 6
    public function keys()
41
    {
42 6
        return array_keys($this->data);
43
    }
44
45 75
    public function put($key, $value)
46
    {
47 75
        if (! $this->checkKeyType($key)) {
48 3
            throw new \InvalidArgumentException(
49 3
                'The key provided for ' . get_class($this)
50 3
                . '::put is not a valid type,'
51 3
                . ' expected ' . $this->getKeyType() . '.'
52 3
            );
53
        }
54 72
        if (! $this->checkValueType($value)) {
55 6
            throw new \InvalidArgumentException(
56 6
                'The value provided for ' . get_class($this)
57 6
                . '::put is not a valid type,'
58 6
                . ' expected ' . $this->getValueType() . '.'
59 6
            );
60
        }
61 69
        if ($this->optionUniqueValues() and $this->containsValue($value)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62 3
            throw new \InvalidArgumentException(
63 3
                'The value provided for ' . get_class($this)
64 3
                . '::put is not unique,'
65 3
                . ' this map does not allow duplicated values.'
66 3
            );
67
        }
68 69
        $previous = $this->get($key);
69 69
        $this->data[$key] = $value;
70 69
        return $previous;
71
    }
72
73 84
    public function putAll(array $values)
74
    {
75 84
        foreach ($values as $key => $value) {
76 51
            $this->put($key, $value);
77 84
        }
78 84
    }
79
80 3
    public function putIfAbsent($key, $value)
81
    {
82 3
        $current = $this->get($key);
83 3
        if (null !== $current) {
84 3
            return $current;
85
        }
86 3
        $this->put($key, $value);
87 3
        return null;
88
    }
89
90 6
    public function remove($key)
91
    {
92 6
        $previous = $this->get($key);
93 6
        if ($previous !== null) {
94 6
            unset($this->data[$key]);
95 6
        }
96 6
        return $previous;
97
    }
98
99 3
    public function removeExact($key, $value)
100
    {
101 3
        $changed = false;
102 3
        $previous = $this->get($key);
103 3
        $isequal = ($this->optionComparisonIsIdentical())
104 3
            ? ($previous === $value)
105 3
            : ($previous == $value);
106 3
        if ($isequal) {
107 3
            unset($this->data[$key]);
108 3
            $changed = true;
109 3
        }
110 3
        return $changed;
111
    }
112
113 3
    public function replace($key, $value)
114
    {
115 3
        return ($this->containsKey($key)) ? $this->put($key, $value) : null;
116
    }
117
118 6
    public function replaceExact($key, $current, $replacement)
119
    {
120 6
        if (! $this->containsKey($key)) {
121 3
            return false;
122
        }
123 6
        $previous = $this->get($key);
124 6
        $isequal = ($this->optionComparisonIsIdentical())
125 6
            ? ($previous === $current)
126 6
            : ($previous == $current);
127 6
        if ($isequal) {
128 6
            $this->put($key, $replacement);
129 6
            return true;
130
        }
131 6
        return false;
132
    }
133
134 3
    public function keysSet()
135
    {
136 3
        return new Set($this->getKeyType(), $this->keys());
137
    }
138
139 3
    public function valuesCollection()
140
    {
141 3
        return new Collection($this->getValueType(), $this->toArray());
142
    }
143
144
    /*
145
     * Implementations from \ArrayAccess
146
     */
147
    
148
    /** @inheritdoc */
149 9
    public function offsetExists($offset)
150
    {
151 9
        return $this->containsKey($offset);
152
    }
153
154
    /** @inheritdoc */
155 3
    public function offsetGet($offset)
156
    {
157 3
        return $this->get($offset);
158
    }
159
160
    /** @inheritdoc */
161 6
    public function offsetSet($offset, $value)
162
    {
163 6
        $this->put($offset, $value);
164 3
    }
165
166
    /** @inheritdoc */
167 3
    public function offsetUnset($offset)
168
    {
169 3
        $this->remove($offset);
170 3
    }
171
}
172