1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Churn\Results; |
5
|
|
|
|
6
|
|
|
use Churn\Processes\GitCommitCountProcess; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class ResultsParser |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Collection of results. |
13
|
|
|
* @var ResultCollection |
14
|
|
|
*/ |
15
|
|
|
private $resultsCollection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Turns a collection of completed processes into a |
19
|
|
|
* collection of parsed result objects. |
20
|
|
|
* @param Collection $completedProcesses Collection of completed processes. |
21
|
|
|
* @return ResultCollection |
22
|
|
|
*/ |
23
|
|
|
public function parse(Collection $completedProcesses): ResultCollection |
24
|
|
|
{ |
25
|
|
|
$this->resultsCollection = new ResultCollection; |
26
|
|
|
|
27
|
|
|
foreach ($completedProcesses as $file => $processes) { |
28
|
|
|
$this->parseCompletedProcessesForFile($file, $processes); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->resultsCollection; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parse the list of processes for a file. |
36
|
|
|
* @param string $file The file the processes were executed on. |
37
|
|
|
* @param array $processes The proceses that were executed on the file. |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
private function parseCompletedProcessesForFile(string $file, array $processes) |
41
|
|
|
{ |
42
|
|
|
$commits = (integer) $this->parseCommits($processes['GitCommitCountProcess']); |
43
|
|
|
$complexity = (integer) $processes['CyclomaticComplexityProcess']->getOutput(); |
44
|
|
|
|
45
|
|
|
$result = new Result([ |
46
|
|
|
'file' => $file, |
47
|
|
|
'commits' => $commits, |
48
|
|
|
'complexity' => $complexity, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->resultsCollection->push($result); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Parse the number of commits on the file from the raw process output. |
56
|
|
|
* @param GitCommitCountProcess $process Git Commit Count Process. |
57
|
|
|
* @return integer |
58
|
|
|
*/ |
59
|
|
|
private function parseCommits(GitCommitCountProcess $process): int |
60
|
|
|
{ |
61
|
|
|
$output = $process->getOutput(); |
62
|
|
|
preg_match("/([0-9]{1,})/", $output, $matches); |
63
|
|
|
return (integer) $matches[1] ?? 0; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|