1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Tools\Php\SyntaxCheck; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\FileTool\FileTool; |
6
|
|
|
use Funivan\Cs\Fs\File; |
7
|
|
|
use Funivan\Cs\Fs\FileFilter; |
8
|
|
|
use Funivan\Cs\Report\Report; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
13
|
|
|
*/ |
14
|
|
|
class SyntaxCheckReview implements FileTool { |
15
|
|
|
|
16
|
|
|
const NAME = 'php_syntax_check_review'; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
4 |
|
public function getName() { |
23
|
4 |
|
return self::NAME; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function getDescription() { |
31
|
|
|
return 'Check php and html files syntax using standard php lint tool'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function canProcess(File $file) { |
39
|
|
|
return (new FileFilter())->notDeleted()->extension(['php', 'phtml', 'html'])->isValid($file); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
4 |
|
public function process(File $file, Report $report) { |
47
|
4 |
|
$cmd = sprintf('php --syntax-check %s', $file->getPath()); |
48
|
|
|
|
49
|
4 |
|
$process = new Process($cmd); |
50
|
4 |
|
$process->run(); |
51
|
|
|
|
52
|
|
|
# Create the array of outputs and remove empty values. |
53
|
4 |
|
$output = array_filter(explode(PHP_EOL, $process->getOutput())); |
54
|
|
|
|
55
|
4 |
|
if ($process->isSuccessful()) { |
56
|
1 |
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$regex = '!\s+on line (\d+)!'; |
60
|
|
|
|
61
|
3 |
|
$needle = 'Parse error: syntax error, '; |
62
|
3 |
|
foreach (array_slice($output, 0, count($output) - 1) as $error) { |
63
|
3 |
|
$raw = ucfirst(substr($error, strlen($needle))); |
64
|
3 |
|
$message = str_replace(' in ' . $file->getPath(), '', $raw); |
65
|
|
|
|
66
|
3 |
|
$line = 0; |
67
|
3 |
|
preg_match($regex, $message, $lineMatch); |
68
|
3 |
|
if (isset($lineMatch[1])) { |
69
|
3 |
|
$line = (int) $lineMatch[1]; |
70
|
3 |
|
$message = preg_replace($regex, '', $message); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$report->addMessage($file, $this, $message, $line); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
} |