Created
May 31, 2019 14:53
-
-
Save nuernbergerA/b75c2cb2c80747fdf2e19e4bcfe00202 to your computer and use it in GitHub Desktop.
Parse phpinsights output to checkstyle xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$input = file_get_contents('php://stdin'); | |
$headerPattern = '/• \[(.+)\] (.+): \((.+)\)/'; | |
$linePattern = '/ (.+):(\d+): (.+)/'; | |
$severity = 'error'; | |
$source = ''; | |
$message = ''; | |
$files = []; | |
foreach (explode("\n", $input) as $line) { | |
if (preg_match($headerPattern, $line, $matches)) { | |
$severity = $matches[1] === 'Code' ? 'error' : 'warning'; | |
$source = $matches[3]; | |
//$message = $matches[2]; | |
} | |
if (preg_match($linePattern, $line, $matches)) { | |
$files[$matches[1]][] = [ | |
'line' => $matches[2], | |
'column' => 1, | |
'severity' => $severity, | |
'source' => $source, | |
'message' => addslashes($matches[3]), | |
]; | |
} | |
} | |
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.'<checkstyle version="3.1.0">'.PHP_EOL; | |
foreach ($files as $fileName => $errors) { | |
echo '<file name="'.$fileName.'">'.PHP_EOL; | |
foreach ($errors as $error) { | |
echo '<error line="'.$error['line'].'" column="'.$error['column'].'" severity="'.$error['severity'].'" message="'.$error['message'].'" source="'.$error['source'].'" />'.PHP_EOL; | |
} | |
echo '</file>'.PHP_EOL; | |
} | |
echo '</checkstyle>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php artisan insights --no-interaction -v | php /path/to/parse_phpinsights.php > checkstyle-result.xml