Passed
Push — 1.x ( 5399bc...5b3b89 )
by Ulises Jeremias
02:34
created

FixedArray::arraySort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 9.4285
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\Collection as CollectionInterface;
12
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
13
use Mbh\Collection\CallbackHeap;
14
use Mbh\Iterator\SliceIterator;
15
use Mbh\Iterator\ConcatIterator;
16
use SplFixedArray;
17
use SplHeap;
18
use SplStack;
19
use LimitIterator;
20
use Iterator;
21
use ArrayAccess;
22
use Countable;
23
use CallbackFilterIterator;
24
use JsonSerializable;
25
use RuntimeException;
26
use Traversable;
27
use ReflectionClass;
28
29
/**
30
 * The Fixed Array
31
 *
32
 * A FixedArray is a sequence of values in a contiguous buffer that grows and
33
 * shrinks automatically. It’s the most efficient sequential structure because
34
 * a value’s index is a direct mapping to its index in the buffer, and the
35
 * growth factor isn't bound to a specific multiple or exponent.
36
 *
37
 * @package structures
38
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
39
 */
40
41
class FixedArray implements SequenceableInterface
42
{
43
    use Traits\Sequenceable;
44
45
    // The secondary flash array - fixed array
46
    protected $sfa = null;
47
48
    /**
49
     * Create an fixed array
50
     *
51
     * @param Traversable $fixed data guaranteed to be immutable
52
     */
53
    protected function __construct(Traversable $fixed)
54
    {
55
        $this->sfa = $fixed;
56
    }
57
58
    /**
59
     * Fallback behaviour to use the builtin array sort functions
60
     *
61
     * @param callable $callback The callback for comparison
62
     * @return SequenceableInterface
63
     */
64
    public function arraySort(callable $callback = null): SequenceableInterface
65
    {
66
        $array = $this->toArray();
67
68
        if ($callback) {
69
            usort($array, $callback);
70
        } else {
71
            sort($array);
72
        }
73
74
        $this->sfa = static::fromArray($array);
75
76
        return $this;
77
    }
78
79
    public function toArray(): array
80
    {
81
        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 Mbh\Iterator\SliceIterator or SplFixedArray or Guzzle\Service\Resource\ResourceIteratorInterface or Mbh\Iterator\ConcatIterator 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

81
        return $this->sfa->/** @scrutinizer ignore-call */ toArray();
Loading history...
82
    }
83
84
    private function validIndex(int $index)
85
    {
86
        return $index >= 0 && $index < count($this);
87
    }
88
89
    /**
90
     * Countable
91
     */
92
    public function count(): int
93
    {
94
        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

94
        return count(/** @scrutinizer ignore-type */ $this->sfa);
Loading history...
95
    }
96
97
    /**
98
     * Iterator
99
     */
100
    public function current()
101
    {
102
        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

102
        return $this->sfa->/** @scrutinizer ignore-call */ current();
Loading history...
103
    }
104
105
    public function key(): int
106
    {
107
        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

107
        return $this->sfa->/** @scrutinizer ignore-call */ key();
Loading history...
108
    }
109
110
    public function next()
111
    {
112
        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

112
        return $this->sfa->/** @scrutinizer ignore-call */ next();
Loading history...
113
    }
114
115
    public function rewind()
116
    {
117
        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

117
        return $this->sfa->/** @scrutinizer ignore-call */ rewind();
Loading history...
118
    }
119
120
    public function valid()
121
    {
122
        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

122
        return $this->sfa->/** @scrutinizer ignore-call */ valid();
Loading history...
123
    }
124
125
    /**
126
     * ArrayAccess
127
     */
128
    public function offsetExists($offset): bool
129
    {
130
        return is_integer($offset)
131
            && $this->validIndex($offset)
132
            && $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 Mbh\Iterator\SliceIterator or SplFixedArray or SplObjectStorage or Mbh\Iterator\ConcatIterator 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

132
            && $this->sfa->/** @scrutinizer ignore-call */ offsetExists($offset);
Loading history...
133
    }
134
135
    public function offsetGet($offset)
136
    {
137
        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 Mbh\Iterator\SliceIterator or SplFixedArray or SplObjectStorage or Mbh\Iterator\ConcatIterator 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

137
        return $this->sfa->/** @scrutinizer ignore-call */ offsetGet($offset);
Loading history...
138
    }
139
140
    public function offsetSet($offset, $value)
141
    {
142
        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...
143
            && $this->validIndex($offset)
144
            && $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 Mbh\Iterator\SliceIterator or SplFixedArray or SplObjectStorage or Mbh\Iterator\ConcatIterator 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

144
            && $this->sfa->/** @scrutinizer ignore-call */ offsetSet($offset, $value);
Loading history...
145
    }
146
147
    public function offsetUnset($offset)
148
    {
149
        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...
150
            && $this->validIndex($offset)
151
            && $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 Mbh\Iterator\SliceIterator or SplFixedArray or SplObjectStorage or Mbh\Iterator\ConcatIterator 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

151
            && $this->sfa->/** @scrutinizer ignore-call */ offsetUnset($offset);
Loading history...
152
    }
153
154
    public function clear()
155
    {
156
        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 Mbh\Iterator\SliceIterator or Imagick or ImagickPixelIterator or Mbh\Collection\FixedArray 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

156
        return $this->sfa->/** @scrutinizer ignore-call */ clear();
Loading history...
157
    }
158
}
159