-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLintProcess.php
158 lines (138 loc) · 3.94 KB
/
LintProcess.php
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
156
157
158
<?php
namespace JakubOnderka\PhpParallelLint\Process;
use JakubOnderka\PhpParallelLint\RunTimeException;
class LintProcess extends PhpProcess
{
const FATAL_ERROR = 'Fatal error';
const PARSE_ERROR = 'Parse error';
const DEPRECATED_ERROR = 'Deprecated:';
/**
* @var bool
*/
private $showDeprecatedErrors;
/**
* @var string
*/
protected $severity;
/**
* @param PhpExecutable $phpExecutable
* @param string $fileToCheck Path to file to check
* @param bool $aspTags
* @param bool $shortTag
* @param bool $deprecated
* @throws RunTimeException
*/
public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags = false, $shortTag = false, $deprecated = false)
{
if (empty($fileToCheck)) {
throw new \InvalidArgumentException("File to check must be set.");
}
$parameters = array(
'-d asp_tags=' . ($aspTags ? 'On' : 'Off'),
'-d short_open_tag=' . ($shortTag ? 'On' : 'Off'),
'-d error_reporting=E_ALL',
'-n',
'-l',
$fileToCheck,
);
$this->showDeprecatedErrors = $deprecated;
parent::__construct($phpExecutable, $parameters);
}
/**
* @return bool
* @throws
*/
public function containsError()
{
return $this->containsParserError($this->getOutput()) ||
$this->containsFatalError($this->getOutput()) ||
$this->containsDeprecatedError($this->getOutput());
}
/**
* @return string
* @throws RunTimeException
*/
public function getSyntaxError()
{
if ($this->containsError()) {
// Look for fatal errors first
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsFatalError($line)) {
$this->severity = 'error';
return $line;
}
}
// Look for parser errors second
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsParserError($line)) {
$this->severity = 'error';
return $line;
}
}
// Look for deprecated errors third
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsDeprecatedError($line)) {
$this->severity = 'warning';
return $line;
}
}
throw new RunTimeException("The output '{$this->getOutput()}' does not contain Parse or Syntax errors");
}
return false;
}
/**
* @return string
*/
public function getSeverity()
{
if (!empty($this->severity)) {
return $this->severity;
} else {
$error = $this->getSyntaxError();
return $this->severity;
}
}
/**
* @return bool
* @throws RunTimeException
*/
public function isFail()
{
return defined('PHP_WINDOWS_VERSION_MAJOR') ? $this->getStatusCode() === 1 : parent::isFail();
}
/**
* @return bool
* @throws RunTimeException
*/
public function isSuccess()
{
return $this->getStatusCode() === 0;
}
/**
* @param string $string
* @return bool
*/
private function containsParserError($string)
{
return strpos($string, self::PARSE_ERROR) !== false;
}
/**
* @param string $string
* @return bool
*/
private function containsFatalError($string)
{
return strpos($string, self::FATAL_ERROR) !== false;
}
/**
* @param string $string
* @return bool
*/
private function containsDeprecatedError($string)
{
if ($this->showDeprecatedErrors === false) {
return false;
}
return strpos($string, self::DEPRECATED_ERROR) !== false;
}
}