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

VectorLikeTrait::splice()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\Exception\InvalidOperationException;
6
7
trait VectorLikeTrait
8
{
9
    use ConstVectorLikeTrait,
10
        CommonMutableContainerTrait;
11
12
    /**
13
     * identical to at, implemented for ArrayAccess
14
     */
15
    public function offsetGet($offset)
16
    {
17
        return $this->at($offset);
18
    }
19
20
    public function offsetSet($offset, $value)
21
    {
22
        if (is_null($offset)) {
23
            $this->add($value);
24
        } else {
25
            $this->set($offset, $value);
26
        }
27
    }
28
29
    public function offsetUnset($offset)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        throw InvalidOperationException::unsupportedUnset($this);
32
    }
33
34
    /**
35
     * Stores a value into the Vector with the specified key, overwriting the
36
     * previous value associated with the key. If the key is not present,
37
     * an exception is thrown. "$vec->set($k,$v)" is semantically equivalent
38
     * to "$vec[$k] = $v" (except that set() returns the Vector).
39
     *
40
     * @param $key
41
     * @param $value
42
     * @return $this
43
     */
44
    public function set($key, $value)
45
    {
46
        $this->validateKeyType($key);
47
        $this->container[$key] = $value;
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 View Code Duplication
    public function setAll($items)
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...
56
    {
57
        $this->validateTraversable($items);
58
59
        foreach ($items as $key => $item) {
60
            if (is_array($item)) {
61
                $item = new static($item);
0 ignored issues
show
Unused Code introduced by
The call to VectorLikeTrait::__construct() has too many arguments starting with $item.

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...
62
            }
63
            $this->set($key, $item);
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function add($item)
73
    {
74
        $this->container[] = $item;
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function removeKey($key)
83
    {
84
        $this->validateKeyType($key);
85
        $this->validateKeyBounds($key);
86
87
        array_splice($this->container, $key, 1);
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function splice($offset, $length = null)
96
    {
97
        if (!is_int($offset)) {
98
            throw new \InvalidArgumentException('Parameter offset must be an integer');
99
        }
100
101
        if (!is_null($length) && !is_int($length)) {
102
            throw new \InvalidArgumentException('Parameter len must be null or an integer');
103
        }
104
105
        $removed = is_null($length) ? array_splice($this->container, $offset) :
0 ignored issues
show
Unused Code introduced by
$removed is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
            array_splice($this->container, $offset, $len);
0 ignored issues
show
Bug introduced by
The variable $len does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
107
108
//        if (count($removed) > 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
//            $this->hacklib_expireAllIterators();
110
//        }
111
    }
112
}
113