1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Churn\Result; |
6
|
|
|
|
7
|
|
|
use Churn\Event\Event\AfterFileAnalysisEvent; |
8
|
|
|
use Churn\Event\Subscriber\AfterFileAnalysis; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class ResultAccumulator implements AfterFileAnalysis |
14
|
|
|
{ |
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
|
|
|
public function onAfterFileAnalysis(AfterFileAnalysisEvent $event): void |
58
|
|
|
{ |
59
|
|
|
$this->add($event->getResult()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Result $result The result for a file. |
64
|
|
|
*/ |
65
|
|
|
public function add(Result $result): void |
66
|
|
|
{ |
67
|
|
|
if (0 === $result->getPriority()) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->numberOfFiles++; |
72
|
|
|
|
73
|
|
|
if ($result->getCommits() > $this->maxCommits) { |
74
|
|
|
$this->maxCommits = $result->getCommits(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($result->getComplexity() > $this->maxComplexity) { |
78
|
|
|
$this->maxComplexity = $result->getComplexity(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->highestScores->add($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the maximum number of changes for a file. |
86
|
|
|
*/ |
87
|
|
|
public function getMaxCommits(): int |
88
|
|
|
{ |
89
|
|
|
return $this->maxCommits; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the maximum complexity for a file. |
94
|
|
|
*/ |
95
|
|
|
public function getMaxComplexity(): int |
96
|
|
|
{ |
97
|
|
|
return $this->maxComplexity; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the number of files processed. |
102
|
|
|
*/ |
103
|
|
|
public function getNumberOfFiles(): int |
104
|
|
|
{ |
105
|
|
|
return $this->numberOfFiles; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the highest score. |
110
|
|
|
*/ |
111
|
|
|
public function getMaxScore(): ?float |
112
|
|
|
{ |
113
|
|
|
$result = $this->highestScores->toArray()[0] ?? null; |
114
|
|
|
|
115
|
|
|
return null === $result |
116
|
|
|
? null |
117
|
|
|
: $result->getScore($this->maxCommits, $this->maxComplexity); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array<array<float|int|string>> |
122
|
|
|
*/ |
123
|
|
|
public function toArray(): array |
124
|
|
|
{ |
125
|
|
|
$rows = []; |
126
|
|
|
|
127
|
|
|
foreach ($this->highestScores->toArray() as $result) { |
128
|
|
|
$score = $result->getScore($this->maxCommits, $this->maxComplexity); |
129
|
|
|
|
130
|
|
|
if (null !== $this->minScore && $score < $this->minScore) { |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$rows[] = [ |
135
|
|
|
$result->getFile()->getDisplayPath(), |
136
|
|
|
$result->getCommits(), |
137
|
|
|
$result->getComplexity(), |
138
|
|
|
$score, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $rows; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|