Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Script used to do mass import statement fixes in ZF2
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";
// echo " Matches: " . var_export($matches, 1) . "\n";
$matched = "\n" . $matches[1];
$uses = array();
'/\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];
$uses = "\nuse " . implode(";\nuse ", $uses) . ";\n";
$replaced = str_replace($matched, $uses, $file);
file_put_contents($argv[1], $replaced);
echo " Wrote changes\n";

To execute, I did the following from the library/Zend directory, using zsh:

for file in **/*;do
    php /path/to/replace-uses.php $file;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.