1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Process; |
4
|
|
|
|
5
|
|
|
use function array_merge; |
6
|
|
|
use Churn\File\File; |
7
|
|
|
use Churn\Process\ChangesCount\GitChangesCountProcess; |
8
|
|
|
use Churn\Process\ChangesCount\NoVcsChangesCountProcess; |
9
|
|
|
use Closure; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use function is_callable; |
12
|
|
|
use Phar; |
13
|
|
|
use function strlen; |
14
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
15
|
|
|
use Symfony\Component\Process\Process; |
16
|
|
|
|
17
|
|
|
class ProcessFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Builder of objects implementing ChangesCountInterface. |
21
|
|
|
* @var Closure |
22
|
|
|
*/ |
23
|
|
|
private $changesCountProcessBuilder; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Executable to run PHP processes. |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $phpExecutable; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class constructor. |
33
|
|
|
* @param string $vcs Name of the version control system. |
34
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(string $vcs, string $commitsSince) |
37
|
|
|
{ |
38
|
|
|
$this->changesCountProcessBuilder = $this->getChangesCountProcessBuilder($vcs, $commitsSince); |
39
|
|
|
$this->phpExecutable = (string)(new PhpExecutableFinder())->find(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a process that will count the number of changes for $file. |
44
|
|
|
* @param File $file File that the process will execute on. |
45
|
|
|
* @return ChangesCountInterface |
46
|
|
|
*/ |
47
|
|
|
public function createChangesCountProcess(File $file): ChangesCountInterface |
48
|
|
|
{ |
49
|
|
|
return ($this->changesCountProcessBuilder)($file); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates a Cyclomatic Complexity Process that will run on $file. |
54
|
|
|
* @param File $file File that the process will execute on. |
55
|
|
|
* @return CyclomaticComplexityInterface |
56
|
|
|
*/ |
57
|
|
|
public function createCyclomaticComplexityProcess(File $file): CyclomaticComplexityInterface |
58
|
|
|
{ |
59
|
|
|
$command = array_merge( |
60
|
|
|
[$this->phpExecutable], |
61
|
|
|
$this->getAssessorArguments(), |
62
|
|
|
[$file->getFullPath()] |
63
|
|
|
); |
64
|
|
|
$process = new Process($command); |
65
|
|
|
|
66
|
|
|
return new CyclomaticComplexityProcess($file, $process); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $vcs Name of the version control system. |
71
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
72
|
|
|
* @return Closure |
73
|
|
|
* @throws InvalidArgumentException If VCS is not supported. |
74
|
|
|
*/ |
75
|
|
|
private function getChangesCountProcessBuilder(string $vcs, string $commitsSince): Closure |
76
|
|
|
{ |
77
|
|
|
switch ($vcs) { |
78
|
|
|
case 'git': |
79
|
|
|
return static function (File $file) use ($commitsSince): ChangesCountInterface { |
80
|
|
|
return new GitChangesCountProcess($file, $commitsSince); |
81
|
|
|
}; |
82
|
|
|
case 'none': |
83
|
|
|
return static function (File $file): ChangesCountInterface { |
84
|
|
|
return new NoVcsChangesCountProcess($file); |
85
|
|
|
}; |
86
|
|
|
default: |
87
|
|
|
throw new InvalidArgumentException('Unsupported VCS: ' . $vcs); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
|
|
private function getAssessorArguments(): array |
95
|
|
|
{ |
96
|
|
|
if (is_callable([Phar::class, 'running']) && strlen(Phar::running(false)) > 0) { |
97
|
|
|
return [Phar::running(false), 'assess-complexity']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner']; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|