Passed
Push — 1.x ( 7b5b09...f62522 )
by Ulises Jeremias
02:17
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
    /**
40
     * @inheritDoc
41
     */
42 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...
43
    {
44
        $count = count($this);
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Functional 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

44
        $count = count(/** @scrutinizer ignore-type */ $this);
Loading history...
45
        $sfa = new SplFixedArray($count);
46
47
        for ($i = 0; $i < $count; $i++) {
48
            $sfa[$i] = $callback($this[$i], $i, $this);
49
        }
50
51
        return new static($sfa);
0 ignored issues
show
Unused Code introduced by
The call to Mbh\Collection\Traits\Functional::__construct() has too many arguments starting with $sfa. ( Ignorable by Annotation )

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

51
        return /** @scrutinizer ignore-call */ new static($sfa);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function walk(callable $callback)
58
    {
59
        foreach ($this as $i => $elem) {
60
            $callback($elem, $i, $this);
61
        }
62
63
        return $this;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 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...
70
    {
71
        $count = count($this);
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Functional 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

71
        $count = count(/** @scrutinizer ignore-type */ $this);
Loading history...
72
        $sfa = new SplFixedArray($count);
73
        $newCount = 0;
74
75
        foreach ($this as $elem) {
76
            if ($callback($elem)) {
77
                $sfa[$newCount++] = $elem;
78
            }
79
        }
80
81
        $sfa->setSize($newCount);
82
        return new static($sfa);
0 ignored issues
show
Unused Code introduced by
The call to Mbh\Collection\Traits\Functional::__construct() has too many arguments starting with $sfa. ( Ignorable by Annotation )

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

82
        return /** @scrutinizer ignore-call */ new static($sfa);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function reduce(callable $callback, $accumulator = null)
89
    {
90
        foreach ($this as $i => $elem) {
91
            $accumulator = $callback($accumulator, $elem, $i, $this);
92
        }
93
94
        return $accumulator;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function join(string $token = ',', string $secondToken = null): string
101
    {
102
        $str = "";
103
        if ($secondToken) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $secondToken of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
104
            foreach ($this as $i => $elem) {
105
                $str .= $token . (string) $elem . $secondToken;
106
            }
107
        } else {
108
            $this->rewind();
0 ignored issues
show
Bug introduced by
It seems like rewind() 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

108
            $this->/** @scrutinizer ignore-call */ 
109
                   rewind();
Loading history...
109
            while ($this->valid()) {
0 ignored issues
show
Bug introduced by
It seems like valid() 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

109
            while ($this->/** @scrutinizer ignore-call */ valid()) {
Loading history...
110
                $str .= (string) $this->current();
0 ignored issues
show
Bug introduced by
It seems like current() 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

110
                $str .= (string) $this->/** @scrutinizer ignore-call */ current();
Loading history...
111
                $this->next();
0 ignored issues
show
Bug introduced by
It seems like next() 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

111
                $this->/** @scrutinizer ignore-call */ 
112
                       next();
Loading history...
112
                if ($this->valid()) {
113
                    $str .= $token;
114
                }
115
            }
116
        }
117
118
        return $str;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function slice(int $begin = 0, int $end = null)
125
    {
126
        $it = new SliceIterator($this, $begin, $end);
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Functional is incompatible with the type Iterator expected by parameter $iterator of Mbh\Iterator\SliceIterator::__construct(). ( Ignorable by Annotation )

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

126
        $it = new SliceIterator(/** @scrutinizer ignore-type */ $this, $begin, $end);
Loading history...
127
        return new static($it);
0 ignored issues
show
Unused Code introduced by
The call to Mbh\Collection\Traits\Functional::__construct() has too many arguments starting with $it. ( 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 /** @scrutinizer ignore-call */ new static($it);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function concat()
134
    {
135
        $args = func_get_args();
136
        array_unshift($args, $this);
137
138
        // Concat this iterator, and variadic args
139
        $class = new ReflectionClass('Mbh\Iterator\ConcatIterator');
140
        $concatIt = $class->newInstanceArgs($args);
141
142
        // Create as new immutable's iterator
143
        return new static($concatIt);
0 ignored issues
show
Unused Code introduced by
The call to Mbh\Collection\Traits\Functional::__construct() has too many arguments starting with $concatIt. ( Ignorable by Annotation )

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

143
        return /** @scrutinizer ignore-call */ new static($concatIt);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function find(callable $callback)
150
    {
151
        foreach ($this as $i => $elem) {
152
            if ($callback($elem, $i, $this)) {
153
                return $elem;
154
            }
155
        }
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function sort(callable $callback = null)
162
    {
163
        if ($callback) {
164
            return $this->mergeSort($callback);
165
        }
166
167
        return $this->arraySort();
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function sorted(callable $callback = null)
174
    {
175
        $copy = FixedArray::fromItems($this);
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Functional is incompatible with the type Traversable expected by parameter $array of Mbh\Collection\FixedArray::fromItems(). ( Ignorable by Annotation )

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

175
        $copy = FixedArray::fromItems(/** @scrutinizer ignore-type */ $this);
Loading history...
176
177
        if ($callback) {
178
            $copy->mergeSort($callback);
179
        }
180
181
        $copy->arraySort();
182
183
        return static::fromItems($copy);
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    public function heapSorted(SplHeap $heap)
190
    {
191
        return $this->copy()->heapSort($heap);
0 ignored issues
show
Bug introduced by
It seems like copy() 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

191
        return $this->/** @scrutinizer ignore-call */ copy()->heapSort($heap);
Loading history...
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function heapSort(SplHeap $heap)
198
    {
199
        foreach ($this as $item) {
200
            $heap->insert($item);
201
        }
202
203
        $this->setTraversable(static::fromItems($heap));
204
205
        return $this;
206
    }
207
}
208