Psalm::processReport()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.0648
c 0
b 0
f 0
cc 5
nc 2
nop 1
1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use Fabrica\Tools;
6
use Fabrica\Tools\Builder;
7
use Fabrica\Models\Infra\Ci\Build;
8
use Fabrica\Models\Infra\Ci\BuildError;
9
use Fabrica\Tools\Plugin;
10
11
/**
12
 * A static analysis tool for finding errors in PHP applications https://getpsalm.org
13
 */
14
class Psalm extends Plugin
15
{
16
    /**
17
     * @var int 
18
     */
19
    protected $allowedErrors;
20
21
    /**
22
     * @var int 
23
     */
24
    protected $allowedWarnings;
25
26
    /**
27
     * @param Builder $builder
28
     * @param Build   $build
29
     * @param array   $options
30
     *
31
     * @throws \Exception
32
     */
33 View Code Duplication
    public function __construct(Builder $builder, Build $build, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        parent::__construct($builder, $build, $options);
36
37
        $this->executable = $this->findBinary('psalm');
38
39
        if (isset($options['allowed_errors']) && \is_int($options['allowed_errors'])) {
40
            $this->allowedErrors = $options['allowed_errors'];
41
        } else {
42
            $this->allowedErrors   = 0;
43
        }
44
45
        if (isset($options['allowed_warnings']) && \is_int($options['allowed_warnings'])) {
46
            $this->allowedWarnings = $options['allowed_warnings'];
47
        } else {
48
            $this->allowedWarnings = 0;
49
        }
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function execute()
56
    {
57
        $psalm = $this->executable;
58
59 View Code Duplication
        if ((!\defined('DEBUG_MODE') || !DEBUG_MODE) && !(bool)$this->build->getExtra('debug')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $this->builder->logExecOutput(false);
61
        }
62
63
        $this->builder->executeCommand('cd "%s" && ' . $psalm . ' --output-format=json', $this->builder->buildPath);
64
        $this->builder->logExecOutput(true);
65
66
        // Define that the plugin succeed
67
        $success = true;
68
69
        list($errors, $infos) = $this->processReport($this->builder->getLastOutput());
70
71 View Code Duplication
        if (0 < \count($errors)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            if (-1 !== $this->allowedErrors && \count($errors) > $this->allowedErrors) {
73
                $success = false;
74
            }
75
76
            foreach ($errors as $error) {
77
                $this->builder->logFailure('ERROR: ' . $error['full_message'] . \PHP_EOL);
78
79
                $this->build->reportError(
80
                    $this->builder,
81
                    self::pluginName(),
82
                    $error['message'],
83
                    BuildError::SEVERITY_HIGH,
84
                    $error['file'],
85
                    $error['line_from'],
86
                    $error['line_to']
87
                );
88
            }
89
        }
90
91 View Code Duplication
        if (0 < \count($infos)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            if (-1 !== $this->allowedWarnings && \count($infos) > $this->allowedWarnings) {
93
                $success = false;
94
            }
95
96
            foreach ($infos as $info) {
97
                $this->builder->logFailure('INFO: ' . $info['full_message'] . \PHP_EOL);
98
99
                $this->build->reportError(
100
                    $this->builder,
101
                    self::pluginName(),
102
                    $info['message'],
103
                    BuildError::SEVERITY_LOW,
104
                    $info['file'],
105
                    $info['line_from'],
106
                    $info['line_to']
107
                );
108
            }
109
        }
110
111
        if ($success) {
112
            $this->builder->logSuccess('No errors found!');
113
        }
114
115
        return $success;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public static function pluginName()
122
    {
123
        return 'psalm';
124
    }
125
126
    /**
127
     * @param  string $output
128
     * @return array
129
     */
130
    protected function processReport($output)
131
    {
132
        $data = \json_decode(trim($output), true);
133
134
        $errors = [];
135
        $infos  = [];
136
137
        if (!empty($data) && \is_array($data)) {
138
            foreach ($data as $value) {
139
                if (!\in_array($value['severity'], ['error','info'])) {
140
                    continue;
141
                }
142
143
                ${$value['severity'].'s'}[] = [
144
                    'full_message' => \vsprintf(
145
                        '%s - %s:%d:%d - %s' . \PHP_EOL . '%s', [
146
                        $value['type'],
147
                        $value['file_name'],
148
                        $value['line_from'],
149
                        $value['column_from'],
150
                        $value['message'],
151
                        $value['snippet']
152
                        ]
153
                    ),
154
                    'message'   => $value['message'],
155
                    'file'      => $value['file_name'],
156
                    'line_from' => $value['line_from'],
157
                    'line_to'   => $value['line_to']
158
                ];
159
            }
160
        }
161
162
        return [$errors, $infos];
163
    }
164
}
165