Skip to content

Instantly share code, notes, and snippets.

@nuernbergerA
Created May 31, 2019 14:53
Show Gist options
  • Save nuernbergerA/b75c2cb2c80747fdf2e19e4bcfe00202 to your computer and use it in GitHub Desktop.
Save nuernbergerA/b75c2cb2c80747fdf2e19e4bcfe00202 to your computer and use it in GitHub Desktop.
Parse phpinsights output to checkstyle xml
<?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>';
@nuernbergerA
Copy link
Author

php artisan insights --no-interaction -v | php /path/to/parse_phpinsights.php > checkstyle-result.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment