Passed
Push — 1.x ( cf963f...5ef8bb )
by Ulises Jeremias
03:00
created

FixedArray::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection;
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 Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
12
use Mbh\Collection\CallbackHeap;
13
use Mbh\Iterator\SliceIterator;
14
use Mbh\Iterator\ConcatIterator;
15
use SplFixedArray;
16
use SplHeap;
17
use SplStack;
18
use LimitIterator;
19
use Iterator;
20
use ArrayAccess;
21
use Countable;
22
use CallbackFilterIterator;
23
use JsonSerializable;
24
use RuntimeException;
25
use Traversable;
26
use ReflectionClass;
27
28
/**
29
 * The Fixed Array
30
 *
31
 * A FixedArray is a sequence of values in a contiguous buffer that grows and
32
 * shrinks automatically. It’s the most efficient sequential structure because
33
 * a value’s index is a direct mapping to its index in the buffer, and the
34
 * growth factor isn't bound to a specific multiple or exponent.
35
 *
36
 * @package structures
37
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
38
 */
39
40
class FixedArray implements SequenceableInterface
41
{
42
    use Traits\Sequenceable;
43
44
    // The secondary flash array - fixed array
45
    protected $sfa = null;
46
47
    /**
48
     * Create an immutable array
49
     *
50
     * @param Traversable $immute data guaranteed to be immutable
51
     */
52
    protected function __construct(Traversable $immute)
53
    {
54
        $this->sfa = $immute;
55
    }
56
57
    /**
58
     * Factory for building FixedArrays from any traversable
59
     *
60
     * @return FixedArray
61
     */
62
    public static function fromItems(Traversable $array): self
63
    {
64
        // We can only do it this way if we can count it
65
        if ($array instanceof Countable) {
66
            $sfa = new SplFixedArray(count($array));
67
68
            foreach ($array as $i => $elem) {
69
                $sfa[$i] = $elem;
70
            }
71
72
            return new static($sfa);
73
        }
74
75
        // If we can't count it, it's simplest to iterate into an array first
76
        return static::fromArray(iterator_to_array($array));
77
    }
78
79
    /**
80
     * Build from an array
81
     *
82
     * @return FixedArray
83
     */
84
    public static function fromArray(array $array): self
85
    {
86
        return new static(SplFixedArray::fromArray($array));
87
    }
88
89
    public function toArray(): array
90
    {
91
        return $this->sfa->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as MongoDB\Driver\Cursor or Guzzle\Http\Message\Header\HeaderCollection or Guzzle\Http\Message\Header\HeaderInterface or http\QueryString or Guzzle\Common\Event or Guzzle\Common\Collection or Mbh\Collection\Interfaces\Collection or SplFixedArray or Guzzle\Service\Resource\ResourceIteratorInterface or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\ConcatIterator or Mbh\Iterator\SliceIterator or Mbh\Collection\CallbackHeap. ( Ignorable by Annotation )

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

91
        return $this->sfa->/** @scrutinizer ignore-call */ toArray();
Loading history...
92
    }
93
94
    private function validIndex(int $index)
95
    {
96
        return $index >= 0 && $index < count($this);
97
    }
98
99
    /**
100
     * Countable
101
     */
102
    public function count(): int
103
    {
104
        return count($this->sfa);
0 ignored issues
show
Bug introduced by
$this->sfa of type Traversable is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

104
        return count(/** @scrutinizer ignore-type */ $this->sfa);
Loading history...
105
    }
106
107
    /**
108
     * Iterator
109
     */
110
    public function current()
111
    {
112
        return $this->sfa->current();
0 ignored issues
show
Bug introduced by
The method current() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as IntlCodePointBreakIterator or IntlRuleBasedBreakIterator or Iterator or IntlBreakIterator or MongoGridFSCursor or SimpleXMLIterator. ( Ignorable by Annotation )

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

112
        return $this->sfa->/** @scrutinizer ignore-call */ current();
Loading history...
113
    }
114
115
    public function key(): int
116
    {
117
        return $this->sfa->key();
0 ignored issues
show
Bug introduced by
The method key() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Iterator or MongoGridFSCursor or SimpleXMLIterator. ( Ignorable by Annotation )

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

117
        return $this->sfa->/** @scrutinizer ignore-call */ key();
Loading history...
118
    }
119
120
    public function next()
121
    {
122
        return $this->sfa->next();
0 ignored issues
show
Bug introduced by
The method next() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as IntlCodePointBreakIterator or IntlRuleBasedBreakIterator or Iterator or IntlBreakIterator or MongoGridFSCursor or SimpleXMLIterator. ( Ignorable by Annotation )

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

122
        return $this->sfa->/** @scrutinizer ignore-call */ next();
Loading history...
123
    }
124
125
    public function rewind()
126
    {
127
        return $this->sfa->rewind();
0 ignored issues
show
Bug introduced by
The method rewind() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Iterator or MongoGridFSCursor or SimpleXMLIterator. ( Ignorable by Annotation )

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

127
        return $this->sfa->/** @scrutinizer ignore-call */ rewind();
Loading history...
128
    }
129
130
    public function valid()
131
    {
132
        return $this->sfa->valid();
0 ignored issues
show
Bug introduced by
The method valid() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Iterator or MongoGridFSCursor or SimpleXMLIterator. ( Ignorable by Annotation )

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

132
        return $this->sfa->/** @scrutinizer ignore-call */ valid();
Loading history...
133
    }
134
135
    /**
136
     * ArrayAccess
137
     */
138
    public function offsetExists($offset): bool
139
    {
140
        return is_integer($offset)
141
            && $this->validIndex($offset)
142
            && $this->sfa->offsetExists($offset);
0 ignored issues
show
Bug introduced by
The method offsetExists() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Threaded or SimpleXMLElement or Thread or Worker or Stackable or Guzzle\Http\Message\Header\HeaderCollection or http\QueryString or Symfony\Component\EventDispatcher\GenericEvent or Guzzle\Common\Event or Guzzle\Common\Collection or ArrayObject or SplDoublyLinkedList or SplFixedArray or SplObjectStorage or Mbh\Collection\Interfaces\Sequenceable or CachingIterator or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\ConcatIterator or Mbh\Iterator\SliceIterator or Phar or ArrayIterator or Phar or Phar or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

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

142
            && $this->sfa->/** @scrutinizer ignore-call */ offsetExists($offset);
Loading history...
143
    }
144
145
    public function offsetGet($offset)
146
    {
147
        return $this->sfa->offsetGet($offset);
0 ignored issues
show
Bug introduced by
The method offsetGet() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Threaded or SimpleXMLElement or Thread or Worker or Stackable or Guzzle\Http\Message\Header\HeaderCollection or http\QueryString or Symfony\Component\EventDispatcher\GenericEvent or Guzzle\Common\Event or Guzzle\Common\Collection or ArrayObject or SplDoublyLinkedList or SplFixedArray or SplObjectStorage or Mbh\Collection\Interfaces\Sequenceable or CachingIterator or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\ConcatIterator or Mbh\Iterator\SliceIterator or Phar or ArrayIterator or Phar or Phar or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

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

147
        return $this->sfa->/** @scrutinizer ignore-call */ offsetGet($offset);
Loading history...
148
    }
149
150
    public function offsetSet($offset, $value)
151
    {
152
        return is_integer($offset)
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_integer($offse...setSet($offset, $value) returns the type boolean which is incompatible with the return type mandated by ArrayAccess::offsetSet() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
153
            && $this->validIndex($offset)
154
            && $this->sfa->offsetSet($offset, $value);
0 ignored issues
show
Bug introduced by
The method offsetSet() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Threaded or SimpleXMLElement or Thread or Worker or Stackable or Guzzle\Http\Message\Header\HeaderCollection or http\QueryString or Symfony\Component\EventDispatcher\GenericEvent or Guzzle\Common\Event or Guzzle\Common\Collection or ArrayObject or SplDoublyLinkedList or SplFixedArray or SplObjectStorage or Mbh\Collection\Interfaces\Sequenceable or CachingIterator or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\ConcatIterator or Mbh\Iterator\SliceIterator or Phar or ArrayIterator or Phar or Phar or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

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

154
            && $this->sfa->/** @scrutinizer ignore-call */ offsetSet($offset, $value);
Loading history...
155
    }
156
157
    public function offsetUnset($offset)
158
    {
159
        return is_integer($offset)
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_integer($offse...a->offsetUnset($offset) returns the type boolean which is incompatible with the return type mandated by ArrayAccess::offsetUnset() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
160
            && $this->validIndex($offset)
161
            && $this->sfa->offsetUnset($offset);
0 ignored issues
show
Bug introduced by
The method offsetUnset() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Threaded or SimpleXMLElement or Thread or Worker or Stackable or Guzzle\Http\Message\Header\HeaderCollection or http\QueryString or Symfony\Component\EventDispatcher\GenericEvent or Guzzle\Common\Event or Guzzle\Common\Collection or ArrayObject or SplDoublyLinkedList or SplFixedArray or SplObjectStorage or Mbh\Collection\Interfaces\Sequenceable or CachingIterator or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\ConcatIterator or Mbh\Iterator\SliceIterator or Phar or ArrayIterator or Phar or Phar or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

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

161
            && $this->sfa->/** @scrutinizer ignore-call */ offsetUnset($offset);
Loading history...
162
    }
163
164
    public function clear()
165
    {
166
        return $this->sfa->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Guzzle\Plugin\History\HistoryPlugin or Guzzle\Http\Message\Header\HeaderCollection or Guzzle\Common\Collection or Imagick or ImagickPixelIterator or Mbh\Collection\FixedArray or Mbh\Iterator\SliceIterator or Guzzle\Iterator\MethodProxyIterator or Mbh\Iterator\SliceIterator. ( Ignorable by Annotation )

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

166
        return $this->sfa->/** @scrutinizer ignore-call */ clear();
Loading history...
167
    }
168
}
169