|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Churn\Result; |
|
6
|
|
|
|
|
7
|
|
|
use Churn\Event\Event\AfterFileAnalysis as AfterFileAnalysisEvent; |
|
8
|
|
|
use Churn\Event\Event\AfterFileAnalysisEvent as AfterFileAnalysisEventWithResult; |
|
9
|
|
|
use Churn\Event\Subscriber\AfterFileAnalysis; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ResultAccumulator implements AfterFileAnalysis, ResultReporter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var integer |
|
18
|
|
|
*/ |
|
19
|
|
|
private $maxCommits; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var integer |
|
23
|
|
|
*/ |
|
24
|
|
|
private $maxComplexity; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var integer |
|
28
|
|
|
*/ |
|
29
|
|
|
private $numberOfFiles; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var HighestScores |
|
33
|
|
|
*/ |
|
34
|
|
|
private $highestScores; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var float|null |
|
38
|
|
|
*/ |
|
39
|
|
|
private $minScore; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param integer $maxSize The maximum number of files to display in the results table. |
|
43
|
|
|
* @param float|null $minScore The minimum score a file need to display in the results table. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(int $maxSize, ?float $minScore) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->maxCommits = 0; |
|
48
|
|
|
$this->maxComplexity = 0; |
|
49
|
|
|
$this->numberOfFiles = 0; |
|
50
|
|
|
$this->minScore = $minScore; |
|
51
|
|
|
$this->highestScores = new HighestScores($maxSize); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param AfterFileAnalysisEvent $event The event triggered when the analysis of a file is done. |
|
56
|
|
|
*/ |
|
57
|
|
|
#[\Override] |
|
58
|
|
|
public function onAfterFileAnalysis(AfterFileAnalysisEvent $event): void |
|
59
|
|
|
{ |
|
60
|
|
|
if (!($event instanceof AfterFileAnalysisEventWithResult)) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->add($event->getResult()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param ResultInterface $result The result for a file. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function add(ResultInterface $result): void |
|
71
|
|
|
{ |
|
72
|
|
|
if (0 === $result->getPriority()) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->numberOfFiles++; |
|
77
|
|
|
|
|
78
|
|
|
if ($result->getCommits() > $this->maxCommits) { |
|
79
|
|
|
$this->maxCommits = $result->getCommits(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($result->getComplexity() > $this->maxComplexity) { |
|
83
|
|
|
$this->maxComplexity = $result->getComplexity(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->highestScores->add($result); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns the maximum number of changes for a file. |
|
91
|
|
|
*/ |
|
92
|
|
|
#[\Override] |
|
93
|
|
|
public function getMaxCommits(): int |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->maxCommits; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the maximum complexity for a file. |
|
100
|
|
|
*/ |
|
101
|
|
|
#[\Override] |
|
102
|
|
|
public function getMaxComplexity(): int |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->maxComplexity; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the number of files processed. |
|
109
|
|
|
*/ |
|
110
|
|
|
#[\Override] |
|
111
|
|
|
public function getNumberOfFiles(): int |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->numberOfFiles; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the highest score. |
|
118
|
|
|
*/ |
|
119
|
|
|
#[\Override] |
|
120
|
|
|
public function getMaxScore(): ?float |
|
121
|
|
|
{ |
|
122
|
|
|
$result = $this->highestScores->toArray()[0] ?? null; |
|
123
|
|
|
|
|
124
|
|
|
return null === $result |
|
125
|
|
|
? null |
|
126
|
|
|
: $result->getScore($this->maxCommits, $this->maxComplexity); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array<array<float|int|string>> |
|
131
|
|
|
*/ |
|
132
|
|
|
public function toArray(): array |
|
133
|
|
|
{ |
|
134
|
|
|
$rows = []; |
|
135
|
|
|
|
|
136
|
|
|
foreach ($this->highestScores->toArray() as $result) { |
|
137
|
|
|
$score = $result->getScore($this->maxCommits, $this->maxComplexity); |
|
138
|
|
|
|
|
139
|
|
|
if (null !== $this->minScore && $score < $this->minScore) { |
|
140
|
|
|
break; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$rows[] = [ |
|
144
|
|
|
$result->getFile()->getDisplayPath(), |
|
145
|
|
|
$result->getCommits(), |
|
146
|
|
|
$result->getComplexity(), |
|
147
|
|
|
$score, |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $rows; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|