Changeset 1511

Show
Ignore:
Timestamp:
02/05/12 16:47:28 (3 months ago)
Author:
Torben Brodt
Message:

update namespace tools

Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • namespace-unused

    r1509 r1511  
    11#!/usr/bin/php 
    22<?php 
     3# remove unused use statements 
    34if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) { 
    45        help(); 
     
    1516} 
    1617 
    17  
    1818function updateFile($filename) { 
    1919        echo $filename; 
    2020 
    2121        $block = file($filename); 
    22         $useblock = array(); 
    23         $i = 0; 
    24         foreach($block as $line) { 
     22        $regexp = $matches = array(); 
     23        foreach($block as $i => $line) { 
    2524                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                        ); 
    2730                } 
    28                 $i++; 
    2931        } 
    30         $flipped = array_flip($useblock); 
    3132         
    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).')/'; 
    3851                preg_match_all($regexp, implode("", $block), $res); 
    3952                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']++; 
    4755                        } 
    4856                } 
    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; 
    5263                        } 
    5364                } 
    5465        } 
    5566 
    56         if($useblock) { 
     67        if($hit) { 
    5768                file_put_contents($filename, implode("", $block)); 
    5869                echo "\tchanged\n"; 
  • namespace-use-order

    r1510 r1511  
    11#!/usr/bin/php 
    22<?php 
     3# sorts namespace-use clauses in a directory way 
    34if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) { 
    45        help(); 
     
    1415        } 
    1516} 
    16  
    1717 
    1818function updateFile($filename) { 
     
    9999                } 
    100100                // sort: alphabetical order 
    101                 asort($dir); 
    102                 asort($file); 
     101                natcasesort($dir); 
     102                natcasesort($file); 
    103103                $sort = array_merge(array_keys($dir), array_keys($file)); 
    104104