Created
November 2, 2016 10:16
-
-
Save bikong0411/4430251f12ad543f472c834acd4856fe to your computer and use it in GitHub Desktop.
find php function and class in file under a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use feature 'say'; | |
use diagnostics; | |
use Carp qw/carp croak/; | |
use IO::File; | |
use File::Find; | |
use Data::Dumper; | |
sub get_class_func($); | |
sub get_files($); | |
sub get_branch_num($$); | |
sub write_to_file($$$$); | |
croak "Usage: Directory" unless @ARGV ==1; | |
my $dir = shift; | |
croak "$dir must be a directory " unless -d $dir; | |
my $final_file = IO::File->new("statistics.log",'w'); | |
my $ignoreClassPrefix =["ApolloV2" ,"CI_" ,"Composer"]; | |
############################ test code ##################################################### | |
#my $file = "/path/to/dao/commonDao.php";# | |
#my $result = get_class_func($file); # | |
#write_to_file($final_file,$file,$result); # | |
#exit; # | |
############################################################################################ | |
my $files = get_files($dir); | |
foreach my $file(@$files) { | |
my $result = get_class_func($file); | |
write_to_file($final_file,$file,$result,$ignoreClassPrefix); | |
say "analysis $file over!!!"; | |
} | |
$final_file->close; | |
say "###Success###, result store in statistics.log"; | |
sub get_files($){ | |
my $dir = shift; | |
my $files = []; | |
find( | |
sub { | |
/\.php$/ && push @$files, "$File::Find::name"; | |
}, $dir | |
); | |
return $files; | |
} | |
=begin | |
from file get class and function | |
=cut | |
sub get_class_func($) { | |
my $file = shift; | |
my $fh = IO::File->new($file,'r'); | |
my $result = {}; | |
my $inClass = 0; | |
my $inFunc = 0; | |
my $inComment = 0; | |
my $classBranchNum = 0; | |
my $funcBranchNum = 0; | |
my $className = '-'; | |
my $funcName = ''; | |
while(!$fh->eof) { | |
chomp($line=$fh->getline); | |
#match //,#单行注释 | |
$line =~ s#//.*##g; | |
$line =~ s/#.*//g; | |
#match /* | |
if($line =~ /\/\*/) { | |
$inComment = 1; | |
} | |
if($line =~ /\*\//) { | |
$inComment = 0; | |
next; | |
} | |
#如果在注释内 | |
if($inComment == 1) { | |
next; | |
} | |
#match class keywords | |
if($line =~ /^\s*(?:class|interface)\s+([a-zA-Z0-9-_]*)/io) { | |
$inClass = 1; | |
$className = $1; | |
$classBranchNum = $classBranchNum + get_branch_num($line, '{') - get_branch_num($line, '}'); | |
next; | |
} | |
#match function keywords | |
if($line =~ /\s+function\s+([a-zA-Z0-9-_]*)/io) { | |
$inFunc = 1; | |
$funcName = $1; | |
$result->{$className} = [] unless defined $result->{$className}; | |
push @{$result->{$className}}, $funcName; | |
} | |
my $leftBranchNum = get_branch_num($line, '{'); | |
my $rightBranchNum = get_branch_num($line, '}'); | |
if($inClass) { | |
$classBranchNum = $classBranchNum + $leftBranchNum - $rightBranchNum; | |
} | |
if($inFunc) { | |
$funcBranchNum = $leftBranchNum + $leftBranchNum - $rightBranchNum; | |
} | |
if ($inClass && $classBranchNum == 0 ) { | |
$className = '-'; | |
$inClass = 0; | |
} | |
if ( $inFunc && $funcBranchNum == 0) { | |
$funcName = ''; | |
$inFunc = 0; | |
} | |
} | |
return $result; | |
} | |
sub get_branch_num($$) { | |
my($line, $branch) = @_; | |
$branch = qq/\Q$branch\E/; | |
my $num = ()= $line =~ /$branch/g; | |
return $num; | |
} | |
sub write_to_file($$$$) { | |
my($fh, $file, $result,$ignoreClassPrefix) = @_; | |
while( my ( $className, $funcAryRef ) = each %$result) { | |
foreach my $func(@$funcAryRef) { | |
my $writeFlag = 1; | |
my @match = grep { my $tmpRegex = qq/\Q$_\E/;$className =~ /$tmpRegex/;} @$ignoreClassPrefix; | |
if (not @match ) { | |
$fh->write("$file\t$className\t$func\n"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment