Passed
Push — 1.x ( 36b118...cb0b9f )
by Ulises Jeremias
02:31
created

ArrayAccess   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 8 2
A offsetUnset() 0 8 2
A offsetExists() 0 4 2
A offsetGet() 0 4 1
1
<?php namespace Mbh\Collection\Traits\Sequenceable\LinkedList;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Traversable;
12
use OutOfBoundsException;
13
use OutOfRangeException;
14
15
trait ArrayAccess
16
{
17
    /**
18
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
19
     * @param int $offset
20
     * @return boolean
21
     */
22
    public function offsetExists($offset)
23
    {
24
        $index = $this->intGuard($offset);
25
        return $index >= 0 && $index < $this->count();
26
    }
27
28
    /**
29
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
30
     * @param int $offset
31
     * @return mixed
32
     * @throws OutOfBoundsException
33
     */
34
    public function offsetGet($offset)
35
    {
36
        $n = $this->guardedSeek($offset, __METHOD__);
37
        return $n->value();
38
    }
39
40
    /**
41
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
42
     * @param int|null $offset
43
     * @param mixed $value
44
     * @return void
45
     * @throws OutOfBoundsException
46
     */
47
    public function offsetSet($offset, $value)
48
    {
49
        if ($offset === null) {
50
            $this->push($value);
51
            return;
52
        }
53
        $n = $this->guardedSeek($offset, __METHOD__);
54
        $n->setValue($value);
55
    }
56
57
    /**
58
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
59
     * @param int $offset
60
     * @return void
61
     */
62
    public function offsetUnset($offset)
63
    {
64
        $index = $this->intGuard($offset);
65
        if ($this->offsetExists($index)) {
66
            $n = $this->seekTo($index);
67
            $this->removeNode($n);
68
            $this->current = $n->prev();
0 ignored issues
show
Bug Best Practice introduced by
The property current does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
            $this->offset--;
0 ignored issues
show
Bug Best Practice introduced by
The property offset does not exist on Mbh\Collection\Traits\Se...\LinkedList\ArrayAccess. Did you maybe forget to declare it?
Loading history...
70
        }
71
    }
72
73
    /**
74
     * @link http://php.net/manual/en/countable.count.php
75
     * @return int
76
     */
77
    abstract public function count(): int;
78
79
    /**
80
     * @param mixed $i
81
     * @return int
82
     * @throws Exception
83
     */
84
    abstract protected function intGuard($i);
85
86
    abstract protected function guardedSeek($index, $method);
87
88
    abstract public function push(...$values);
89
90
    abstract protected function removeNode(LinkedNode $n);
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\Se...e\LinkedList\LinkedNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
92
    /**
93
     * @param $offset
94
     * @return LinkedDataNode
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\Se...nkedList\LinkedDataNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
95
     */
96
    abstract protected function seekTo($offset);
97
}
98