Passed
Push — 1.x ( 5dbfd8...9a028c )
by Ulises Jeremias
02:21
created

ArrayAccess::offsetExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Mbh\Collection\Traits\Sequenceable;
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 OutOfRangeException;
13
14
trait ArrayAccess
15
{
16
    protected $sfa = null;
17
18
    /**
19
     * Create an fixed array
20
     *
21
     * @param Traversable $array data
22
     */
23
    protected function __construct(Traversable $array)
24
    {
25
        $this->sfa = $array;
26
        $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $this->/** @scrutinizer ignore-call */ 
27
               checkCapacity();
Loading history...
27
    }
28
29
    /**
30
     * ArrayAccess
31
     */
32
    public function offsetExists($offset): bool
33
    {
34
        return is_integer($offset)
35
            && $this->validIndex($offset)
0 ignored issues
show
Bug introduced by
It seems like validIndex() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            && $this->/** @scrutinizer ignore-call */ validIndex($offset)
Loading history...
36
            && $this->sfa->offsetExists($offset);
37
    }
38
39
    public function offsetGet($offset)
40
    {
41
        return $this->sfa->offsetGet($offset);
42
    }
43
44
    public function offsetSet($offset, $value)
45
    {
46
        if ($offset === null) {
47
            $this->push($value);
48
        } elseif (is_integer($offset)) {
49
            $this->set($offset, $value);
50
        }
51
    }
52
53
    public function offsetUnset($offset)
54
    {
55
        return is_integer($offset)
56
            && $this->remove($offset);
0 ignored issues
show
Bug introduced by
It seems like remove() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            && $this->/** @scrutinizer ignore-call */ remove($offset);
Loading history...
57
    }
58
59
    /**
60
     * Adds zero or more values to the end of the sequence.
61
     *
62
     * @param mixed ...$values
63
     */
64
    abstract public function push(...$values);
65
66
    /**
67
     * Replaces the value at a given index in the sequence with a new value.
68
     *
69
     * @param int   $index
70
     * @param mixed $value
71
     *
72
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
73
     */
74
    abstract public function set(int $index, $value);
75
}
76