ConstSetLikeTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 32.61 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 30
loc 92
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 17 17 4
A isEmpty() 0 4 1
A count() 0 4 1
A offsetGet() 0 4 1
A offsetExists() 0 4 1
A containsKey() 0 4 1
A contains() 0 4 1
A toArray() 13 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 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 Enumerable) {
90
                $arr[] = $v->toArray();
91
            } else {
92
                $arr[] = $v;
93
            }
94
        }
95
96
        return $arr;
97
    }
98
}
99