Completed
Push — master ( 747a6a...c6c655 )
by Ítalo
02:55
created

ConstSetLikeTrait::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\Iterable;
6
7
trait ConstSetLikeTrait
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 ConstSetLikeTrait::__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
     * identical to at, implemented for ArrayAccess
52
     */
53
    public function offsetGet($offset)
54
    {
55
        return $this->container[$offset];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function offsetExists($offset)
62
    {
63
        return $this->containsKey($offset);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function containsKey($key)
70
    {
71
        return array_key_exists($key, $this->container);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function contains($item)
78
    {
79
        return in_array($item, $this->container, true);
80
    }
81
82
    /**
83
     * Returns an array containing the values from this VectorLike.
84
     */
85 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...
86
    {
87
        $arr = [];
88
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\ConstSetLikeTrait> is not traversable.
Loading history...
89
            if ($v instanceof Iterable) {
90
                $arr[] = $v->toArray();
91
            } else {
92
                $arr[] = $v;
93
            }
94
        }
95
96
        return $arr;
97
    }
98
}
99