CodeAnalyseCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 18.92%

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 104
ccs 7
cts 37
cp 0.1892
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cmd() 0 30 5
A configure() 0 3 1
A __construct() 0 5 1
A handle() 0 14 3
A command() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Laravel Code Analyse.
7
 *
8
 * (c) Nuno Maduro <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace NunoMaduro\LaravelCodeAnalyse\Console;
15
16
use function implode;
17
use function is_string;
18
use Illuminate\Console\Command;
19
use Symfony\Component\Process\Process;
20
use Illuminate\Console\Application as Artisan;
21
22
/**
23
 * @internal
24
 */
25
final class CodeAnalyseCommand extends Command
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $signature = 'code:analyse';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected $description = 'Analyses source code';
36
37
    /**
38
     * @var \NunoMaduro\LaravelCodeAnalyse\Console\OptionsResolver
39
     */
40
    private $optionsResolver;
41
42
    /**
43
     * CodeAnalyseCommand constructor.
44
     *
45
     * @param \NunoMaduro\LaravelCodeAnalyse\Console\OptionsResolver $optionsResolver
46
     */
47 1
    public function __construct(OptionsResolver $optionsResolver)
48
    {
49 1
        $this->optionsResolver = $optionsResolver;
50
51 1
        parent::__construct();
52 1
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    protected function configure(): void
58
    {
59 1
        $this->setDefinition($this->optionsResolver->getDefinition());
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function handle(): void
66
    {
67
        $process = new Process($this->cmd(), $this->laravel->basePath('vendor/bin'));
68
69
        if (Process::isTtySupported()) {
70
            $process->setTty(true);
71
        }
72
73
        $process->setTimeout(null);
74
75
        $process->start();
76
77
        foreach ($process as $type => $data) {
78
            $this->output->writeln($data);
79
        }
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    private function cmd(): string
86
    {
87
        $options = '';
88
        foreach ($this->optionsResolver->getDefinition()
89
                     ->getOptions() as $option) {
90
91
            if ($option->getName() === 'paths') {
92
                continue;
93
            }
94
95
            $this->input->getOption('memory-limit');
96
97
            $value = $this->option($name = $option->getName());
98
99
            if ($option->acceptValue()) {
100
                $options .= " --$name=$value";
101
            } else {
102
                if ($this->option($name)) {
103
                    $options .= " --$name";
104
                }
105
            }
106
        }
107
108
        $params = [
109
            $this->command(),
110
            $this->option('paths'),
111
            $options,
112
        ];
113
114
        return implode(' ', $params);
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    private function command(): string
121
    {
122
        $command = '';
123
124
        if (strncasecmp(PHP_OS, 'WIN', 3) !== 0) {
125
            $command .= Artisan::phpBinary();
126
        }
127
128
        return "$command phpstan analyse";
129
    }
130
}
131