Passed
Pull Request — master (#65)
by Dmitriy
03:03
created

PsalmCommand::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Inspector\Command;
6
7
use Symfony\Component\Process\Process;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Yii\Debug\Api\Inspector\CommandInterface;
10
use Yiisoft\Yii\Debug\Api\Inspector\CommandResponse;
11
12
class PsalmCommand implements CommandInterface
13
{
14
    public const COMMAND_NAME = 'analyse/psalm';
15
16
    public function __construct(private Aliases $aliases)
17
    {
18
    }
19
20
    public static function getTitle(): string
21
    {
22
        return 'Psalm';
23
    }
24
25
    public static function getDescription(): string
26
    {
27
        return '';
28
    }
29
30
    public function run(): CommandResponse
31
    {
32
        $projectDirectory = $this->aliases->get('@root');
33
        $debugDirectory = $this->aliases->get('@runtime/debug');
34
35
        $outputFilePath = $debugDirectory . DIRECTORY_SEPARATOR . 'psalm-report.json';
36
37
        $params = [
38
            'vendor/bin/psalm',
39
            '--report=' . $outputFilePath,
40
        ];
41
42
        $process = new Process($params);
43
44
        $process
45
            ->setWorkingDirectory($projectDirectory)
46
            ->setTimeout(null)
47
            ->run();
48
49
        if (!$process->isSuccessful()) {
50
            return new CommandResponse(
51
                status: CommandResponse::STATUS_ERROR,
52
                result: null,
53
                errors: [$process->getErrorOutput()],
54
            );
55
        }
56
57
        return new CommandResponse(
58
            status: CommandResponse::STATUS_OK,
59
            result: json_decode(
60
                file_get_contents($outputFilePath),
61
                true,
62
                512,
63
                JSON_THROW_ON_ERROR
64
            )
65
        );
66
    }
67
}
68