SortTrait::sort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use Collections\Generic\ComparerInterface;
7
8
/**
9
 * Provides utility protected methods for extracting a property or column
10
 * from an array or object.
11
 */
12
trait SortTrait
13
{
14
    /**
15
     * Sorts the elements in the entire Collection<T> using the specified comparer.
16
     * @param ComparerInterface $comparer The ComparerInterface implementation to use when comparing elements, or null
17
     * to use the default comparer Comparer<T>.Default.
18
     * @return $this
19
     */
20 1 View Code Duplication
    public function sort(ComparerInterface $comparer = null)
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...
21
    {
22 1
        if ($comparer === null) {
23 1
            $comparer = $this->getDefaultComparer();
0 ignored issues
show
Bug introduced by
It seems like getDefaultComparer() 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...
24 1
        }
25 1
        usort($this->container, array($comparer, 'compare'));
0 ignored issues
show
Bug introduced by
The property container 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...
26
27 1
        return $this;
28
    }
29
30
    /**
31
     * Sorts the keys in the entire Collection<T> using the specified comparer.
32
     * @param ComparerInterface $comparer The ComparerInterface implementation to use when comparing elements, or
33
     * null to use the default comparer Comparer<T>.Default.
34
     * @return $this
35
     */
36 1 View Code Duplication
    public function sortByKey(ComparerInterface $comparer = null)
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...
37
    {
38 1
        if ($comparer === null) {
39 1
            $comparer = $this->getDefaultComparer();
0 ignored issues
show
Bug introduced by
It seems like getDefaultComparer() 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...
40 1
        }
41 1
        uksort($this->container, array($comparer, 'compare'));
42
43 1
        return $this;
44
    }
45
}
46