Completed
Pull Request — master (#16)
by
unknown
04:54
created

MapLikeTrait::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\Pair;
6
7
trait MapLikeTrait
8
{
9
    use ConstMapLikeTrait,
10
        CommonMutableContainerTrait;
11
12
    /**
13
     * identical to at, implemented for ArrayAccess
14
     */
15
    public function offsetGet($offset)
16
    {
17
        return $this->at($offset);
18
    }
19
20
    public function offsetSet($offset, $value)
21
    {
22
        if (is_null($offset)) {
23
            $this->add($value);
24
        } else {
25
            $this->set($offset, $value);
26
        }
27
    }
28
29
    public function offsetUnset($offset)
30
    {
31
        $this->removeKey($offset);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function set($key, $value)
38
    {
39
        $this->container[$key] = $value;
40
41
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 View Code Duplication
    public function setAll($items)
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...
48
    {
49
        $this->validateTraversable($items);
50
51
        foreach ($items as $key => $item) {
52
            if (is_array($item)) {
53
                $item = new static($item);
0 ignored issues
show
Unused Code introduced by
The call to MapLikeTrait::__construct() has too many arguments starting with $item.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
            }
55
            $this->set($key, $item);
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function add($pair)
65
    {
66
        if (!($pair instanceof Pair)) {
67
            throw new \InvalidArgumentException('Parameter must be an instance of Pair');
68
        }
69
70
        list($key, $value) = $pair;
71
72
        $this->validateKeyExists($key);
73
        $this->set($key, $value);
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function removeKey($key)
82
    {
83
        $this->validateKeyDoesNotExists($key);
84
85
        unset($this->container[$key]);
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function remove($element)
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...
94
    {
95
        $key = array_search($element, $this->container);
96
97
        if (false === $key) {
98
            throw new \OutOfBoundsException('No element found in the collection');
99
        }
100
101
        $this->removeKey($key);
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     * @return $this
109
     */
110
    public function each(callable $callable)
111
    {
112
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\MapLikeTrait> is not traversable.
Loading history...
113
            $callable($v, $k);
114
        }
115
116
        return $this;
117
    }
118
}
119