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