lib/Acme/ReturnValue.pm

filter: Statementstmt 60.1% 104 / 173 Branchbran 22.0% 11 / 50 Conditioncond 14.2% 2 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 80.0% 24 / 30 Podpod 100% 6 / 6 totaltotal 52.4% 148 / 282 CC 37 SCAR 36.8
CC
37
Cov
49%
in_CPAN
132.0
in_dir
72.0
run
56.0
CRAP 39.5
« lib/Acme/ReturnValue/MakeSite.pm ↑ Summary script/acme_returnvalue.pl »
1 package Acme::ReturnValue;
2  
3 # ABSTRACT: report interesting return values
4 4 our $VERSION = '1.004'; # VERSION
5  
6 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Parse::CPAN::Packages;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Path::Class qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Archive::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 with qw(MooseX::Getopt);
25 4 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 ▶has 'interesting' => (is=>'rw',isa=>'ArrayRef',default=>sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 ▶has 'bad' => (is=>'rw',isa=>'ArrayRef',default=>sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
29 8 ▶has 'failed' => (is=>'rw',isa=>'ArrayRef',default=>sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 4 has 'quiet' => (is=>'ro',isa=>'Bool',default=>0);
32 4 has 'inc' => (is=>'ro',isa=>'Bool',default=>0);
33 4 has 'dir' => (is=>'ro',isa=>'Path::Class::Dir',coerce=>1);
34 4 has 'file' => (is=>'ro',isa=>'Path::Class::File',coerce=>1);
35 4 has 'cpan' => (is=>'ro',isa=>'Path::Class::Dir',coerce=>1);
36 4 has 'dump_to' => (is=>'ro',isa=>'Path::Class::Dir',coerce=>1,default=>'returnvalues');
37  
38 4 has 'json_encoder' => (is=>'ro',lazy_build=>1);
39 0 ▶sub _build_json_encoder {
Subroutine _build_json_encoder: not called CC=1 SCAR=6.9
40 0 return JSON->new->pretty;
41 }
42  
43  
44  
45 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
46 0 my $self = shift;
47  
48 0 ▶ if ($self->inc) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->inc) { } 0 0 0
elsif ($self->dir) { } 0 0 0
elsif ($self->file) { } 0 0 0
elsif ($self->cpan) { } 0 0 0
49 0 $self->in_INC;
50 }
51 elsif ($self->dir) {
52 0 $self->in_dir($self->dir);
53 }
54 elsif ($self->file) {
55 0 $self->in_file($self->file);
56 }
57 elsif ($self->cpan) {
58 0 $self->in_CPAN($self->cpan,$self->dump_to);
59 0 exit;
60 }
61 else {
62 0 $self->in_dir('.');
63 }
64  
65 0 my $interesting=$self->interesting;
66 0 ▶ if (@$interesting > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$interesting > 0) { } 0 0 0
67 0 foreach my $cool (@$interesting) {
68 0 say $cool->{package} .': '.$cool->{value};
69 }
70 }
71 else {
72 0 say "boring!";
73 }
74 }
75  
76  
77 8 ▶sub waste_some_cycles {
Subroutine waste_some_cycles: called CC=8 SCAR=20.9
Pod documented
78 8 my ($self, $filename) = @_;
79  
80 8 my $doc = PPI::Document->new($filename);
81  
82 8 eval { # I don't care if that fails...
83 8 $doc->prune('PPI::Token::Comment');
84 8 $doc->prune('PPI::Token::Pod');
85 };
86  
87 8 my @packages=$doc->find('PPI::Statement::Package');
88 8 my $this_package;
89  
90 8 foreach my $node ($packages[0][0]->children) {
91 32 ▶ if ($node->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Token::Word")) 16 16 32
92 16 $this_package = $node->content;
93 }
94 }
95  
96 8 my @significant = grep { _is_code($_) } $doc->schildren();
97 8 my $match = $significant[-1];
98 8 my $rv=$match->content;
99 8 $rv=~s/\s*;$//;
100 8 $rv=~s/^return //gi;
101  
102 8 ▶ return if $rv eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq 1 2 6 8
103 6 ▶ return if $rv eq '__PACKAGE__';
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq "__PACKAGE__" 0 6 6
104 6 ▶ return if $rv =~ /^__PACKAGE__->meta->make_immutable/;
Branch: 1 branch
BranchTrueFalseTotal
if $rv =~ /^__PACKAGE__->meta->make_immutable/ 1 5 6
105  
106 5 $rv = decode_utf8($rv);
107  
108 5 my $data = {
109 'file' => $filename,
110 'package' => $this_package,
111 'PPI' => ref $match,
112 };
113  
114 5 my @bad = map { 'PPI::Statement::'.$_} qw(Sub Variable Compound Package Scheduled Include Sub);
115  
116 5 ▶ if (any { ref($match) eq $_ } @bad) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { ref $match eq $_; } , @bad)) { } 0 5 5
elsif ($rv =~ /^('|"|\d|qw|qq|q|!|~)/) { } 4 1 5
117 0 $data->{'bad'}=$rv;
118 0 push(@{$self->bad},$data);
119 }
120 elsif ($rv =~ /^('|"|\d|qw|qq|q|!|~)/) {
121 4 $data->{'value'}=$rv;
122 4 push(@{$self->interesting},$data);
123 }
124 else {
125 1 $data->{'bad'}=$rv;
126 1 $data->{'PPI'}.=" (but very likely crap)";
127 1 push(@{$self->bad},$data);
128 }
129 }
130  
131  
132 85 ▶sub _is_code {
Subroutine _is_code: called CC=2 SCAR=7.1
133 85 my $elem = shift;
134 85 ▶ return ! ( $elem->isa('PPI::Statement::End')
Condition66%2/3
$elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Data')
l!l&&r!l&&!r
2083
MC/DC50%1/2
$elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Data')
$elem->isa('PPI::Statement::End') $elem->isa('PPI::Statement::Data')
Truth table66%2/3
A || B
A= $elem->isa('PPI::Statement::End')B= $elem->isa('PPI::Statement::Data')
ABresult
1X1
011
000
135 || $elem->isa('PPI::Statement::Data'));
136 }
137  
138  
139 0 ▶sub in_CPAN {
Subroutine in_CPAN: not called CC=11 SCAR=48.8
Pod documented
140 0 my ($self,$cpan,$out)=@_;
141  
142 0 my $p=Parse::CPAN::Packages->new($cpan->file(qw(modules 02packages.details.txt.gz))->stringify);
143  
144 0 ▶ if (!-d $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $out) 0 0 0
145 0 ▶ $out->mkpath || die "cannot make dir $out";
Branch: 1 branch
BranchTrueFalseTotal
$out->mkpath or die "cannot make dir $out" 0 0 0
146 }
147  
148 # get all old data files so we can later delete non-current
149 0 my %old_files;
150 0 ▶ while (my $file = $out->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $out->next) 0 0 0
151 0 ▶ next unless $file =~ /\.json/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.json/ 0 0 0
152 0 $old_files{$file->basename}=1;
153 }
154  
155 # analyse cpan
156 0 foreach my $dist (sort {$a->dist cmp $b->dist} $p->latest_distributions) {
157 0 delete $old_files{$dist->distvname.'.json'};
158 0 ▶ next if (-e $out->file($dist->distvname.'.json'));
Branch: 1 branch
BranchTrueFalseTotal
if -e $out->file($dist->distvname . '.json') 0 0 0
159  
160 0 my $data;
161 0 my $distfile = $cpan->file('authors','id',$dist->prefix);
162 0 $data->{file}=$distfile;
163 0 my $dir;
164 0 eval {
165 0 $dir = tempdir('/var/tmp/arv_XXXXXX');
166 0 ▶ my $archive=Archive::Any->new($distfile->stringify) || die $!;
Condition0%0/2
'Archive::Any'->new($distfile->stringify) || die($!)
l!l
00
MC/DC0%0/1
'Archive::Any'->new($distfile->stringify) || die($!)
'Archive::Any'->new($distfile->stringify)
Truth table0%0/2
A
A= 'Archive::Any'->new($distfile->stringify)
Aresult
11
00
167 0 $archive->extract($dir);
168  
169 0 $self->in_dir($dir,$dist->distvname);
170 };
171 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
172 0 say $@;
173 }
174 0 rmtree($dir);
175 }
176  
177 # remove old data files
178 0 foreach my $del (keys %old_files) {
179 0 ▶ unlink($out->file($del)) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $out->file($del) or die $! 0 0 0
180 }
181  
182 }
183  
184  
185 0 ▶sub in_INC {
Subroutine in_INC: not called CC=2 SCAR=17.9
Pod documented
186 0 my $self=shift;
187 0 foreach my $dir (@INC) {
188 0 $self->in_dir($dir,"INC_$dir");
189 }
190 }
191  
192  
193 0 ▶sub in_dir {
Subroutine in_dir: not called CC=8 SCAR=42.8
Pod documented
194 0 my ($self,$dir,$dumpname)=@_;
195 0 ▶ $dumpname ||= $dir;
Condition0%0/3
$dumpname ||= $dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$dumpname ||= $dir
$dumpname $dir
Truth table0%0/3
A ||= B
A= $dumpnameB= $dir
ABresult
1X1
011
000
196 0 $dumpname=~s/\//_/g;
197  
198 0 ▶ say $dumpname unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
199  
200 0 $self->interesting([]);
201 0 $self->bad([]);
202 0 my @pms;
203 0 ▶ find(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
204 0 ▶ return unless /\.pm\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.pm\z/ 0 0 0
205 0 ▶ return if $File::Find::name=~/\/x?t\//;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ m[/x?t/] 0 0 0
206 0 ▶ return if $File::Find::name=~/\/inc\//;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ m[/inc/] 0 0 0
207 0 push(@pms,$File::Find::name);
208 0 },$dir);
209  
210 0 foreach my $pm (@pms) {
211 0 $self->in_file($pm);
212 }
213  
214 0 my $dump=Path::Class::Dir->new($self->dump_to)->file($dumpname.".json");
215 0 ▶ if ($self->interesting && @{$self->interesting}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->interesting and @{$self->interesting;}) { } 0 0 0
elsif ($self->bad and @{$self->bad;}) { } 0 0 0
Condition0%0/3
$self->interesting and @{$self->interesting;}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->interesting and @{$self->interesting;}
$self->interesting @{$self->interesting;}
Truth table0%0/3
A and B
A= $self->interestingB= @{$self->interesting;}
ABresult
0X0
100
111
216 0 $dump->spew(iomode => '>:encoding(UTF-8)', $self->json_encoder->encode($self->interesting));
217 }
218 0 ▶ elsif ($self->bad && @{$self->bad}) {
Condition0%0/3
$self->bad and @{$self->bad;}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->bad and @{$self->bad;}
$self->bad @{$self->bad;}
Truth table0%0/3
A and B
A= $self->badB= @{$self->bad;}
ABresult
0X0
100
111
219 0 $dump->spew(iomode => '>:encoding(UTF-8)', $self->json_encoder->encode($self->bad));
220 }
221 else {
222 0 $dump->spew('{"is_boring":"1"}');
223 }
224 }
225  
226  
227 8 ▶sub in_file {
Subroutine in_file: called CC=2 SCAR=7.9
Pod documented
228 8 my ($self,$file)=@_;
229  
230 8 eval { $self->waste_some_cycles($file) };
231 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
232 0 push (@{$self->failed},{file=>$file,error=>$@});
233 }
234 }
235  
236 4 "let's return a strange value from Riga";
237  
238 __END__
« lib/Acme/ReturnValue/MakeSite.pm ↑ Summary script/acme_returnvalue.pl »