Completed
Push — master ( 58a491...5fc901 )
by Ben
02:04
created

AbstractMap   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 20.41 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
c 3
b 0
f 1
lcom 1
cbo 1
dl 20
loc 98
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 11 2
A containsKey() 0 4 1
A containsValue() 0 4 1
A keys() 0 4 1
A get() 0 8 2
A put() 0 7 1
A putIfAbsent() 10 10 2
A remove() 0 7 1
A removeIf() 0 10 2
A replace() 10 10 2
A replaceIf() 0 10 2

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
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection\Map;
16
17
use Ramsey\Collection\AbstractArray;
18
19
/**
20
 * This class provides an implementation of the MapInterface, to
21
 * minimize the effort required to implement this interface
22
 */
23
abstract class AbstractMap extends AbstractArray implements MapInterface
24
{
25
    public function offsetSet($offset, $value)
26
    {
27
        if ($offset === null) {
28
            throw new \InvalidArgumentException(
29
                'Map elements are key/value pairs; a key must be provided for '
30
                . 'value ' . (string) $value
31
            );
32
        }
33
34
        $this->data[$offset] = $value;
35
    }
36
37
    public function containsKey($key)
38
    {
39
        return array_key_exists($key, $this->data);
40
    }
41
42
    public function containsValue($value)
43
    {
44
        return in_array($value, $this->data, true);
45
    }
46
47
    public function keys()
48
    {
49
        return array_keys($this->data);
50
    }
51
52
    public function get($key, $defaultValue = null)
53
    {
54
        if (!$this->containsKey($key)) {
55
            return $defaultValue;
56
        }
57
58
        return $this->offsetGet($key);
59
    }
60
61
    public function put($key, $value)
62
    {
63
        $previousValue = $this->get($key);
64
        $this[$key] = $value;
65
66
        return $previousValue;
67
    }
68
69 View Code Duplication
    public function putIfAbsent($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $currentValue = $this->get($key);
72
73
        if ($currentValue === null) {
74
            $this[$key] = $value;
75
        }
76
77
        return $currentValue;
78
    }
79
80
    public function remove($key)
81
    {
82
        $previousValue = $this->get($key);
83
        unset($this[$key]);
84
85
        return $previousValue;
86
    }
87
88
    public function removeIf($key, $value)
89
    {
90
        if ($this->get($key) === $value) {
91
            unset($this[$key]);
92
93
            return true;
94
        }
95
96
        return false;
97
    }
98
99 View Code Duplication
    public function replace($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $currentValue = $this->get($key);
102
103
        if ($this->containsKey($key)) {
104
            $this[$key] = $value;
105
        }
106
107
        return $currentValue;
108
    }
109
110
    public function replaceIf($key, $oldValue, $newValue)
111
    {
112
        if ($this->get($key) === $oldValue) {
113
            $this[$key] = $newValue;
114
115
            return true;
116
        }
117
118
        return false;
119
    }
120
}
121