Skip to content

Instantly share code, notes, and snippets.

@padraic
Last active August 29, 2015 13:56
Show Gist options
  • Save padraic/8964909 to your computer and use it in GitHub Desktop.
Save padraic/8964909 to your computer and use it in GitHub Desktop.
Enumerating Composer packages for PHPCS analysis and data collection
<?php
define('PACKAGE_LIST', 'list.txt');
define('CODING_STANDARD', 'PSR2');
define('SEVERITY', 1);
define('ENCODING', 'utf-8');
define('REPORT', 'source');
define('EXTENSIONS', 'php');
$excluded = [
'*/test/*',
'*/tests/*',
'*/example/*',
'*/examples/*',
'*/doc/*',
'*/docs/*',
'*/build/*',
'*/data/*',
'*/resource/*',
'*/resources/*',
'*/bin/*',
'*/script/*',
'*/scripts/*',
'*/ext/*',
];
if (file_exists('composer.phar')) {
passthru('php composer.phar self-update');
} else {
passthru('curl -sS https://getcomposer.org/installer | php');
}
if (!file_exists('composer.lock') || !file_exists('composer.json')) {
$packages_required = '';
$handle = fopen(PACKAGE_LIST, "r");
while(fscanf($handle, "%s", $package_name)) {
$packages_required .= ' ' . $package_name . ':dev-master';
}
fclose($handle);
passthru('php composer.phar require --prefer-dist' . $packages_required);
} else {
passthru('php composer.phar update');
}
$paths = [];
$installed = json_decode(file_get_contents('./vendor/composer/installed.json'), true);
foreach($installed as $package) {
$fullpath = './vendor/' . $package['name'];
if (isset($package['autoload']['psr-0'])) {
foreach($package['autoload']['psr-0'] as $ns => $path) {
if (strlen($path) == 0) {
$fullpath .= '/' . str_replace('\\', '/', $ns);
} else {
$fullpath .= '/' . $path;
}
$paths[$package['name']] = $fullpath;
}
}
}
$options = [
'--ignore=' . implode(',', $excluded),
'--standard=' . CODING_STANDARD,
'--severity=' . SEVERITY,
'--encoding=' . ENCODING,
'--report=' . REPORT,
'--extensions=' . EXTENSIONS,
];
$reports_dir = 'reports-' . time();
mkdir($reports_dir);
$count = count($paths);
$i = 0;
foreach ($paths as $name => $path) {
$i++;
$report_name = str_replace('/', '.', $name);
$options['source'] = '--report-source=./' . $reports_dir . '/' . $report_name . '.source.txt';
$options['csv'] = '--report-csv=./' . $reports_dir . '/' . $report_name . '.csv';
echo "\n" . 'Setting the Sniffer on ' . $name . ' (' . $i . ' of ' . $count . ")\n\n";
$command = 'phpcs ' . implode(' ', $options) . ' ' . $path;
echo $command . "\n\n";
passthru($command);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment