ConcreteProcessFactory::getAssessorArguments()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 4
nop 0
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
/**
14
 * @internal
15
 */
16
final class ConcreteProcessFactory implements ProcessFactory
17
{
18
    /**
19
     * Builder of objects implementing ChangesCountInterface.
20
     *
21
     * @var Closure(File):ChangesCountInterface
22
     */
23
    private $changesCountProcessBuilder;
24
25
    /**
26
     * Builder of objects implementing CyclomaticComplexityInterface.
27
     *
28
     * @var Closure(File):CyclomaticComplexityInterface
29
     */
30
    private $cyclomaticComplexityBuilder;
31
32
    /**
33
     * Class constructor.
34
     *
35
     * @param string $vcs Name of the version control system.
36
     * @param string $commitsSince String containing the date of when to look at commits since.
37
     */
38
    public function __construct(string $vcs, string $commitsSince)
39
    {
40
        $this->changesCountProcessBuilder = $this->getChangesCountProcessBuilder($vcs, $commitsSince);
41
        $this->cyclomaticComplexityBuilder = $this->getCyclomaticComplexityProcessBuilder();
42
    }
43
44
    /**
45
     * @param File $file File that the processes will execute on.
46
     * @return iterable<ProcessInterface> The list of processes to execute.
47
     */
48
    #[\Override]
49
    public function createProcesses(File $file): iterable
50
    {
51
        $processes = [];
52
        $processes[] = ($this->changesCountProcessBuilder)($file);
53
        $processes[] = ($this->cyclomaticComplexityBuilder)($file);
54
55
        return $processes;
56
    }
57
58
    /**
59
     * @param string $vcs Name of the version control system.
60
     * @param string $commitsSince String containing the date of when to look at commits since.
61
     * @return Closure(File):ChangesCountInterface
62
     */
63
    private function getChangesCountProcessBuilder(string $vcs, string $commitsSince): Closure
64
    {
65
        return (new ChangesCountProcessBuilder())->getBuilder($vcs, $commitsSince);
66
    }
67
68
    /**
69
     * Returns a cyclomatic complexity builder.
70
     *
71
     * @return Closure(File):CyclomaticComplexityInterface
72
     */
73
    private function getCyclomaticComplexityProcessBuilder(): Closure
74
    {
75
        $phpExecutable = $this->getPhpExecutable();
76
        $command = \array_merge([$phpExecutable], $this->getAssessorArguments());
77
78
        return static function (File $file) use ($command): CyclomaticComplexityInterface {
79
            $command[] = $file->getFullPath();
80
            $process = new Process($command);
81
82
            return new CyclomaticComplexityProcess($file, $process);
83
        };
84
    }
85
86
    /**
87
     * @return string The PHP executable.
88
     */
89
    private function getPhpExecutable(): string
90
    {
91
        $php = 'php';
92
        $executableFound = (new PhpExecutableFinder())->find();
93
        if (false !== $executableFound) {
94
            $php = $executableFound;
95
        }
96
97
        return $php;
98
    }
99
100
    /** @return array<string> */
101
    private function getAssessorArguments(): array
102
    {
103
        $fullPath = '';
104
        if (\class_exists(Phar::class, false)) {
105
            $fullPath = Phar::running(false);
106
        }
107
        if ('' !== $fullPath) {
108
            return [$fullPath, 'assess-complexity'];
109
        }
110
111
        return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner'];
112
    }
113
}
114