Skip to content

Instantly share code, notes, and snippets.

@ahutchings
Created January 24, 2009 05:00
Show Gist options
  • Save ahutchings/51340 to your computer and use it in GitHub Desktop.
Save ahutchings/51340 to your computer and use it in GitHub Desktop.
Graphical PHP_CodeSniffer results
<?php
// temp dir
$temp_dir = 'c:/windows/temp/';
// construct the filename
$temp_file = $temp_dir."phpcs-".time().".tmp";
// get the argument
$to_check = $argv[1];
// run PHP_CodeSniffer
ob_start();
system("C:/xampp/php/phpcs --report=xml --tab-width=4 $to_check");
$output = ob_get_contents();
ob_end_clean();
// parse the output as xml
$xml = simplexml_load_string($output);
$header = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8" />
<title>PHP_CodeSniffer</title>
<style type="text/css">
div {
font-family: arial;
font-size: 0.9em;
padding: 3px;
margin: 0 5px 7px 5px;
clear: both;
}
.error {
color: #EF3D23;
background: #FDF5F3;
border: 2px solid #EF3D23;
}
.warning {
background: #FFFED0;
border: 2px solid #F7D140;
}
</style>
</head>
<body>
EOD;
$footer = <<<EOD
</body>
</html>
EOD;
$xhtml = $header;
foreach ($xml->file as $file) {
$xhtml .= "<h3>".$file['name']." (".$file['errors']." errors, ".$file['warnings']." warnings)</h3>";
foreach ($file->error as $error) {
$xhtml .= "<div class='error'><b>Error</b> (line ".$error['line'].")<br />".$error."</div>\n";
}
foreach ($file->warning as $warning) {
$xhtml .= "<div class='warning'><b>Warning</b> (line ".$warning['line'].")<br />".$warning."</div>\n";
}
}
$xhtml .= $footer;
// write the file
$fp = fopen($temp_file, 'w');
fwrite($fp, $xhtml);
// open the browser
exec("\"c:\\program files\\mozilla firefox\\firefox.exe\" file:///$temp_file");
// delete the file
unset($fp);
unlink($temp_file);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment