public function checkSyntax($filePath)
{
$binary = $this->getPHPBinaryPath();
exec($binary . ' -l ' . escapeshellarg($filePath) . ' 2>&1', $output);
$syntax = [];
$errorMsgLine = ' in ' . $filePath;
if (count($output)) {
foreach ($output as $string) {
if (empty($string)) {
continue;
}
if (strpos($string, 'error') !== false) {
$syntax['error'] = str_replace($errorMsgLine, '', $string);
if (preg_match('#line (?P<line>\\d*)#is', $syntax['error'], $matches)) {
$syntax['line'] = intval($matches['line']);
}
}
if (strpos($string, 'No syntax errors detected') !== false) {
$syntax['is_valid'] = true;
break;
}
}
}
return $syntax;
}