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
|
|
|
* Phlint is a tool with an aim to help maintain quality of php code by analyzing code and pointing out potential code |
13
|
|
|
* issues. It focuses on how the code works rather than how the code looks. Phlint is designed from the start to do |
14
|
|
|
* deep semantic analysis rather than doing only shallow or stylistic analysis. |
15
|
|
|
* https://gitlab.com/phlint/phlint |
16
|
|
|
*/ |
17
|
|
|
class Phlint extends Plugin |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $allowedErrors = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Builder $builder |
26
|
|
|
* @param Build $build |
27
|
|
|
* @param array $options |
28
|
|
|
* |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function __construct(Builder $builder, Build $build, array $options = []) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
parent::__construct($builder, $build, $options); |
34
|
|
|
|
35
|
|
|
$this->executable = $this->findBinary('phlint'); |
36
|
|
|
|
37
|
|
|
if (\array_key_exists('allowed_errors', $options) && \is_int($options['allowed_errors'])) { |
38
|
|
|
$this->allowedErrors = $options['allowed_errors']; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function execute() |
46
|
|
|
{ |
47
|
|
|
$this->builder->executeCommand( |
48
|
|
|
'cd "%s" && %s analyze --no-interaction --no-ansi', |
49
|
|
|
$this->builder->buildPath, |
50
|
|
|
$this->executable |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
// Define that the plugin succeed |
54
|
|
|
$success = true; |
55
|
|
|
|
56
|
|
|
$errors = $this->processReport($this->builder->getLastOutput()); |
57
|
|
|
|
58
|
|
|
if (0 < \count($errors)) { |
59
|
|
|
if (-1 !== $this->allowedErrors && \count($errors) > $this->allowedErrors) { |
60
|
|
|
$success = false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($errors as $error) { |
64
|
|
|
$this->build->reportError( |
65
|
|
|
$this->builder, |
66
|
|
|
self::pluginName(), |
67
|
|
|
$error['message'], |
68
|
|
|
BuildError::SEVERITY_HIGH, |
69
|
|
|
$error['file'], |
70
|
|
|
$error['line_from'] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $success; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public static function pluginName() |
82
|
|
|
{ |
83
|
|
|
return 'phlint'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $output |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function processReport($output) |
91
|
|
|
{ |
92
|
|
|
$data = \explode(\chr(226), \preg_replace('#\\x1b[[][^A-Za-z\n]*[A-Za-z]#', '', \trim($output))); |
93
|
|
|
\array_pop($data); |
94
|
|
|
\array_shift($data); |
95
|
|
|
|
96
|
|
|
$errors = []; |
97
|
|
|
|
98
|
|
|
if (0 < \count($data)) { |
99
|
|
|
foreach ($data as $error) { |
100
|
|
|
$error = \explode(PHP_EOL, $error); |
101
|
|
|
$header = \substr(\trim(\array_shift($error)), 3); |
102
|
|
|
$file = \strstr(\substr(\strstr($header, 'in '), 3), ':', true); |
103
|
|
|
$line = \substr(\strrchr($header, ':'), 1); |
104
|
|
|
$message = \ltrim($error[0]) . PHP_EOL . \ltrim($error[1]); |
105
|
|
|
|
106
|
|
|
$errors[] = [ |
107
|
|
|
'message' => $message, |
108
|
|
|
'file' => $file, |
109
|
|
|
'line_from' => $line |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $errors; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.