Passed
Push — 1.x ( 542d35...8ea17c )
by Ulises Jeremias
02:20
created

Functional   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 189
Duplicated Lines 12.7 %

Importance

Changes 0
Metric Value
dl 24
loc 189
rs 10
c 0
b 0
f 0
wmc 27

13 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 9 9 2
A heapSort() 0 9 2
A heapSorted() 0 3 1
A concat() 0 11 1
B join() 0 19 5
A walk() 0 7 2
A filter() 13 13 3
A getSplFixedArrayAndSize() 0 4 1
A find() 0 5 3
A sorted() 0 11 2
A reduce() 0 7 2
A sort() 0 7 2
A slice() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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...
49
    {
50
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
51
52
        for ($i = 0; $i < $count; $i++) {
53
            $sfa[$i] = $callback($this[$i], $i, $this);
54
        }
55
56
        return new static($sfa);
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function walk(callable $callback)
63
    {
64
        foreach ($this as $i => $elem) {
65
            $callback($elem, $i, $this);
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 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...
75
    {
76
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
77
78
        $newCount = 0;
79
        foreach ($this as $elem) {
80
            if ($callback($elem)) {
81
                $sfa[$newCount++] = $elem;
82
            }
83
        }
84
85
        $sfa->setSize($newCount);
86
        return new static($sfa);
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function reduce(callable $callback, $accumulator = null)
93
    {
94
        foreach ($this as $i => $elem) {
95
            $accumulator = $callback($accumulator, $elem, $i, $this);
96
        }
97
98
        return $accumulator;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function join(string $token = ',', string $secondToken = null): string
105
    {
106
        $str = "";
107
        if ($secondToken !== null) {
108
            foreach ($this as $i => $elem) {
109
                $str .= $token . (string) $elem . $secondToken;
110
            }
111
        } else {
112
            $this->rewind();
113
            while ($this->valid()) {
114
                $str .= (string) $this->current();
115
                $this->next();
116
                if ($this->valid()) {
117
                    $str .= $token;
118
                }
119
            }
120
        }
121
122
        return $str;
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function slice(int $begin = 0, int $end = null)
129
    {
130
        $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

130
        $it = new SliceIterator(/** @scrutinizer ignore-type */ $this, $begin, $end);
Loading history...
131
        return new static($it);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function concat()
138
    {
139
        $args = func_get_args();
140
        array_unshift($args, $this);
141
142
        // Concat this iterator, and variadic args
143
        $class = new ReflectionClass('Mbh\Iterator\ConcatIterator');
144
        $concatIt = $class->newInstanceArgs($args);
145
146
        // Create as new immutable's iterator
147
        return new static($concatIt);
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function find(callable $callback)
154
    {
155
        foreach ($this as $i => $elem) {
156
            if ($callback($elem, $i, $this)) {
157
                return $elem;
158
            }
159
        }
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function sort(callable $callback = null)
166
    {
167
        if ($callback) {
168
            return $this->mergeSort($callback);
169
        }
170
171
        return $this->arraySort();
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function sorted(callable $callback = null)
178
    {
179
        $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

179
        $copy = FixedArray::fromItems(/** @scrutinizer ignore-type */ $this);
Loading history...
180
181
        if ($callback) {
182
            $copy->mergeSort($callback);
183
        }
184
185
        $copy->arraySort();
186
187
        return static::fromItems($copy);
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function heapSorted(SplHeap $heap)
194
    {
195
        return $this->copy()->heapSort($heap);
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function heapSort(SplHeap $heap)
202
    {
203
        foreach ($this as $item) {
204
            $heap->insert($item);
205
        }
206
207
        $this->setTraversable(static::fromItems($heap));
208
209
        return $this;
210
    }
211
212
    abstract public function count(): int;
213
214
    abstract public function current();
215
216
    abstract public function next();
217
218
    abstract public function rewind();
219
220
    abstract public function valid();
221
}
222