SortTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 64
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A usort() 0 6 1
A rsort() 0 6 1
A sort() 0 6 1
A usortStable() 0 10 2
A mapSort() 0 7 1
1
<?php
2
namespace Spindle\Collection\Traits;
3
4
trait SortTrait
5
{
6
    /**
7
     * @return \Spindle\Collection\Collection (new instance)
8
     */
9 1
    public function usort(callable $cmp)
10
    {
11 1
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
12 1
        usort($array, $cmp);
13 1
        return new $this($array, $this->debug);
0 ignored issues
show
Bug introduced by
The property debug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
    }
15
16
    /**
17
     * @param int $sort_flags \SORT_REGULAR|\SORT_NUMERIC|\SORT_STRING
18
     * @return \Spindle\Collection\Collection (new instance)
19
     */
20 1
    public function rsort($sort_flags = \SORT_REGULAR)
21
    {
22 1
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23 1
        rsort($array, $sort_flags);
24 1
        return new $this($array, $this->debug);
25
    }
26
27
    /**
28
     * @param int $sort_flags \SORT_REGULAR|\SORT_NUMERIC|\SORT_STRING
29
     * @return \Spindle\Collection\Collection (new instance)
30
     */
31 1
    public function sort($sort_flags = \SORT_REGULAR)
32
    {
33 1
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34 1
        sort($array, $sort_flags);
35 1
        return new $this($array, $this->debug);
36
    }
37
38
    /**
39
     * stable sort
40
     * @return \Spindle\Collection\Collection (new instance)
41
     */
42 1
    public function usortStable(callable $cmp)
43
    {
44 1
        $array = $this->map('[$_, $_i]')->toArray();
0 ignored issues
show
Bug introduced by
It seems like map() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45 1
        usort($array, static function ($a, $b) use ($cmp) {
46 1
            return $cmp($a[0], $b[0]) ?: ($a[1] - $b[1]);
47 1
        });
48 1
        $sorted = new $this($array, $this->debug);
49 1
        $sorted->column(0)->toArray();
50 1
        return $sorted;
51
    }
52
53
    /**
54
     * sort with Schwartzian Transform
55
     * @param string|callable $fn map function
56
     * @param SORT_ASC|SORT_DESC $sort_order
57
     * @param SORT_REGULAR|SORT_NUMERIC|SORT_STRING $sort_flags
58
     * @return \Spindle\Collection (new instance)
59
     */
60 1
    public function mapSort($fn, $sort_order = \SORT_ASC, $sort_flags = \SORT_REGULAR)
61
    {
62 1
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63 1
        $mapped = (new static($array))->map($fn)->toArray();
0 ignored issues
show
Unused Code introduced by
The call to SortTrait::__construct() has too many arguments starting with $array.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
It seems like map() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64 1
        array_multisort($mapped, $sort_order, $sort_flags, $array);
65 1
        return new $this($array, $this->debug);
66
    }
67
}
68