Passed
Pull Request — master (#19)
by
unknown
03:59
created

KeyValuePair   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 37.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 26
loc 70
ccs 22
cts 22
cp 1
rs 10
wmc 13
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetExists() 0 4 1
B offsetGet() 12 12 5
B offsetSet() 14 14 5
A offsetUnset() 0 4 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
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\lib\Containers;
8
9
/**
10
 * Class Pair
11
 *
12
 * @package Zicht\Itertools\lib\Containers
13
 */
14
class KeyValuePair implements \ArrayAccess
15
{
16
    /** @var mixed */
17
    public $key;
18
19
    /** @var mixed */
20
    public $value;
21
22
    /**
23
     * Pair constructor.
24
     *
25
     * @param mixed $key
26
     * @param mixed $value
27
     */
28 9
    public function __construct($key = null, $value = null)
29
    {
30 9
        $this->key = $key;
31 9
        $this->value = $value;
32 9
    }
33
34
    /**
35
     * @{inheritDoc}
36
     */
37 4
    public function offsetExists($offset)
38
    {
39 4
        return in_array($offset, [0, 1, 'key', 'value']);
40
    }
41
42
    /**
43
     * @{inheritDoc}
44
     */
45 5 View Code Duplication
    public function offsetGet($offset)
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...
46
    {
47 5
        if ($offset === 0 || $offset === 'key') {
48 4
            return $this->key;
49
        }
50
51 5
        if ($offset === 1 || $offset === 'value') {
52 4
            return $this->value;
53
        }
54
55 1
        throw new \InvalidArgumentException('$OFFSET must be either 0, 1, "key", or "value"');
56
    }
57
58
    /**
59
     * @{inheritDoc}
60
     */
61 4 View Code Duplication
    public function offsetSet($offset, $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...
62
    {
63 4
        if ($offset === 0 || $offset === 'key') {
64 2
            $this->key = $value;
65 2
            return;
66
        }
67
68 4
        if ($offset === 1 || $offset === 'value') {
69 2
            $this->value = $value;
70 2
            return;
71
        }
72
73 2
        throw new \InvalidArgumentException('$OFFSET must be either 0, 1, "key", or "value"');
74
    }
75
76
    /**
77
     * @{inheritDoc}
78
     */
79 2
    public function offsetUnset($offset)
80
    {
81 2
        return $this->offsetSet($offset, null);
82
    }
83
}
84