Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active October 30, 2015 13:10
Show Gist options
  • Save vijinho/0b0d4d576b09f77ca5ed to your computer and use it in GitHub Desktop.
Save vijinho/0b0d4d576b09f77ca5ed to your computer and use it in GitHub Desktop.
Find PHP legacy functions that are not in use in source code
<?php
/**
* Find unused functions in a legacy project that are safe to remove
*
* @author: Vijay Mahrra <[email protected]>
*/
$folder = 'legacy';
$cmd = "grep -aRi 'function ' $folder/*";
exec($cmd, $results);
$removeable = [];
foreach ($results as $k => $line) {
if (preg_match('/^[\s]*(?<filename>[^:]+):[\s]*function[\s]*(?<function>[^\(]+)\(*/', $line, $matches)) {
$cmd = "grep -laRi '" . $matches['function'] . "' $folder/* | grep -v " . $matches['filename'];
$matching = [];
$found = exec($cmd, $matching);
if (empty($matching)) {
$used = [];
$cmd = "grep -i '" . $matches['function'] . "' " . $matches['filename'];
$check = exec($cmd, $used);
if (!empty($used) && count($used) == 1) {
$removable[$matches['filename']][] = $matches['function'];
}
}
}
}
print_r($removable);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment