Test Failed
Push — 1.x ( 521d03...1324ca )
by Ulises Jeremias
18:56 queued 03:55
created

Functional::heapSorted()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection\Traits;
2
3
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
4
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
5
use Mbh\Collection\FixedArray;
6
use Mbh\Collection\CallbackHeap;
7
use Mbh\Iterator\SliceIterator;
8
use Mbh\Iterator\ConcatIterator;
9
use SplFixedArray;
10
use SplHeap;
11
use SplStack;
12
use LimitIterator;
13
use Iterator;
14
use ArrayAccess;
15
use Countable;
16
use CallbackFilterIterator;
17
use JsonSerializable;
18
use RuntimeException;
19
use Traversable;
20
use ReflectionClass;
21
use UnderflowException;
22
use OutOfRangeException;
23
24
/**
25
 * MBHFramework
26
 *
27
 * @link      https://github.com/MBHFramework/mbh-framework
28
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
29
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
30
 */
31
32
trait Functional
33
{
34
    use Sort {
35
        Sort::heapSort as heapSortWithCallback;
36
        Sort::heapSorted as heapSortedWithCallback;
37
    }
38
39
    protected function getSplFixedArrayAndSize()
40
    {
41
        $count = $this->count();
42
        return [new SplFixedArray($count), $count];
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function concat(...$args)
49
    {
50
        array_unshift($args, $this);
51
52
        // Concat this iterator, and variadic args
53
        $class = new ReflectionClass('Mbh\Iterator\ConcatIterator');
54
        $concatIt = $class->newInstanceArgs($args);
55
56
        // Create as new immutable's iterator
57
        return static::fromArray($concatIt->toArray());
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function find(callable $callback)
64
    {
65
        foreach ($this as $i => $elem) {
66
            if ($callback($elem, $i, $this)) {
67
                return $elem;
68
            }
69
        }
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 View Code Duplication
    public function filter(callable $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
78
79
        $newCount = 0;
80
        foreach ($this as $elem) {
81
            if ($callback($elem)) {
82
                $sfa[$newCount++] = $elem;
83
            }
84
        }
85
86
        $sfa->setSize($newCount);
87
        return new static($sfa);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function heapSorted(SplHeap $heap)
94
    {
95
        return $this->copy()->heapSort($heap);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function heapSort(SplHeap $heap)
102
    {
103
        foreach ($this as $item) {
104
            $heap->insert($item);
105
        }
106
107
        $this->setSfa(static::fromItems($heap));
108
109
        return $this;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function join(string $token = ',', string $secondToken = null): string
116
    {
117
        $str = "";
118
        if ($secondToken !== null) {
119
            foreach ($this as $i => $elem) {
120
                $str .= $token . (string) $elem . $secondToken;
121
            }
122
        } else {
123
            $this->rewind();
124
            while ($this->valid()) {
125
                $str .= (string) $this->current();
126
                $this->next();
127
                if ($this->valid()) {
128
                    $str .= $token;
129
                }
130
            }
131
        }
132
133
        return $str;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139 View Code Duplication
    public function map(callable $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
142
143
        for ($i = 0; $i < $count; $i++) {
144
            $sfa[$i] = $callback($this[$i], $i, $this);
145
        }
146
147
        return new static($sfa);
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function reduce(callable $callback, $accumulator = null)
154
    {
155
        foreach ($this as $i => $elem) {
156
            $accumulator = $callback($accumulator, $elem, $i, $this);
157
        }
158
159
        return $accumulator;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function search($value)
166
    {
167
        foreach ($this as $i => $elem) {
168
            if ($value === $elem) {
169
                return $i;
170
            }
171
        }
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function slice(int $begin = 0, int $end = null)
178
    {
179
        $it = new SliceIterator($this->getSfa(), $begin, $end);
180
        return static::fromArray($it->toArray());
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function splice(int $begin = 0, int $end = null, mixed $replacement = [])
0 ignored issues
show
Unused Code introduced by
The parameter $replacement is not used and could be removed. ( Ignorable by Annotation )

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

186
    public function splice(int $begin = 0, int $end = null, /** @scrutinizer ignore-unused */ mixed $replacement = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
The type Mbh\Collection\Traits\mixed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
187
    {
188
        $slice = $this->slice($begin, $end);
0 ignored issues
show
Unused Code introduced by
The assignment to $slice is dead and can be removed.
Loading history...
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function sort(callable $callback = null)
195
    {
196
        if ($callback) {
197
            return $this->mergeSort($callback);
198
        }
199
200
        return $this->arraySort();
201
    }
202
203
    /**
204
     * @inheritDoc
205
     */
206
    public function sorted(callable $callback = null)
207
    {
208
        $copy = FixedArray::fromItems($this->copy());
209
210
        if ($callback) {
211
            $copy->mergeSort($callback);
212
        }
213
214
        $copy->arraySort();
215
216
        return static::fromItems($copy);
217
    }
218
219
    /**
220
     * @inheritDoc
221
     */
222
    public function walk(callable $callback)
223
    {
224
        foreach ($this as $i => $elem) {
225
            $callback($elem, $i, $this);
226
        }
227
228
        return $this;
229
    }
230
231
    abstract public static function fromItems(Traversable $array);
232
233
    abstract protected function getSfa(): Traversable;
234
235
    abstract public function count(): int;
236
237
    abstract public function current();
238
239
    abstract public function next();
240
241
    abstract public function rewind();
242
243
    abstract public function valid();
244
}
245