Completed
Push — master ( 7195f8...c1e355 )
by Bill
10s
created

ResultCollection::normalizeAgainst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Results;
4
5
use Illuminate\Support\Collection;
6
7
class ResultCollection extends Collection
8
{
9
    /**
10
     * Order results by their score in descending order.
11
     * @return self
12
     */
13
    public function orderByScoreDesc(): self
14
    {
15
        return $this->sortByDesc(function (Result $result) {
16
            return $result->getScore($this->maxCommits(), $this->maxComplexity());
17
        });
18
    }
19
20
    /**
21
     * Filter the results where their score is >= the provided $score
22
     * @param float $score Score to filter by.
23
     * @return static
24
     */
25
    public function whereScoreAbove(float $score)
26
    {
27
        return $this->filter(function (Result $result) use ($score) {
28
            return $result->getScore($this->maxCommits(), $this->maxComplexity()) >= $score;
29
        });
30
    }
31
32
    /**
33
     * Get the highest number of commits.
34
     * @return integer
35
     */
36
    public function maxCommits(): int
37
    {
38
        return $this->max(function (Result $result) {
39
            return $result->getCommits();
40
        });
41
    }
42
43
    /**
44
     * Get the highest complexity.
45
     * @return integer
46
     */
47
    public function maxComplexity(): int
48
    {
49
        return $this->max(function (Result $result) {
50
            return $result->getComplexity();
51
        });
52
    }
53
54
    /**
55
     * Override the original toArray() method to remove those disordered indices.
56
     *
57
     * @return array
58
     */
59
    public function toArray(): array
60
    {
61
        return array_values(array_map(
62
            function (Result $result) {
63
                return array_merge(
64
                    $result->toArray(),
65
                    [$result->getScore($this->maxCommits(), $this->maxComplexity())]
66
                );
67
            },
68
            $this->items
69
        ));
70
    }
71
}
72