1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/phplint |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\PHPLint\Process; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Lint. |
18
|
|
|
*/ |
19
|
|
|
class Lint extends Process |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function hasSyntaxError() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$output = trim($this->getOutput()); |
27
|
|
|
|
28
|
|
|
if (defined('HHVM_VERSION') && empty($output)) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false === strpos($output, 'No syntax errors detected'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return bool|array |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getSyntaxError() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if ($this->hasSyntaxError()) { |
41
|
|
|
$out = explode("\n", trim($this->getOutput())); |
42
|
|
|
|
43
|
|
|
return $this->parseError(array_shift($out)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Parse error message. |
51
|
|
|
* |
52
|
|
|
* @param string $message |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function parseError($message) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$pattern = '/^(PHP\s+)?(Parse|Fatal) error:\s*(?:\w+ error,\s*)?(?<error>.+?)\s+in\s+.+?\s*line\s+(?<line>\d+)/'; |
59
|
|
|
|
60
|
|
|
$matched = preg_match($pattern, $message, $match); |
61
|
|
|
|
62
|
|
|
if (empty($message)) { |
63
|
|
|
$message = 'Unknown'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [ |
67
|
|
|
'error' => $matched ? "{$match['error']} in line {$match['line']}" : $message, |
68
|
|
|
'line' => $matched ? abs($match['line']) : 0, |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function hasSyntaxIssue() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$output = trim($this->getOutput()); |
78
|
|
|
|
79
|
|
|
if (defined('HHVM_VERSION') && empty($output)) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
return (bool)preg_match('/(Warning:|Deprecated:|Notice:)/', $output); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool|array |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function getSyntaxIssue() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
if ($this->hasSyntaxIssue()) { |
93
|
|
|
$out = explode("\n", trim($this->getOutput())); |
94
|
|
|
|
95
|
|
|
return $this->parseIssue(array_shift($out)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Parse error message. |
103
|
|
|
* |
104
|
|
|
* @param string $message |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
private function parseIssue($message) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$pattern = '/^(PHP\s+)?(Warning|Deprecated|Notice):\s*?(?<error>.+?)\s+in\s+.+?\s*line\s+(?<line>\d+)/'; |
111
|
|
|
|
112
|
|
|
$matched = preg_match($pattern, $message, $match); |
113
|
|
|
|
114
|
|
|
if (empty($message)) { |
115
|
|
|
$message = 'Unknown'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return [ |
119
|
|
|
'error' => $matched ? "{$match['error']} in line {$match['line']}" : $message, |
120
|
|
|
'line' => $matched ? abs($match['line']) : 0, |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.