| 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
| |||||||||||||
| 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
| |||||||||||||
| 21 | 9 | ▶ | GetOptionsFromArray(\@_, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 36 | |||||||||||||
| 37 | 9 | my %args; | |||||||||||
| 38 | 9 | ▶ | %args = %{ YAML::XS::LoadFile($spec_file) } if defined $spec_file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 9 | ▶ | push @{ $args{files} }, (@files, @{ $args{file} // [] }); | ||||||||||
|
Condition50%1/2
$args{"file"} // []
MC/DC0%0/1
$args{"file"} // []
$args{"file"}
Truth table50%1/2
A
A= $args{"file"}
| |||||||||||||
| 40 | 9 | ▶ | push @{ $args{filters} }, (@filters, @{ $args{filter} // [] }); | ||||||||||
|
Condition50%1/2
$args{"filter"} // []
MC/DC0%0/1
$args{"filter"} // []
$args{"filter"}
Truth table50%1/2
A
A= $args{"filter"}
| |||||||||||||
| 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
| |||||||||||||
| 48 | 9 | ▶ | $fh_out //= \*STDOUT; | ||||||||||
|
Condition50%1/2
$fh_out //= \*STDOUT
MC/DC0%0/1
$fh_out //= \*STDOUT
$fh_out
Truth table50%1/2
A
A= $fh_out
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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"} // []
MC/DC0%0/1
$args{"files"} // []
$args{"files"}
Truth table50%1/2
A
A= $args{"files"}
| |||||||||||||
| 88 | 18 | ▶ | my $files_ignore = $args{ignore} // []; | ||||||||||
|
Condition100%2/2
$args{"ignore"} // []
MC/DC100%1/1
$args{"ignore"} // []
$args{"ignore"}
Truth table100%2/2
A
A= $args{"ignore"}
| |||||||||||||
| 89 | 18 | ▶ | my $subs_include = $args{include} // []; | ||||||||||
|
Condition100%2/2
$args{"include"} // []
MC/DC100%1/1
$args{"include"} // []
$args{"include"}
Truth table100%2/2
A
A= $args{"include"}
| |||||||||||||
| 90 | 18 | ▶ | my $subs_exclude = $args{exclude} // []; | ||||||||||
|
Condition100%2/2
$args{"exclude"} // []
MC/DC100%1/1
$args{"exclude"} // []
$args{"exclude"}
Truth table100%2/2
A
A= $args{"exclude"}
| |||||||||||||
| 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
| |||||||||||||
| 100 | |||||||||||||
| 101 | 64 | foreach my $sub (list_subs($file, $filters)) { | |||||||||||
| 102 | 140 | ▶ | next if exists $sub_excluded{$sub}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 119 | |||||||||||||
| 120 | 164 | ▶ | if ($sub->name =~ /'|::/) { # qualified | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 135 | 184 | ▶ | return $elem if $elem->isa('PPI::Statement::Package'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | |||||||||||||
| 137 | 178 | my $prev = $elem; | |||||||||||
| 138 | 178 | ▶ | while ($prev = $prev->sprevious_sibling) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 282 | ▶ | return $prev if $prev->isa('PPI::Statement::Package'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | } | ||||||||||||
| 141 | 32 | return _detect_package($elem->parent); | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 2 | 1; | |||||||||||
| 145 | __END__ | ||||||||||||