ConstVectorLikeTrait::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\Enumerable;
6
7
trait ConstVectorLikeTrait
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 $value) {
23
                if (is_array($value)) {
24
                    $value = new static($value);
0 ignored issues
show
Unused Code introduced by
The call to ConstVectorLikeTrait::__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[] = $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->validateKeyType($key);
56
        $this->validateKeyBounds($key);
57
58
        return $this->container[$key];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function get($index)
65
    {
66
        $this->validateKeyType($index);
67
68
        return $this->container[$index];
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function containsKey($key)
75
    {
76
        $this->validateKeyType($key);
77
78
        return $key >= 0 && $key < $this->count();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function offsetExists($offset)
85
    {
86
        return $this->containsKey($offset) && $this->at($offset) !== null;
87
    }
88
89
    /**
90
     * Returns an array containing the values from this VectorLike.
91
     */
92 View Code Duplication
    public function toArray()
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...
93
    {
94
        $arr = [];
95
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\ConstVectorLikeTrait> is not traversable.
Loading history...
96
            if ($v instanceof Enumerable) {
97
                $arr[] = $v->toArray();
98
            } else {
99
                $arr[] = $v;
100
            }
101
        }
102
103
        return $arr;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     * @return $this
109
     */
110
    public function each(callable $callable)
111
    {
112
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\ConstVectorLikeTrait> is not traversable.
Loading history...
113
            $callable($v);
114
        }
115
116
        return $this;
117
    }
118
}
119