Last active
October 30, 2015 13:10
-
-
Save vijinho/0b0d4d576b09f77ca5ed to your computer and use it in GitHub Desktop.
Find PHP legacy functions that are not in use in source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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