Completed
Pull Request — master (#122)
by Matthias
01:39
created

ResultCollection::maxCommits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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