ConstMapLikeTrait::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
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