bin/opentracing_wrapscope_generator

filter: Statementstmt 100% 127 / 127 Branchbran 76.3% 29 / 38 Conditioncond 71.4% 10 / 14 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 24 / 24 Podpod n/a totaltotal 91.9% 193 / 210 CC 31 SCAR 16.7
CC
31
Cov
93%
run
10.8
examine_files
9.0
list_subs
6.0
CRAP 5.3
↑ Summary blib/lib/OpenTracing/WrapScope.pm »
1 #!/usr/bin/env perl
2 package OpenTracing::WrapScope::ConfigGenerator;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature qw/say state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Getopt::Long qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::MoreUtils qw/notall/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl::Critic::Utils::McCabe qw/calculate_mccabe_of_sub/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Pod::Usage qw/pod2usage/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶exit run(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 2 2
18  
19 9 ▶sub run {
Subroutine run: called CC=10 SCAR=23.8
20 9 ▶ pod2usage -verbose => 1, -noperldoc => 1 if not @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 9 9
21 9 ▶ GetOptionsFromArray(\@_,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "spec=s", \my $spec_file, "file=s", \my @files, "ignore=s", \my @ignore, "include=s", \my @include, "exclude=s", \my @exclude, "filter=s", \my @filters, "out=s", \my $output_file, "help", \my $help) or pod2usage("-verbose", 1, "-noperldoc", 1, "-msg", "Invalid options") 9 0 9
22 'spec=s' => \my $spec_file,
23 'file=s' => \my @files,
24 'ignore=s' => \my @ignore,
25 'include=s' => \my @include,
26 'exclude=s' => \my @exclude,
27 'filter=s' => \my @filters,
28 'out=s' => \my $output_file,
29 'help' => \my $help,
30 ) or pod2usage (
31 -verbose => 1,
32 -noperldoc => 1,
33 -msg => 'Invalid options',
34 );
35 9 ▶ pod2usage -verbose => 1, -noperldoc => 1 if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 9 9
36  
37 9 my %args;
38 9 ▶ %args = %{ YAML::XS::LoadFile($spec_file) } if defined $spec_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec_file 9 0 9
39 9 ▶ push @{ $args{files} }, (@files, @{ $args{file} // [] });
Condition50%1/2
$args{"file"} // []
l!l
09
MC/DC0%0/1
$args{"file"} // []
$args{"file"}
Truth table50%1/2
A
A= $args{"file"}
Aresult
11
00
40 9 ▶ push @{ $args{filters} }, (@filters, @{ $args{filter} // [] });
Condition50%1/2
$args{"filter"} // []
l!l
09
MC/DC0%0/1
$args{"filter"} // []
$args{"filter"}
Truth table50%1/2
A
A= $args{"filter"}
Aresult
11
00
41 9 push @{ $args{ignore} }, @ignore;
42 9 push @{ $args{include} }, @include;
43 9 push @{ $args{exclude} }, @exclude;
44  
45 9 my @subs = OpenTracing::WrapScope::ConfigGenerator::examine_files(%args);
46  
47 9 ▶ open my $fh_out, '>', $output_file if $output_file;
Branch: 1 branch
BranchTrueFalseTotal
if $output_file 9 0 9
48 9 ▶ $fh_out //= \*STDOUT;
Condition50%1/2
$fh_out //= \*STDOUT
l!l
90
MC/DC0%0/1
$fh_out //= \*STDOUT
$fh_out
Truth table50%1/2
A
A= $fh_out
Aresult
11
00
49  
50 9 say {$fh_out} $_ foreach @subs;
51  
52 9 return 0;
53 }
54  
55 18 ▶sub _generate_filters {
Subroutine _generate_filters: called CC=4 SCAR=13.9
56 18 my ($filter_specs) = @_;
57 18 ▶ return if not $filter_specs;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter_specs 7 11 18
58  
59 state $GENERATORS = {
60 2 ▶ exclude_private => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 18 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 18 my ($sub) = @_;
63 18 return $sub->name !~ /(?:\A|::)_\w+\z/;
64 2 };
65 },
66 2 ▶ complexity => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
67 2 my ($threshold) = @_;
68 2 ▶ croak 'No arguments for complexity filter' if not $threshold;
Branch: 1 branch
BranchTrueFalseTotal
unless $threshold 0 2 2
69 12 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 12 my ($sub) = @_;
71 12 return calculate_mccabe_of_sub($sub) >= $threshold;
72 2 };
73 },
74 11 };
75  
76 11 my @filters;
77 11 foreach (@$filter_specs) {
78 4 my ($filter, $arg) = split /=/;
79 4 ▶ my $generator = $GENERATORS->{$filter} or croak "No such filter: $_";
Branch: 1 branch
BranchTrueFalseTotal
my $generator = $GENERATORS->{$filter} or croak("No such filter: $_") 4 0 4
80 4 push @filters, $generator->($arg);
81 }
82 11 return \@filters;
83 }
84  
85 18 ▶sub examine_files {
Subroutine examine_files: called CC=9 SCAR=22.0
86 18 my %args = @_;
87 18 ▶ my $files_base = $args{files} // [];
Condition50%1/2
$args{"files"} // []
l!l
180
MC/DC0%0/1
$args{"files"} // []
$args{"files"}
Truth table50%1/2
A
A= $args{"files"}
Aresult
11
00
88 18 ▶ my $files_ignore = $args{ignore} // [];
Condition100%2/2
$args{"ignore"} // []
l!l
126
MC/DC100%1/1
$args{"ignore"} // []
$args{"ignore"}
Truth table100%2/2
A
A= $args{"ignore"}
Aresult
11
00
89 18 ▶ my $subs_include = $args{include} // [];
Condition100%2/2
$args{"include"} // []
l!l
117
MC/DC100%1/1
$args{"include"} // []
$args{"include"}
Truth table100%2/2
A
A= $args{"include"}
Aresult
11
00
90 18 ▶ my $subs_exclude = $args{exclude} // [];
Condition100%2/2
$args{"exclude"} // []
l!l
108
MC/DC100%1/1
$args{"exclude"} // []
$args{"exclude"}
Truth table100%2/2
A
A= $args{"exclude"}
Aresult
11
00
91 18 my $filters = _generate_filters($args{filters});
92  
93 2 ▶ my @files = grep { -f } map { glob } @$files_base;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 18 my %file_ignored = map { $_ => undef } map { glob } @$files_ignore;
95 18 my %sub_excluded = map { $_ => undef } @$subs_exclude;
96  
97 18 my @subs = @$subs_include;
98 18 foreach my $file (@files) {
99 76 ▶ next if exists $file_ignored{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $file_ignored{$file} 12 64 76
100  
101 64 foreach my $sub (list_subs($file, $filters)) {
102 140 ▶ next if exists $sub_excluded{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sub_excluded{$sub} 4 136 140
103 136 push @subs, $sub;
104 }
105 }
106 18 return uniq @subs;
107 }
108  
109 69 ▶sub list_subs {
Subroutine list_subs: called CC=6 SCAR=17.9
110 69 my ($filename, $filters) = @_;
111  
112 69 my $doc = PPI::Document->new($filename);
113 69 my $subs = $doc->find('PPI::Statement::Sub');
114 69 ▶ return if not $subs;
Branch: 1 branch
BranchTrueFalseTotal
unless $subs 0 69 69
115  
116 69 my @subs;
117 69 foreach my $sub (@$subs) {
118 176 ▶ next if notall { $_->($sub) } @$filters;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &notall(sub { &$_($sub); } , @$filters) 12 164 176
119  
120 164 ▶ if ($sub->name =~ /'|::/) { # qualified
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->name =~ /'|::/) { } 2 162 164
121 2 push @subs, $sub->name;
122 }
123 else {
124 162 my $pkg = _detect_package($sub);
125 162 ▶ $pkg = $pkg ? $pkg->namespace : 'main';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 152 10 162
126 162 push @subs, $pkg . '::' . $sub->name;
127 }
128 }
129 69 return @subs;
130 }
131  
132 194 ▶sub _detect_package {
Subroutine _detect_package: called CC=5 SCAR=16.1
133 194 my ($elem) = @_;
134 194 ▶ return unless $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 10 184 194
135 184 ▶ return $elem if $elem->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Package") 6 178 184
136  
137 178 my $prev = $elem;
138 178 ▶ while ($prev = $prev->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev = $prev->sprevious_sibling) 282 32 314
139 282 ▶ return $prev if $prev->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Statement::Package") 146 136 282
140 }
141 32 return _detect_package($elem->parent);
142 }
143  
144 2 1;
145 __END__
↑ Summary blib/lib/OpenTracing/WrapScope.pm »