HookSetupCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 63
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 1
A copyPreCommitHook() 0 7 2
A setPermissions() 0 7 2
1
<?php
2
3
namespace Noitran\CsFixer\Console;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Process;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
9
/**
10
 * Class HookSetupCommand
11
 */
12
class HookSetupCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'phpcs:install-hook';
20
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'phpcs:install-hook';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Installs pre-commit git hook to run phpcs every on code changes.';
34
35
    /**
36
     * Handles the Command
37
     */
38
    public function handle(): void
39
    {
40
        $hookFile = config('phpcs.git_pre_commit_file');
41
        $gitHooksPath = config('phpcs.git_hooks_path');
42
43
        $this->copyPreCommitHook($hookFile, $gitHooksPath);
44
        $this->setPermissions($gitHooksPath);
45
    }
46
47
    /**
48
     * @param $hookFile
49
     * @param $gitHooksPath
50
     *
51
     * @return void
52
     */
53
    protected function copyPreCommitHook($hookFile, $gitHooksPath): void
54
    {
55
        $process = new Process('cp ' . $hookFile . ' ' . $gitHooksPath . '/');
0 ignored issues
show
Bug introduced by
'cp ' . $hookFile . ' ' . $gitHooksPath . '/' of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $process = new Process(/** @scrutinizer ignore-type */ 'cp ' . $hookFile . ' ' . $gitHooksPath . '/');
Loading history...
56
        $process->run();
57
58
        if (! $process->isSuccessful()) {
59
            throw new ProcessFailedException($process);
60
        }
61
    }
62
63
    /**
64
     * @param $gitHooksPath
65
     *
66
     * @return void
67
     */
68
    protected function setPermissions($gitHooksPath): void
69
    {
70
        $process = new Process('chmod +x ' . $gitHooksPath . '/pre-commit');
0 ignored issues
show
Bug introduced by
'chmod +x ' . $gitHooksPath . '/pre-commit' of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $process = new Process(/** @scrutinizer ignore-type */ 'chmod +x ' . $gitHooksPath . '/pre-commit');
Loading history...
71
        $process->run();
72
73
        if (! $process->isSuccessful()) {
74
            throw new ProcessFailedException($process);
75
        }
76
    }
77
}
78