1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Churn\Process; |
6
|
|
|
|
7
|
|
|
use Churn\File\File; |
8
|
|
|
use Closure; |
9
|
|
|
use Phar; |
10
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
class ConcreteProcessFactory implements ProcessFactory |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Builder of objects implementing ChangesCountInterface. |
18
|
|
|
* |
19
|
|
|
* @var Closure |
20
|
|
|
*/ |
21
|
|
|
private $changesCountProcessBuilder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builder of objects implementing CyclomaticComplexityInterface. |
25
|
|
|
* |
26
|
|
|
* @var Closure |
27
|
|
|
*/ |
28
|
|
|
private $cyclomaticComplexityBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class constructor. |
32
|
|
|
* |
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->cyclomaticComplexityBuilder = $this->getCyclomaticComplexityProcessBuilder(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param File $file File that the processes will execute on. |
44
|
|
|
* @return iterable<ProcessInterface> The list of processes to execute. |
45
|
|
|
*/ |
46
|
|
|
public function createProcesses(File $file): iterable |
47
|
|
|
{ |
48
|
|
|
$processes = []; |
49
|
|
|
$processes[] = ($this->changesCountProcessBuilder)($file); |
50
|
|
|
$processes[] = ($this->cyclomaticComplexityBuilder)($file); |
51
|
|
|
|
52
|
|
|
return $processes; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $vcs Name of the version control system. |
57
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
58
|
|
|
* @throws InvalidArgumentException If VCS is not supported. |
59
|
|
|
*/ |
60
|
|
|
private function getChangesCountProcessBuilder(string $vcs, string $commitsSince): Closure |
61
|
|
|
{ |
62
|
|
|
return (new ChangesCountProcessBuilder())->getBuilder($vcs, $commitsSince); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a cyclomatic complexity builder. |
67
|
|
|
*/ |
68
|
|
|
private function getCyclomaticComplexityProcessBuilder(): Closure |
69
|
|
|
{ |
70
|
|
|
$phpExecutable = (string)(new PhpExecutableFinder())->find(); |
71
|
|
|
$command = \array_merge([$phpExecutable], $this->getAssessorArguments()); |
72
|
|
|
|
73
|
|
|
return static function (File $file) use ($command): CyclomaticComplexityInterface { |
74
|
|
|
$command[] = $file->getFullPath(); |
75
|
|
|
$process = new Process($command); |
76
|
|
|
|
77
|
|
|
return new CyclomaticComplexityProcess($file, $process); |
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @return array<string> */ |
82
|
|
|
private function getAssessorArguments(): array |
83
|
|
|
{ |
84
|
|
|
if (\is_callable([Phar::class, 'running']) && '' !== Phar::running(false)) { |
85
|
|
|
return [Phar::running(false), 'assess-complexity']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner']; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|