Created
July 9, 2012 21:49
-
-
Save weierophinney/3079222 to your computer and use it in GitHub Desktop.
Script used to do mass import statement fixes in ZF2
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 | |
echo "Analyzing {$argv[1]}\n"; | |
$file = file_get_contents($argv[1]); | |
$matches = null; | |
preg_match('/\n(use .*?)(?:\r?\n(?:\/\*|interface|abstract|class|trait))/s', $file, $matches); | |
if (!$matches || !$matches[1]) { | |
echo " Did not match initial pattern\n"; | |
exit(0); | |
} | |
// echo " Matches: " . var_export($matches, 1) . "\n"; | |
$matched = "\n" . $matches[1]; | |
$uses = array(); | |
preg_replace_callback( | |
'/\n(?:use\s+|\s*)([a-zA-Z][a-zA-Z0-9\\\\_]*( as [a-zA-Z0-9_]+)?)/', | |
function ($m) use (&$uses) { | |
// echo "Callback matches: " . var_export($m, 1) . "\n\n"; | |
$uses[] = $m[1]; | |
}, | |
$matched | |
); | |
natcasesort($uses); | |
$uses = "\nuse " . implode(";\nuse ", $uses) . ";\n"; | |
$replaced = str_replace($matched, $uses, $file); | |
file_put_contents($argv[1], $replaced); | |
echo " Wrote changes\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To execute, I did the following from the library/Zend directory, using zsh: