ValidateSubs/Role.pm

filter: Statementstmt 100% 69 / 69 Branchbran 100% 30 / 30 Conditioncond 70.8% 34 / 48 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 82.6% 148 / 179 CC 34 SCAR 33.4
CC
34
Cov
90%
_validate_sub
34.7
_default
2.0
BEGIN
1.0
CRAP 28.2
↑ Summary ValidateSubs.pm »
1 package MooX::ValidateSubs::Role;
2  
3 10 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Type::Utils qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Type::Params qw/compile compile_named/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Types::Standard qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 76 ▶sub _validate_sub {
Subroutine _validate_sub: called CC=33 SCAR=35.5
10 76 my ( $self, $name, $type, $spec, @params ) = @_;
11 76 my $store_spec = sprintf '%s_spec', $name;
12  
13 76 ▶ my $compiled_check = ($self->$store_spec->{"compiled_$type"} ||= do {
Condition66%2/3
$self->$store_spec->{"compiled_$type"} ||= do { if (ref $spec eq "ARRAY") { my(@types) = map({() = (@$_, 0); $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint;} @$spec); compile(@types); } else { my %types; foreach my $key (keys %$spec) { () = (@{$spec->{$key};}, 0); $types{$key} = $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint; } ; compile_named(%types); } }
l!l&&r!l&&!r
48280
MC/DC0%0/2
$self->$store_spec->{"compiled_$type"} ||= do { if (ref $spec eq "ARRAY") { my(@types) = map({() = (@$_, 0); $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint;} @$spec); compile(@types); } else { my %types; foreach my $key (keys %$spec) { () = (@{$spec->{$key};}, 0); $types{$key} = $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint; } ; compile_named(%types); } }
$self->$store_spec->{"compiled_$type"} do { if (ref $spec eq "ARRAY") { my(@types) = map({() = (@$_, 0); $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint;} @$spec); compile(@types); } else { my %types; foreach my $key (keys %$spec) { () = (@{$spec->{$key};}, 0); $types{$key} = $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint; } ; compile_named(%types); } }
Truth table66%2/3
A ||= B
A= $self->$store_spec->{"compiled_$type"}B= do { if (ref $spec eq "ARRAY") { my(@types) = map({() = (@$_, 0); $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint;} @$spec); compile(@types); } else { my %types; foreach my $key (keys %$spec) { () = (@{$spec->{$key};}, 0); $types{$key} = $default eq 1 ? &Types::Standard::Optional()->of($constraint) : $constraint; } ; compile_named(%types); } }
ABresult
1X1
011
000
14 28 ▶ if (ref $spec eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 12 16 28
15 my @types = map {
16 12 my ($constraint, $default) = (@$_, 0);
17 33 ▶ $default eq '1' ? Types::Standard::Optional->of($constraint) : $constraint;
Branch: 1 branch
BranchTrueFalseTotal
$default eq '1' ? : 2 31 33
18 } @$spec;
19 12 compile(@types);
20 }
21 else {
22 16 my %types;
23 16 for my $key (keys %$spec) {
24 56 my ($constraint, $default) = (@{$spec->{$key}}, 0);
25 56 ▶ $types{$key} =
Branch: 1 branch
BranchTrueFalseTotal
$default eq '1' ? : 3 53 56
26 $default eq '1' ? Types::Standard::Optional->of($constraint) : $constraint;
27 }
28 16 compile_named(%types);
29 }
30 });
31  
32 76 my @count = ( scalar @params );
33 76 ▶ if ( ref $spec eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq "ARRAY") 36 40 76
34 36 push @count, scalar @{$spec};
35  
36 36 ▶ @params = $self->_preprocess_params(@params) and $count[0] = scalar @params if ( do {
Branch: 1 branch
BranchTrueFalseTotal
if do { my $preprocess = $count[0]; $_ == 0 || !$_ % 2 ? $params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && (last) : (next) foreach (0 .. $count[0] - 1); $preprocess } 8 28 36
Condition66%2/3
do { my $preprocess = $count[0]; $_ == 0 || !$_ % 2 ? $params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && (last) : (next) foreach (0 .. $count[0] - 1); $preprocess } && ((@params = $self->_preprocess_params(@params)) && ($count[0] = scalar @params))
!ll&&!rl&&r
2880
MC/DC0%0/2
do { my $preprocess = $count[0]; $_ == 0 || !$_ % 2 ? $params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && (last) : (next) foreach (0 .. $count[0] - 1); $preprocess } && ((@params = $self->_preprocess_params(@params)) && ($count[0] = scalar @params))
do { my $preprocess = $count[0]; $_ == 0 || !$_ % 2 ? $params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && (last) : (next) foreach (0 .. $count[0] - 1); $preprocess } ((@params = $self->_preprocess_params(@params)) && ($count[0] = scalar @params))
Truth table66%2/3
A && B
A= do { my $preprocess = $count[0]; $_ == 0 || !$_ % 2 ? $params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && (last) : (next) foreach (0 .. $count[0] - 1); $preprocess }B= ((@params = $self->_preprocess_params(@params)) && ($count[0] = scalar @params))
ABresult
0X0
100
111
37 36 my $preprocess = $count[0];
38 36 ▶ $_ == 0 || ! $_ % 2 ? $params[$_] =~ m/[0-9]+/ && $params[$_] <= $count[1] ? next : do { $preprocess = 0 } && last : next foreach 0 .. $count[0] - 1;
Branch: 2 branches
BranchTrueFalseTotal
$_ == 0 || !$_ % 2 ? : 25 29 54
$params[$_] =~ /[0-9]+/ && $params[$_] <= $count[1] ? : 8 17 25
Condition75%6/8
$_ == 0 or !$_ % 2
l!l&&r!l&&!r
25029
$params[$_] =~ /[0-9]+/ and $params[$_] <= $count[1]
!ll&&!rl&&r
1168
do { $preprocess = 0 } and last
!ll
170
MC/DC50%1/2
$_ == 0 or !$_ % 2
$_ == 0 !$_ % 2
MC/DC100%2/2
$params[$_] =~ /[0-9]+/ and $params[$_] <= $count[1]
$params[$_] =~ /[0-9]+/ $params[$_] <= $count[1]
MC/DC0%0/1
do { $preprocess = 0 } and last
do { $preprocess = 0 }
Truth table66%2/3
A or not(B)
A= $_ == 0B= !$_ % 2
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $params[$_] =~ /[0-9]+/B= $params[$_] <= $count[1]
ABresult
0X0
100
111
Truth table50%1/2
A
A= do { $preprocess = 0 }
Aresult
00
11
39 36 ▶ $preprocess;
Condition33%1/3
@params = $self->_preprocess_params(@params) and $count[0] = scalar @params
!ll&&!rl&&r
080
MC/DC0%0/2
@params = $self->_preprocess_params(@params) and $count[0] = scalar @params
@params = $self->_preprocess_params(@params) $count[0] = scalar @params
Truth table33%1/3
A and B
A= @params = $self->_preprocess_params(@params)B= $count[0] = scalar @params
ABresult
0X0
100
111
40 } );
41  
42 36 ▶ if ( $count[0] == 1 && $count[1] != 1 and ref $params[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count[0] == 1 and $count[1] != 1 and ref $params[0] eq "ARRAY") 4 32 36
Condition100%6/6
$count[0] == 1 and $count[1] != 1 and ref $params[0] eq "ARRAY"
!ll&&!rl&&r
3114
$count[0] == 1 and $count[1] != 1
!ll&&!rl&&r
2745
MC/DC100%3/3
$count[0] == 1 and $count[1] != 1 and ref $params[0] eq "ARRAY"
$count[0] == 1 $count[1] != 1 ref $params[0] eq "ARRAY"
Truth table100%4/4
A and B and C
A= $count[0] == 1B= $count[1] != 1C= ref $params[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
43 4 @params = @{ $params[0] };
44 4 $count[0] = scalar @params;
45 4 $count[3] = 'ref';
46 }
47  
48 36 $count[2] = $count[1] - grep { $spec->[$_]->[1] } 0 .. $count[1] - 1;
49 36 ▶ $count[0] >= $count[2] && $count[0] <= $count[1]
Branch: 1 branch
BranchTrueFalseTotal
$count[0] >= $count[2] and $count[0] <= $count[1] or croak(sprintf('Error - Invalid count in %s for sub - %s - expected - %s - got - %s', $type, $name, $count[1], $count[0])) 29 7 36
Condition66%2/3
$count[0] >= $count[2] and $count[0] <= $count[1]
!ll&&!rl&&r
7029
MC/DC50%1/2
$count[0] >= $count[2] and $count[0] <= $count[1]
$count[0] >= $count[2] $count[0] <= $count[1]
Truth table66%2/3
A and B
A= $count[0] >= $count[2]B= $count[0] <= $count[1]
ABresult
0X0
100
111
50 or croak sprintf 'Error - Invalid count in %s for sub - %s - expected - %s - got - %s',
51 $type, $name, $count[1], $count[0];
52  
53 29 foreach ( 0 .. $count[1] - 1 ) {
54 82 ▶ not $params[$_] and $spec->[$_]->[1]
Branch: 1 branch
BranchTrueFalseTotal
not $params[$_] and $spec->[$_][1] and $spec->[$_][1] =~ /^1$/ && (next) || ($params[$_] = $self->_default($spec->[$_][1])) 33 49 82
Condition63%7/11
!$params[$_] && $spec->[$_][1] && ($spec->[$_][1] =~ /^1$/ && (next) || ($params[$_] = $self->_default($spec->[$_][1])))
!ll&&!rl&&r
49330
not $params[$_] and $spec->[$_][1]
!ll&&!rl&&r
49033
$spec->[$_][1] =~ /^1$/ and next or $params[$_] = $self->_default($spec->[$_][1])
l!l&&r!l&&!r
0320
$spec->[$_][1] =~ /^1$/ and next
!ll
321
MC/DC0%0/4
!$params[$_] && $spec->[$_][1] && ($spec->[$_][1] =~ /^1$/ && (next) || ($params[$_] = $self->_default($spec->[$_][1])))
not $params[$_] $spec->[$_][1] $spec->[$_][1] =~ /^1$/ $params[$_] = $self->_default($spec->[$_][1])
Truth table20%1/5
not(A) and B && C or D
A= not $params[$_]B= $spec->[$_][1]C= $spec->[$_][1] =~ /^1$/D= $params[$_] = $self->_default($spec->[$_][1])
ABCDresult
0XXX0
10XX0
11000
111X1
11011
55 and ( $spec->[$_]->[1] =~ m/^1$/ and next or $params[$_] = $self->_default( $spec->[$_]->[1] ) );
56 }
57  
58 29 @params = $compiled_check->(@params);
59 29 ▶ return defined $count[3] ? \@params : @params;
Branch: 1 branch
BranchTrueFalseTotal
defined $count[3] ? : 3 26 29
60 }
61  
62 40 ▶ my %para = $count[0] == 1 ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
$count[0] == 1 ? : 6 34 40
63 40 my %cry = ( %{$spec}, %para );
64 40 foreach ( keys %cry ) {
65 not $para{$_} and $spec->{$_}->[1]
66 139 ▶ and ( $spec->{$_}->[1] =~ m/^1$/ and next or $para{$_} = $self->_default( $spec->{$_}->[1] ) );
Branch: 1 branch
BranchTrueFalseTotal
not $para{$_} and $spec->{$_}[1] and $spec->{$_}[1] =~ /^1$/ && (next) || ($para{$_} = $self->_default($spec->{$_}[1])) 53 86 139
Condition72%8/11
!$para{$_} && $spec->{$_}[1] && ($spec->{$_}[1] =~ /^1$/ && (next) || ($para{$_} = $self->_default($spec->{$_}[1])))
!ll&&!rl&&r
86530
not $para{$_} and $spec->{$_}[1]
!ll&&!rl&&r
701653
$spec->{$_}[1] =~ /^1$/ and next or $para{$_} = $self->_default($spec->{$_}[1])
l!l&&r!l&&!r
0480
$spec->{$_}[1] =~ /^1$/ and next
!ll
485
MC/DC0%0/4
!$para{$_} && $spec->{$_}[1] && ($spec->{$_}[1] =~ /^1$/ && (next) || ($para{$_} = $self->_default($spec->{$_}[1])))
not $para{$_} $spec->{$_}[1] $spec->{$_}[1] =~ /^1$/ $para{$_} = $self->_default($spec->{$_}[1])
Truth table40%2/5
not(A) and B && C or D
A= not $para{$_}B= $spec->{$_}[1]C= $spec->{$_}[1] =~ /^1$/D= $para{$_} = $self->_default($spec->{$_}[1])
ABCDresult
0XXX0
10XX0
11000
111X1
11011
67 }
68  
69 40 my $paraRef = $compiled_check->(\%para);
70  
71 28 ▶ return $count[0] == 1 ? $paraRef : %{$paraRef};
Branch: 1 branch
BranchTrueFalseTotal
$count[0] == 1 ? : 5 23 28
72 }
73  
74 80 ▶sub _default {
Subroutine _default: called CC=2 SCAR=6.9
75 80 my ( $self, $default ) = @_;
76  
77 80 ▶ if ( ref $default eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default eq "CODE") 62 18 80
78 62 return $default->();
79 }
80 18 return $self->$default;
81 }
82  
83 8 ▶sub _preprocess_params {
Subroutine _preprocess_params: called CC=1 SCAR=0.0
84 8 my ($self, %params) = @_;
85  
86 8 my @world;
87 8 map { $world[$_] = $params{$_} } sort keys %params;
88 8 return @world;
89 }
90  
91 10 1;
92  
↑ Summary ValidateSubs.pm »