ConstMapLikeTrait   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 14.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 17
loc 120
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 17 17 4
A isEmpty() 0 4 1
A count() 0 4 1
A at() 0 6 1
A get() 0 8 2
A containsKey() 0 4 1
A contains() 0 4 1
A offsetExists() 0 4 2
A each() 0 8 2
A toArray() 0 13 3

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
namespace Collections\Traits;
4
5
use Collections\Enumerable;
6
7
trait ConstMapLikeTrait
8
{
9
    use CommonContainerMethodsTrait;
10
11
    /**
12
     * @var array
13
     */
14
    private $container;
15
16 View Code Duplication
    protected function init($it = null)
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...
17
    {
18
        if (null !== $it) {
19
            $this->validateTraversable($it);
20
21
            $coll = [];
22
            foreach ($it as $key => $value) {
23
                if (is_array($value)) {
24
                    $value = new static($value);
0 ignored issues
show
Unused Code introduced by
The call to ConstMapLikeTrait::__construct() has too many arguments starting with $value.

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...
25
                }
26
                $coll[$key] = $value;
27
            }
28
            $this->container = $coll;
29
        } else {
30
            $this->container = [];
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function isEmpty()
38
    {
39
        return $this->count() === 0;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function count()
46
    {
47
        return count($this->container);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function at($key)
54
    {
55
        $this->validateKeyDoesNotExists($key);
56
57
        return $this->container[$key];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get($index)
64
    {
65
        if ($this->containsKey($index)) {
66
            return $this->container[$index];
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function containsKey($key)
76
    {
77
        return array_key_exists($key, $this->container);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function contains($element)
84
    {
85
        return in_array($element, $this->container, true);
86
    }
87
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function offsetExists($offset)
93
    {
94
        return $this->containsKey($offset) && $this->at($offset) !== null;
95
    }
96
97
    /**
98
     * Returns an array containing the values from this VectorLike.
99
     */
100
    public function toArray()
101
    {
102
        $arr = [];
103
        foreach ($this as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\ConstMapLikeTrait> is not traversable.
Loading history...
104
            if ($value instanceof Enumerable) {
105
                $arr[$key] = $value->toArray();
106
            } else {
107
                $arr[$key] = $value;
108
            }
109
        }
110
111
        return $arr;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     * @return $this
117
     */
118
    public function each(callable $callable)
119
    {
120
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\ConstMapLikeTrait> is not traversable.
Loading history...
121
            $callable($v, $k);
122
        }
123
124
        return $this;
125
    }
126
}
127