Changeset 1511
- Timestamp:
- 02/05/12 16:47:28 (3 months ago)
- Files:
-
- 1 added
- 2 modified
-
namespace-global-local (added)
-
namespace-unused (modified) (2 diffs)
-
namespace-use-order (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
namespace-unused
r1509 r1511 1 1 #!/usr/bin/php 2 2 <?php 3 # remove unused use statements 3 4 if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) { 4 5 help(); … … 15 16 } 16 17 17 18 18 function updateFile($filename) { 19 19 echo $filename; 20 20 21 21 $block = file($filename); 22 $useblock = array(); 23 $i = 0; 24 foreach($block as $line) { 22 $regexp = $matches = array(); 23 foreach($block as $i => $line) { 25 24 if(preg_match('/^use .*(?: |\\\)([a-zA-Z]+);$/', $line, $res)) { 26 $useblock[$i] = $res[1]; 25 $regexp[$i] = $res[1]; 26 $matches[$res[1]] = array( 27 'line' => $i, 28 'hits' => 0 29 ); 27 30 } 28 $i++;29 31 } 30 $flipped = array_flip($useblock);31 32 32 if($useblock) { 33 $regexp = '/(?:new ('.implode("|", $useblock).')(?: |\()'. 34 '|('.implode("|", $useblock).')::[^\W]+'. 35 '|(?:extends|implements) ('.implode("|", $useblock).')(?: |$)'. 36 '|(?:catch \(('.implode("|", $useblock).') )'. 37 ')/'; 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).')/'; 38 51 preg_match_all($regexp, implode("", $block), $res); 39 52 if($res) { 40 $res = array_merge((array)$res[1], (array)$res[2], (array)$res[3], (array)$res[4]); 41 foreach($res as $className) { 42 if(!isset($flipped[$className])) { 43 continue; 44 } 45 $idx = $flipped[$className]; 46 unset($useblock[$idx]); 53 foreach($res[1] as $className) { 54 $matches[$className]['hits']++; 47 55 } 48 56 } 49 if($useblock) { 50 foreach($useblock as $idx => $line) { 51 unset($block[$idx]); 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; 52 63 } 53 64 } 54 65 } 55 66 56 if($ useblock) {67 if($hit) { 57 68 file_put_contents($filename, implode("", $block)); 58 69 echo "\tchanged\n"; -
namespace-use-order
r1510 r1511 1 1 #!/usr/bin/php 2 2 <?php 3 # sorts namespace-use clauses in a directory way 3 4 if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) { 4 5 help(); … … 14 15 } 15 16 } 16 17 17 18 18 function updateFile($filename) { … … 99 99 } 100 100 // sort: alphabetical order 101 asort($dir);102 asort($file);101 natcasesort($dir); 102 natcasesort($file); 103 103 $sort = array_merge(array_keys($dir), array_keys($file)); 104 104
