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