Passed
Push — master ( 53846a...c9ead1 )
by Fabien
02:48
created

ConcreteProcessFactory::getPhpExecutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 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
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
    public function createProcesses(File $file): iterable
49
    {
50
        $processes = [];
51
        $processes[] = ($this->changesCountProcessBuilder)($file);
52
        $processes[] = ($this->cyclomaticComplexityBuilder)($file);
53
54
        return $processes;
55
    }
56
57
    /**
58
     * @param string $vcs Name of the version control system.
59
     * @param string $commitsSince String containing the date of when to look at commits since.
60
     */
61
    private function getChangesCountProcessBuilder(string $vcs, string $commitsSince): Closure
62
    {
63
        return (new ChangesCountProcessBuilder())->getBuilder($vcs, $commitsSince);
64
    }
65
66
    /**
67
     * Returns a cyclomatic complexity builder.
68
     */
69
    private function getCyclomaticComplexityProcessBuilder(): Closure
70
    {
71
        $phpExecutable = $this->getPhpExecutable();
72
        $command = \array_merge([$phpExecutable], $this->getAssessorArguments());
73
74
        return static function (File $file) use ($command): CyclomaticComplexityInterface {
75
            $command[] = $file->getFullPath();
76
            $process = new Process($command);
77
78
            return new CyclomaticComplexityProcess($file, $process);
79
        };
80
    }
81
82
    /**
83
     * @return string The PHP executable.
84
     */
85
    private function getPhpExecutable(): string
86
    {
87
        $php = 'php';
88
        $executableFound = (new PhpExecutableFinder())->find();
89
        if (false !== $executableFound) {
90
            $php = $executableFound;
91
        }
92
93
        return $php;
94
    }
95
96
    /** @return array<string> */
97
    private function getAssessorArguments(): array
98
    {
99
        if (\is_callable([Phar::class, 'running']) && '' !== Phar::running(false)) {
100
            return [Phar::running(false), 'assess-complexity'];
101
        }
102
103
        return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner'];
104
    }
105
}
106