Passed
Push — 1.x ( 65e39f...5399bc )
by Ulises Jeremias
02:36
created

Sort::quickSort()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 60
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 33
nop 1
dl 0
loc 60
rs 7.0677
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Sort::arraySort() 0 11 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Mbh\Collection\Traits;
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\FixedArray;
12
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
13
use SplFixedArray;
14
use SplStack;
15
use LimitIterator;
16
17
trait Sort
18
{
19
    /**
20
     * Perform a bottom-up, non-recursive, in-place mergesort.
21
     * Efficient for very-large objects, and written without recursion
22
     * since PHP isn't well optimized for large recursion stacks.
23
     *
24
     * @param callable $callback The callback for comparison
25
     * @return SequenceableInterface
26
     */
27
    public function mergeSort(callable $callback): SequenceableInterface
28
    {
29
        $count = count($this);
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Sort 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

29
        $count = count(/** @scrutinizer ignore-type */ $this);
Loading history...
30
        $result = new SplFixedArray($count);
31
        for ($k = 1; $k < $count; $k = $k << 1) {
32
            for ($left = 0; ($left + $k) < $count; $left += $k << 1) {
33
                $right = $left + $k;
34
                $rend = min($right + $k, $count);
35
                $m = $left;
36
                $i = $left;
37
                $j = $right;
38
                while ($i < $right && $j < $rend) {
39
                    if ($callback($this[$i], $this[$j]) <= 0) {
40
                        $result[$m] = $this[$i];
41
                        $i++;
42
                    } else {
43
                        $result[$m] = $this[$j];
44
                        $j++;
45
                    }
46
                    $m++;
47
                }
48
                while ($i < $right) {
49
                    $result[$m] = $this[$i];
50
                    $i++;
51
                    $m++;
52
                }
53
                while ($j < $rend) {
54
                    $result[$m] = $this[$j];
55
                    $j++;
56
                    $m++;
57
                }
58
                for ($m = $left; $m < $rend; $m++) {
59
                    $this[$m] = $result[$m];
60
                }
61
            }
62
        }
63
64
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mbh\Collection\Traits\Sort which is incompatible with the type-hinted return Mbh\Collection\Interfaces\Sequenceable.
Loading history...
65
    }
66
67
    /**
68
     * Sort by applying a CallbackHeap and building a new heap
69
     * Can be efficient for sorting large stored objects.
70
     *
71
     * @param callable $callback The comparison callback
72
     * @return SequenceableInterface
73
     */
74
    public function heapSort(callable $callback): SequenceableInterface
75
    {
76
        $h = new CallbackHeap($callback);
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\CallbackHeap 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...
77
        foreach ($this as $elem) {
78
            $h->insert($elem);
79
        }
80
81
        return static::fromItems($h);
82
    }
83
84
    /**
85
     * Fallback behaviour to use the builtin array sort functions
86
     *
87
     * @param callable $callback The callback for comparison
88
     * @return SequenceableInterface
89
     */
90
    public function arraySort(callable $callback = null): SequenceableInterface
91
    {
92
        $array = $this->toArray();
0 ignored issues
show
Bug introduced by
It seems like toArray() 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

92
        /** @scrutinizer ignore-call */ 
93
        $array = $this->toArray();
Loading history...
93
94
        if ($callback) {
95
            usort($array, $callback);
96
        } else {
97
            sort($array);
98
        }
99
100
        return static::fromArray($array);
101
    }
102
}
103