root/namespace-use-order @ 1505

Revision 1505, 1.1 kB (checked in by Torben Brodt, 16 months ago)

fix namespace use cleanup script

  • Property svn:executable set to *
Line 
1#!/usr/bin/php
2<?php
3if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) {
4        help();
5        exit;
6}
7$dir = $_SERVER['argv'][1];
8
9$ite = new RecursiveDirectoryIterator($dir);
10foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
11        updateFile($filename);
12}
13
14
15function updateFile($filename) {
16        $useblock = $block = array();
17        $updated = false;
18        foreach(file($filename) as $line) {
19                if(preg_match('/^use /', $line)) {
20                        $useblock[] = $line;
21                } else {
22                        if($useblock) {
23                                $compare1 = implode("", $useblock);
24                                sort($useblock);
25                                $compare2 = implode("", $useblock);
26                                $updated = $updated || $compare1 !== $compare2;
27                                $block = array_merge($block, $useblock);
28                                $useblock = array();
29                        }
30                        $block[] = $line;
31                }
32        }
33       
34        echo $filename;
35        if($updated) {
36                file_put_contents($filename, implode("", $block));
37                echo "\tchanged\n";
38        } else {
39                echo "\tunchanged\n";
40        }
41}
42
43function help() {
44
45        echo "Usage: " . basename($_SERVER['argv'][0]) . " folder [--help]\n";
46        echo "\n";
47        echo "The options are as follows:\n";
48        echo "  -?, --help\n";
49        echo "    Show this help and exit.\n\n";
50}
Note: See TracBrowser for help on using the browser.