Passed
Push — 1.x ( f1b002...306f92 )
by Ulises Jeremias
02:31
created

Sequenceable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 156
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 3 1
A contains() 0 9 3
A unserialize() 0 4 1
1
<?php namespace Mbh\Collection\Traits;
2
3
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
4
use Traversable;
5
use ArrayAccess;
6
use Iterator;
7
use SplHeap;
8
use UnderflowException;
9
use OutOfRangeException;
10
11
/**
12
 * MBHFramework
13
 *
14
 * @link      https://github.com/MBHFramework/mbh-framework
15
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
16
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
17
 */
18
19
trait Sequenceable
20
{
21
    /**
22
     * Build from an array
23
     *
24
     * @return SequenceableInterface
25
     */
26
    abstract public static function fromArray(array $array);
27
28
    /**
29
     * Factory for building FixedArrays from any traversable
30
     *
31
     * @return SequenceableInterface
32
     */
33
    abstract public static function fromItems(Traversable $array);
34
35
    /**
36
     * Determines whether the sequence contains all of zero or more values.
37
     *
38
     * @param mixed ...$values
39
     *
40
     * @return bool true if at least one value was provided and the sequence
41
     *              contains all given values, false otherwise.
42
     */
43
    public function contains(...$values): bool
44
    {
45
        foreach ($values as &$value) {
46
            if ($this->search($value) !== null) {
47
                return false;
48
            }
49
        }
50
51
        return true;
52
    }
53
54
    /**
55
     * Returns a shallow copy of the collection.
56
     *
57
     * @return Collection a copy of the collection.
58
     */
59
    public function copy()
60
    {
61
        return static::fromArray($this->toArray());
0 ignored issues
show
Bug introduced by
It seems like toArray() 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

61
        return static::fromArray($this->/** @scrutinizer ignore-call */ toArray());
Loading history...
62
    }
63
64
    /**
65
     * Returns the first value in the sequence.
66
     *
67
     * @return mixed
68
     *
69
     * @throws UnderflowException if the sequence is empty.
70
     */
71
    abstract public function first();
72
73
    /**
74
     * Returns the value at a given index (position) in the sequence.
75
     *
76
     * @param int $index
77
     *
78
     * @return mixed
79
     *
80
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
81
     */
82
    abstract public function get(int $index);
83
84
    /**
85
     * Inserts zero or more values at a given index.
86
     *
87
     * Each value after the index will be moved one position to the right.
88
     * Values may be inserted at an index equal to the size of the sequence.
89
     *
90
     * @param int   $index
91
     * @param mixed ...$values
92
     *
93
     * @throws OutOfRangeException if the index is not in the range [0, n]
94
     */
95
    abstract public function insert(int $index, ...$values);
96
97
    /**
98
     * Returns the last value in the sequence.
99
     *
100
     * @return mixed
101
     *
102
     * @throws UnderflowException if the sequence is empty.
103
     */
104
    abstract public function last();
105
106
    /**
107
     * Removes the last value in the sequence, and returns it.
108
     *
109
     * @return mixed what was the last value in the sequence.
110
     *
111
     * @throws UnderflowException if the sequence is empty.
112
     */
113
    abstract public function pop();
114
115
    /**
116
     * Adds zero or more values to the end of the sequence.
117
     *
118
     * @param mixed ...$values
119
     */
120
    abstract public function push(...$values);
121
122
    /**
123
     * Removes and returns the value at a given index in the sequence.
124
     *
125
     * @param int $index this index to remove.
126
     *
127
     * @return mixed the removed value.
128
     *
129
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
130
     */
131
    abstract public function remove(int $index);
132
133
    /**
134
     * Find a single element key
135
     *
136
     * @param mixed $value The value to search
137
     * @return mixed The key for the element we found
138
     */
139
    abstract public function search($value);
140
141
    /**
142
     * Replaces the value at a given index in the sequence with a new value.
143
     *
144
     * @param int   $index
145
     * @param mixed $value
146
     *
147
     * @throws OutOfRangeException if the index is not in the range [0, size-1]
148
     */
149
    abstract public function set(int $index, $value);
150
151
    /**
152
     * Removes and returns the first value in the sequence.
153
     *
154
     * @return mixed what was the first value in the sequence.
155
     *
156
     * @throws UnderflowException if the sequence was empty.
157
     */
158
    abstract public function shift();
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function unserialize($values)
164
    {
165
        $values = unserialize($values);
166
        $this->setValues(SplFixedArray::fromArray($values));
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\SplFixedArray was not found. Did you mean SplFixedArray? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
It seems like setValues() 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

166
        $this->/** @scrutinizer ignore-call */ 
167
               setValues(SplFixedArray::fromArray($values));
Loading history...
167
    }
168
169
    /**
170
     * Adds zero or more values to the front of the sequence.
171
     *
172
     * @param mixed ...$values
173
     */
174
    abstract public function unshift(...$values);
175
}
176