| 1 | package Perl::Metrics2::Parse; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # Delegatable PPI caching parser. | ||||||||||||||
| 4 | # Takes a PPI::Cache directory and a list of files to parse. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Process (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Process::Storable (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Process::Delegatable (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use PPI::Util (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use PPI::Cache (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use PPI::Document (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use Params::Util '_ARRAY'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | our $VERSION = '0.06'; | |||||||||||||
| 17 | 1 | our @ISA = qw{ | |||||||||||||
| 18 | Process::Delegatable | ||||||||||||||
| 19 | Process::Storable | ||||||||||||||
| 20 | Process | ||||||||||||||
| 21 | }; | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 24 | 0 | my $class = shift; | |||||||||||||
| 25 | 0 | my $self = bless { @_ }, $class; | |||||||||||||
| 26 | |||||||||||||||
| 27 | # Check params | ||||||||||||||
| 28 | 0 | ▶ | unless ( -d $self->cache ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 0 | die "Missing or invalid cache directory"; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | 0 | ▶ | unless ( _ARRAY($self->files) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 0 | die "Missing or invalid file list"; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | return $self; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 0 | ▶ | sub cache { | ||||||||||||
|
Subroutine
cache: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 39 | 0 | $_[0]->{cache}; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | ▶ | sub files { | ||||||||||||
|
Subroutine
files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 43 | 0 | $_[0]->{files}; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | sub ok { | ||||||||||||
|
Subroutine
ok: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 47 | 0 | $_[0]->{ok}; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | sub prepare { | ||||||||||||
|
Subroutine
prepare: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||
| 52 | |||||||||||||||
| 53 | # Set the default PPI document cache | ||||||||||||||
| 54 | 0 | $self->{ppi_cache} = PPI::Cache->new( | |||||||||||||
| 55 | path => $self->cache, | ||||||||||||||
| 56 | ); | ||||||||||||||
| 57 | 0 | ▶ | unless ( PPI::Document->set_cache( $self->{ppi_cache} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | die "Failed to set PPI parser cache"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | return 1; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||||
| 66 | 0 | my @files = @{$self->files}; | |||||||||||||
| 67 | |||||||||||||||
| 68 | # Prepare the accounting | ||||||||||||||
| 69 | 0 | $self->{stats}->{files} = scalar @files; | |||||||||||||
| 70 | 0 | $self->{stats}->{parsed} = 0; | |||||||||||||
| 71 | 0 | $self->{stats}->{error} = 0; | |||||||||||||
| 72 | 0 | $self->{messages} = []; | |||||||||||||
| 73 | |||||||||||||||
| 74 | # Process the files | ||||||||||||||
| 75 | 0 | foreach my $file ( @files ) { | |||||||||||||
| 76 | # Skip if already cached | ||||||||||||||
| 77 | 0 | my $md5 = PPI::Util::md5hex_file($file); | |||||||||||||
| 78 | 0 | my (undef, $path) = $self->{ppi_cache}->_paths($md5); | |||||||||||||
| 79 | 0 | ▶ | next if -f $path; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | |||||||||||||||
| 81 | # Parse and cache the file, ignoring errors | ||||||||||||||
| 82 | 0 | my $document = eval { | |||||||||||||
| 83 | 0 | PPI::Document->new($file) | |||||||||||||
| 84 | }; | ||||||||||||||
| 85 | 0 | ▶ | if ( $@ ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 86 | 0 | push @{$self->{messages}}, "CRASHED while parsing $file"; | |||||||||||||
| 87 | 0 | $self->{stats}->{error}++; | |||||||||||||
| 88 | } elsif ( ! $document ) { | ||||||||||||||
| 89 | 0 | my $errstr = PPI::Document->errstr; | |||||||||||||
| 90 | 0 | push @{$self->{messages}}, "Failed to parse $file"; | |||||||||||||
| 91 | 0 | $self->{stats}->{error}++; | |||||||||||||
| 92 | } else { | ||||||||||||||
| 93 | 0 | push @{$self->{messages}}, "Parsed $file"; | |||||||||||||
| 94 | 0 | $self->{stats}->{parsed}++; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | # Success means we ran without errors, EVEN if we | ||||||||||||||
| 99 | # didn't actually need to parse anything. | ||||||||||||||
| 100 | 0 | ▶ | if ( $self->{stats}->{error} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 0 | $self->{ok} = 0; | |||||||||||||
| 102 | } else { | ||||||||||||||
| 103 | 0 | $self->{ok} = 1; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | return 1; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 1 | 1; | |||||||||||||