|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Churn\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Churn\Processes\ChurnProcess; |
|
6
|
|
|
use Churn\Values\Config; |
|
7
|
|
|
use Churn\Values\File; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
10
|
|
|
use Symfony\Component\Process\ProcessUtils; |
|
11
|
|
|
|
|
12
|
|
|
class ProcessFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The config values. |
|
16
|
|
|
* @var Config |
|
17
|
|
|
*/ |
|
18
|
|
|
private $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ProcessFactory constructor. |
|
22
|
|
|
* @param Config $config Configuration Settings. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Config $config) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->config = $config; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates a VCS Commit Process that will run on $file. |
|
31
|
|
|
* @param File $file File that the process will execute on. |
|
32
|
|
|
* @return ChurnProcess |
|
33
|
|
|
*/ |
|
34
|
|
|
public function createVcsCommitProcess(File $file): ChurnProcess |
|
35
|
|
|
{ |
|
36
|
|
|
$process = new Process($this->getVscCommand($file) . ' | sort | uniq -c | sort -nr'); |
|
37
|
|
|
|
|
38
|
|
|
return new ChurnProcess($file, $process, 'VcsCommitProcess'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a Cyclomatic Complexity Process that will run on $file. |
|
43
|
|
|
* @param File $file File that the process will execute on. |
|
44
|
|
|
* @return ChurnProcess |
|
45
|
|
|
*/ |
|
46
|
|
|
public function createCyclomaticComplexityProcess(File $file): ChurnProcess |
|
47
|
|
|
{ |
|
48
|
|
|
$rootFolder = __DIR__ . '/../../'; |
|
49
|
|
|
|
|
50
|
|
|
$process = new Process( |
|
51
|
|
|
"php {$rootFolder}CyclomaticComplexityAssessorRunner {$file->getFullPath()}" |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return new ChurnProcess($file, $process, 'CyclomaticComplexityProcess'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getVscCommand(File $file): string |
|
58
|
|
|
{ |
|
59
|
|
|
$path = $file->getFullPath(); |
|
60
|
|
|
while ($dir = dirname($path)) { |
|
61
|
|
|
if ($dir == $path) { |
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (is_dir($dir . '/.git')) { |
|
66
|
|
|
return 'git log --since="' . $this->config->getCommitsSince() . '" --name-only --pretty=format: ' . $file->getFullPath(); |
|
67
|
|
|
} elseif (is_dir($dir . '/.hg')) { |
|
68
|
|
|
$since = date('Y-m-d', strtotime($this->config->getCommitsSince())); |
|
69
|
|
|
return 'hg log ' . $file->getFullPath() . ' --date "' . $since . ' to now" --template "' . $file->getDisplayPath() . '\n"'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$path = $dir; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new \InvalidArgumentException($file->getFullPath() . ' is not located in a known VCS'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|