Passed
Push — master ( b915a1...d7da76 )
by Fabien
02:15
created

ProcessFactory::getAssessorArguments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Process;
4
5
use function array_merge;
6
use Churn\File\File;
7
use function getcwd;
8
use function is_callable;
9
use Phar;
10
use function strlen;
11
use Symfony\Component\Process\PhpExecutableFinder;
12
use Symfony\Component\Process\Process;
13
14
class ProcessFactory
15
{
16
    /**
17
     * String containing the date of when to look at commits since.
18
     * @var string
19
     */
20
    private $commitsSince;
21
22
    /**
23
     * ProcessFactory constructor.
24
     * @param string $commitsSince String containing the date of when to look at commits since.
25
     */
26
    public function __construct(string $commitsSince)
27
    {
28
        $this->commitsSince = $commitsSince;
29
    }
30
31
    /**
32
     * Creates a Git Commit Process that will run on $file.
33
     * @param File $file File that the process will execute on.
34
     * @return ChurnProcess
35
     */
36
    public function createGitCommitProcess(File $file): ChurnProcess
37
    {
38
        $process = new Process([
39
            'git', '-C', getcwd(), 'rev-list', '--since',
40
            $this->commitsSince, '--no-merges', '--count', 'HEAD',
41
            $file->getFullPath(),
42
            ]);
43
44
        return new ChurnProcess($file, $process, 'GitCommitProcess');
45
    }
46
47
    /**
48
     * Creates a Cyclomatic Complexity Process that will run on $file.
49
     * @param File $file File that the process will execute on.
50
     * @return ChurnProcess
51
     */
52
    public function createCyclomaticComplexityProcess(File $file): ChurnProcess
53
    {
54
        $command = array_merge(
55
            [(new PhpExecutableFinder())->find()],
56
            $this->getAssessorArguments(),
57
            [$file->getFullPath()]
58
        );
59
        $process = new Process($command);
60
61
        return new ChurnProcess($file, $process, 'CyclomaticComplexityProcess');
62
    }
63
64
    /**
65
     * @return string[]
66
     */
67
    private function getAssessorArguments(): array
68
    {
69
        if (is_callable([Phar::class, 'running']) && strlen(Phar::running(false)) > 0) {
70
            return [Phar::running(false), 'assess-complexity'];
71
        }
72
73
        return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner'];
74
    }
75
}
76