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