Skip to content

Instantly share code, notes, and snippets.

@winks
Last active December 15, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winks/5254254 to your computer and use it in GitHub Desktop.
Save winks/5254254 to your computer and use it in GitHub Desktop.
A composer.json mess detector
#!/usr/bin/env php
<?php
////////////////////////////////////////
// check_deps
//
// This tool parses your composer.json, checks for certain patterns we do not
// want (e.g. dev-master) and then lists the available packages from packagist
// and displays all the released versions so you could possibly switch to a
// tagged and released *stable* version.
//
// If you think this is not needed, you probably never experienced a
// proper case of dependency hell.
$stuff = array('dev-master', '@dev', 'alpha', 'beta', 'rc');
////////////////////////////////////////////////////////////////////////////////
if (count($argv) > 1) {
if (is_readable(realpath($argv[1]))) {
$jsonFile = realpath($argv[1]);
} else {
echo "cannot find '${argv[1]}'.\n";
exit(1);
}
} else {
$jsonFile = __DIR__ . '/../composer.json';
if (!is_readable($jsonFile)) {
echo "cannot find 'composer.json'.\n";
exit(2);
}
}
$json = json_decode(file_get_contents($jsonFile), true);
/**
* Check for badwords in composer.json version strings.
*
* @param array $what an array like the 'require' field in a json_decoded composer.json
* @param array $stuff an array of unwanted patterns, e.g. array('dev-master', '@alpha'()
*
* @return array an array like this: array('author/foobar' => 'dev-master')
*/
function check($what, $stuff) {
$unstable = array();
foreach ($what as $k => $v) {
foreach ($stuff as $bad) {
if (strpos(strtolower($v), $bad) !== false) {
$unstable[$k] = $v;
}
}
}
return $unstable;
}
/**
* Format an array and check packagist for versions.
*
* @param array $what an array like this: array('author/foobar' => 'dev-master')
*
* @return void
*/
function fmt($what) {
foreach ($what as $kk => $vv) {
$keys = array_keys($vv);
uasort($keys, function ($a, $b) {
return strlen($a) < strlen($b);
});
$len = strlen(reset($keys));
printf("%s %s %s\n", str_repeat('=', 30), $kk, str_repeat('=', 30));
foreach ($vv as $name => $version) {
printf(
"%s%s\n",
str_pad($name, $len+20, " ", STR_PAD_RIGHT),
$version
);
$url = sprintf(
'https://packagist.org/packages/%s.json',
$name
);
$json = json_decode(file_get_contents($url), true);
if (count($json) > 0) {
if (!isset($json['package']['versions'])) {
echo "NO VERSIONS!\n";
continue;
}
foreach ($json['package']['versions'] as $vers => $data) {
printf(
"\t%s%s\n",
str_pad($vers, 20, " ", STR_PAD_RIGHT),
$data['time']
);
}
}
}
}
}
$unstable = array(
'require' => check($json['require'], $stuff),
'require-dev' => check($json['require-dev'], $stuff),
);
fmt($unstable);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment