root/namespace-unused

Revision 1511, 1.9 kB (checked in by Torben Brodt, 3 months ago)

update namespace tools

  • Property svn:executable set to *
Line 
1#!/usr/bin/php
2<?php
3# remove unused use statements
4if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) {
5        help();
6        exit;
7}
8$dir = $_SERVER['argv'][1];
9$pattern = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '/\.php$/';
10
11$ite = new RecursiveDirectoryIterator($dir);
12foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
13        if(preg_match($pattern, $filename)) {
14                updateFile($filename);
15        }
16}
17
18function updateFile($filename) {
19        echo $filename;
20
21        $block = file($filename);
22        $regexp = $matches = array();
23        foreach($block as $i => $line) {
24                if(preg_match('/^use .*(?: |\\\)([a-zA-Z]+);$/', $line, $res)) {
25                        $regexp[$i] = $res[1];
26                        $matches[$res[1]] = array(
27                                'line' => $i,
28                                'hits' => 0
29                        );
30                }
31        }
32       
33        // sort by length to avoid matching prefixes
34        if($regexp) {
35                $sortArray = $tmp = array();
36                foreach($regexp as $idx => $obj) {
37                        $sortArray[$idx] = strlen($obj);
38                        $tmp[$idx."x"] = $obj;
39                }
40
41                $regexp = array();
42                array_multisort($sortArray, SORT_DESC, $tmp);
43                foreach($tmp as $idx => $obj) {
44                        $regexp[substr($idx, 0, -1)] = $obj;
45                }
46        }
47       
48        $hit = false;
49        if($matches) {
50                $regexp = '/('.implode("|", $regexp).')/';
51                preg_match_all($regexp, implode("", $block), $res);
52                if($res) {
53                        foreach($res[1] as $className) {
54                                $matches[$className]['hits']++;
55                        }
56                }
57               
58                foreach($matches as $row) {
59                        if($row['hits'] == 1) {
60                                echo "\n\t- remove #".$row['line'].': '.$block[$row['line']];
61                                unset($block[$row['line']]);
62                                $hit = true;
63                        }
64                }
65        }
66
67        if($hit) {
68                file_put_contents($filename, implode("", $block));
69                echo "\tchanged\n";
70        } else {
71                echo "\tunchanged\n";
72        }
73}
74
75function help() {
76        echo "removes unused use clauses";
77        echo "\n";
78        echo "Usage: " . basename($_SERVER['argv'][0]) . " folder [--help]\n";
79        echo "\n";
80        echo "The options are as follows:\n";
81        echo "  -?, --help\n";
82        echo "    Show this help and exit.\n\n";
83}
Note: See TracBrowser for help on using the browser.