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 substr; |
17
|
|
|
use function implode; |
18
|
|
|
use function strtoupper; |
19
|
|
|
use Illuminate\Console\Command; |
20
|
|
|
use Symfony\Component\Process\Process; |
21
|
|
|
use Illuminate\Console\Application as Artisan; |
22
|
|
|
|
23
|
|
|
final class CodeAnalyseCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Illuminate\Foundation\Application |
27
|
|
|
*/ |
28
|
|
|
protected $laravel; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected $signature = 'code:analyse |
34
|
|
|
{--level=1} |
35
|
|
|
{--path= : Path to directory which need to analyse} |
36
|
|
|
{--memory-limit= : Memory limit} |
37
|
|
|
'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected $description = 'Analyses source code'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function handle(): void |
48
|
|
|
{ |
49
|
|
|
$level = is_string($this->option('level')) ? $this->option('level') : 'max'; |
|
|
|
|
50
|
|
|
|
51
|
|
|
$path = $this->laravel['path']; |
52
|
|
|
|
53
|
|
|
if ($this->hasOption('path') && $this->option('path')) { |
54
|
|
|
$path = base_path($this->option('path')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$params = [ |
58
|
|
|
static::phpstanBinary(), |
59
|
|
|
'analyse', |
60
|
|
|
'--level=' . $level, |
61
|
|
|
'--autoload-file=' . $this->laravel->basePath('vendor/autoload.php'), |
62
|
|
|
'--configuration=' . __DIR__ . '/../../extension.neon', |
63
|
|
|
$path, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
if ($this->hasOption('memory-limit') && $this->option('memory-limit')) { |
67
|
|
|
$params[] = '--memory-limit=' . $this->option('memory-limit'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$process = new Process(implode(' ', $params), $this->laravel->basePath('vendor/bin')); |
71
|
|
|
|
72
|
|
|
if (Process::isTtySupported()) { |
73
|
|
|
$process->setTty(true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$process->setTimeout(null); |
77
|
|
|
|
78
|
|
|
$process->start(); |
79
|
|
|
|
80
|
|
|
foreach ($process as $type => $data) { |
81
|
|
|
$this->output->writeln($data); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private static function phpstanBinary(): string |
89
|
|
|
{ |
90
|
|
|
return sprintf('%s %s', Artisan::phpBinary(), 'phpstan'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|