forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPahout.php
More file actions
155 lines (128 loc) · 3.93 KB
/
Copy pathPahout.php
File metadata and controls
155 lines (128 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace PHPCensor\Plugin;
use Exception;
use PHPCensor;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
use PHPCensor\Plugin;
/**
* A pair programming partner for writing better PHP.
* https://github.com/wata727/pahout
*/
class Pahout extends Plugin
{
/** @var string */
const TAB = "\t";
/** @var string */
protected $directory;
/** @var int */
protected $allowedWarnings;
/**
* @param Builder $builder
* @param Build $build
*
* @param array $options
*
* @throws Exception
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->executable = $this->findBinary(['pahout', 'pahout.phar']);
if (!empty($options['directory']) && is_string($options['directory'])) {
$this->directory = $options['directory'];
} else {
$this->directory = './src';
}
if (isset($options['allowed_warnings']) && is_int($options['allowed_warnings'])) {
$this->allowedWarnings = $options['allowed_warnings'];
} else {
$this->allowedWarnings = -1;
}
}
/**
* @return bool
*/
public function execute()
{
$pahout = $this->executable;
if (!$this->build->isDebug()) {
$this->builder->logExecOutput(false);
}
$this->builder->executeCommand(
'cd "%s" && ' . $pahout . ' %s --format=json',
$this->builder->buildPath,
$this->directory
);
$this->builder->logExecOutput(true);
// Define that the plugin succeed
$success = true;
list($files, $hints) = $this->processReport($this->builder->getLastOutput());
if (0 < count($hints)) {
if (-1 !== $this->allowedWarnings && count($hints) > $this->allowedWarnings) {
$success = false;
}
foreach ($hints as $hint) {
$this->builder->logFailure('HINT: ' . $hint['full_message'] . PHP_EOL);
$this->build->reportError(
$this->builder,
self::pluginName(),
$hint['message'],
BuildError::SEVERITY_LOW,
$hint['file'],
$hint['line_from']
);
}
}
if ($success) {
$this->builder->logSuccess('Awesome! There is nothing from me to teach you!');
}
$this->builder->log(sprintf('%d files checked, %d hints detected.', count($files), count($hints)));
return $success;
}
/**
* @return string
*/
public static function pluginName()
{
return 'pahout';
}
/**
* @param string $stage
* @param Build $build
* @return bool
*/
public static function canExecuteOnStage($stage, Build $build)
{
return Build::STAGE_TEST === $stage;
}
/**
* @param string $output
* @return array
*/
protected function processReport($output)
{
$data = json_decode(trim($output), true);
$hints = [];
$files = [];
if (!empty($data) && is_array($data) && isset($data['hints'])) {
$files = $data['files'];
foreach ($data['hints'] as $hint) {
$hints[] = [
'full_message' => vsprintf('%s:%d' . PHP_EOL . self::TAB . '%s: %s [%s]', [
$hint['filename'],
$hint['lineno'],
$hint['type'],
$hint['message'],
$hint['link']
]),
'message' => $hint['message'],
'file' => $hint['filename'],
'line_from' => $hint['lineno']
];
}
}
return [$files, $hints];
}
}