Passed
Push — 1.x ( 3cc805...d95d50 )
by Ulises Jeremias
02:25
created

ImmutableArray::concat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 11
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\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 Immutable Array
30
 *
31
 * This provides special methods for quickly creating an immutable array,
32
 * either from any Traversable, or using a C-optimized fromArray() to directly
33
 * instantiate from. Also includes methods fundamental to functional
34
 * programming, e.g. map, filter, join, and sort.
35
 *
36
 * @package structures
37
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
38
 */
39
40
class ImmutableArray implements SequenceableInterface
41
{
42
    use Traits\Sequenceable;
43
44
    // The secondary flash array - fixed array
45
    private $sfa = null;
46
47
    /**
48
     * Create an immutable array
49
     *
50
     * @param Traversable $immute Data guaranteed to be immutable
51
     */
52
    private function __construct(Traversable $immute)
53
    {
54
        $this->sfa = $immute;
55
    }
56
57
    /**
58
     * Factory for building ImmutableArrays from any traversable
59
     *
60
     * @return ImmutableArray
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 ImmutableArray
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
    /**
95
     * Countable
96
     */
97
    public function count(): int
98
    {
99
        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

99
        return count(/** @scrutinizer ignore-type */ $this->sfa);
Loading history...
100
    }
101
102
    /**
103
     * Iterator
104
     */
105
    public function current()
106
    {
107
        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

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

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

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

122
        return $this->sfa->/** @scrutinizer ignore-call */ rewind();
Loading history...
123
    }
124
125
    public function valid()
126
    {
127
        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

127
        return $this->sfa->/** @scrutinizer ignore-call */ valid();
Loading history...
128
    }
129
130
    /**
131
     * ArrayAccess
132
     */
133
    public function offsetExists($offset): bool
134
    {
135
        return $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

135
        return $this->sfa->/** @scrutinizer ignore-call */ offsetExists($offset);
Loading history...
136
    }
137
138
    public function offsetGet($offset)
139
    {
140
        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

140
        return $this->sfa->/** @scrutinizer ignore-call */ offsetGet($offset);
Loading history...
141
    }
142
143
    public function offsetSet($offset, $value)
144
    {
145
        throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.');
146
    }
147
148
    public function offsetUnset($offset)
149
    {
150
        throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.');
151
    }
152
153
    public function clear()
154
    {
155
        throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.');
156
    }
157
}
158