PsalmCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 0
c 2
b 0
f 1
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 1
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
        $processOutput = json_decode(
50
            file_get_contents($outputFilePath),
51
            true,
52
            512,
53
            JSON_THROW_ON_ERROR
54
        );
55
56
        if (!$process->getExitCode() > 1) {
57
            return new CommandResponse(
58
                status: CommandResponse::STATUS_FAIL,
59
                result: null,
60
                errors: array_filter([$processOutput, $process->getErrorOutput()]),
61
            );
62
        }
63
64
        return new CommandResponse(
65
            status: $process->isSuccessful() ? CommandResponse::STATUS_OK : CommandResponse::STATUS_ERROR,
66
            result: $processOutput
67
        );
68
    }
69
}
70