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