Skip to content

Instantly share code, notes, and snippets.

@Halleck45
Last active August 29, 2015 14:05
Show Gist options
  • Save Halleck45/d3d9f9748d8041c4b212 to your computer and use it in GitHub Desktop.
Save Halleck45/d3d9f9748d8041c4b212 to your computer and use it in GitHub Desktop.
Check licenses violations
<?php
// Usage
// composer licenses --format=json > licenses.json
// php analyze.php --file=licenses.json
$options = getopt('', array('file'));
$file = $options['file'];
$json = json_decode(file_get_contents($file));
$dependencies = $json->dependencies;
$errors = array();
foreach($dependencies as $name => $info ) {
$licenses = $info->license;
if(empty($licenses)) {
$errors[$name] = sprintf('no license found for %s', $name);
}
}
if(!empty($errors)) {
// root
$xml = new \DOMDocument("1.0", "UTF-8");
$xml->formatOutput = true;
$root = $xml->createElement("pmd");
$root->setAttribute('version', '@package_version@');
$root->setAttribute('timestamp', date('c'));
// violations
foreach($errors as $name=> $msg) {
$file = $xml->createElement('file');
$file->setAttribute('name', './composer.json');
$violation = $xml->createElement('violation');
$violation->setAttribute('beginline' , 1);
$violation->setAttribute('endline' , 2);
$violation->setAttribute('rule' , 'license');
$violation->setAttribute('ruleset' , 'dependencies should have license');
$violation->setAttribute('priority' , 3);
$violation->nodeValue = str_replace('/', '-', $msg);
$file->appendChild($violation);
$root->appendChild($file);
}
$xml->appendChild($root);
echo $xml->saveXML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment