Passed
Pull Request — master (#248)
by Fabien
02:47
created

Result::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Result;
4
5
use Webmozart\Assert\Assert;
6
7
class Result
8
{
9
    /**
10
     * The file property.
11
     * @var string
12
     */
13
    private $file;
14
15
    /**
16
     * The commits property.
17
     * @var integer
18
     */
19
    private $commits;
20
21
    /**
22
     * The complexity property.
23
     * @var integer
24
     */
25
    private $complexity;
26
27
    /**
28
     * Class constructor.
29
     * @param string $file The path of the processed file.
30
     */
31
    public function __construct(string $file)
32
    {
33
        $this->file = $file;
34
        $this->commits = -1;
35
        $this->complexity = -1;
36
    }
37
38
    /**
39
     * Get the file path.
40
     * @return string
41
     */
42
    public function getFile(): string
43
    {
44
        return $this->file;
45
    }
46
47
    /**
48
     * Indicates whether the metrics are all set.
49
     * @return boolean
50
     */
51
    public function isComplete(): bool
52
    {
53
        return $this->commits > -1 && $this->complexity > -1;
54
    }
55
56
    /**
57
     * @param int $commits Number of changes.
58
     * @return self
59
     */
60
    public function setCommits(int $commits): self
61
    {
62
        $this->commits = $commits;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get the number of changes.
69
     * @return integer
70
     */
71
    public function getCommits(): int
72
    {
73
        return $this->commits;
74
    }
75
76
    /**
77
     * @param int $complexity The file complexity.
78
     * @return self
79
     */
80
    public function setComplexity(int $complexity): self
81
    {
82
        $this->complexity = $complexity;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get the file complexity.
89
     * @return integer
90
     */
91
    public function getComplexity(): int
92
    {
93
        return $this->complexity;
94
    }
95
96
    /**
97
     * Get the file priority.
98
     * @return integer
99
     */
100
    public function getPriority(): int
101
    {
102
        return $this->commits * $this->complexity;
103
    }
104
105
    /**
106
     * Calculate the score.
107
     * @param int $maxCommits    The highest number of commits out of any file scanned.
108
     * @param int $maxComplexity The maximum complexity out of any file scanned.
109
     * @return float
110
     * @codingStandardsIgnoreStart
111
     */
112
    public function getScore(int $maxCommits, int $maxComplexity): float
113
    {
114
        Assert::greaterThan($maxComplexity, 0);
115
        Assert::greaterThan($maxCommits, 0);
116
117
        /*
118
         * Calculate the horizontal and vertical distance from the "top right"
119
         * corner.
120
         */
121
        $horizontalDistance = $maxCommits - $this->getCommits();
122
        $verticalDistance = $maxComplexity - $this->getComplexity();
123
124
        /*
125
         * Normalize these values over time, we first divide by the maximum
126
         * values, to always end up with distances between 0 and 1.
127
         */
128
        $normalizedHorizontalDistance = $horizontalDistance / $maxCommits;
129
        $normalizedVerticalDistance = $verticalDistance / $maxComplexity;
130
131
        /*
132
         * Calculate the distance of this class from the "top right" corner,
133
         * using the simple formula A^2 + B^2 = C^2; or: C = sqrt(A^2 + B^2)).
134
         */
135
        $distanceFromTopRightCorner = sqrt(
136
            $normalizedHorizontalDistance ** 2
137
            + $normalizedVerticalDistance ** 2
138
        );
139
140
        /*
141
         * The resulting value will be between 0 and sqrt(2). A short distance is bad,
142
         * so in order to end up with a high score, we invert the value by
143
         * subtracting it from 1.
144
         */
145
        return round(1 - $distanceFromTopRightCorner, 3);
146
        // @codingStandardsIgnoreEnd
147
    }
148
}
149