Guess/Role/Tiny.pm

filter: Statementstmt 93.6% 44 / 47 Branchbran 78.5% 11 / 14 Conditioncond 66.6% 8 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 1 totaltotal 81.5% 75 / 92 CC 14 SCAR 14.7
CC
14
Cov
86%
_type
7.3
__ANON__
6.0
class_opts
3.0
CRAP 4.4
« Guess/Role/DateTime/Naive.pm ↑ Summary Guess/Role/DateTime.pm »
1 package Type::Guess::Role::Tiny;
2  
3 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use MooX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Type::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Types::Standard qw( Int Num Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶has "name" => ( is => "rw", default => sub { "" } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 0 ▶has "type_tiny" => ( is => "rw", default => sub { Str } ); # whats this for?
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 0 ▶class_has "types" => ( is => "rw", default => sub { [ Int, Num, Str, ] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 # print $class_opts;
16  
17 3 our $class_opts = { tolerance => 0, skip_empty => 1, encoding => "", types => [ Int, Num, Str, ] };
18  
19 105 ▶sub class_opts {
Subroutine class_opts: called CC=3 SCAR=11.0
Pod undocumented
20 105 my ($class, $opt, $val) = @_;
21 105 ▶ die sprintf "Invalid option %s\n" unless exists $class_opts->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Type::Guess::Role::Tiny::class_opts->{$opt} 0 105 105
22 105 ▶ $class_opts->{$opt} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 6 99 105
23 105 return $class_opts->{$opt}
24 }
25  
26 38 ▶around "new" => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
27 38 my $orig = shift;
28 38 my $self = shift;
29 38 ▶ if (scalar @_ > 1 && !ref $_[0] && ref $_[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1 and not ref $_[0] and ref $_[-1]) 6 32 38
Condition83%5/6
scalar @_ > 1 and not ref $_[0] and ref $_[-1]
!ll&&!rl&&r
21116
scalar @_ > 1 and not ref $_[0]
!ll&&!rl&&r
21017
MC/DC66%2/3
scalar @_ > 1 and not ref $_[0] and ref $_[-1]
scalar @_ > 1 not ref $_[0] ref $_[-1]
Truth table75%3/4
A and not(B) and C
A= scalar @_ > 1B= not ref $_[0]C= ref $_[-1]
ABCresult
0XX0
10X0
1100
1111
30 6 my $opts = pop @_;
31 6 for (keys $opts->%*) {
32 6 ▶ $self->class_opts($_, $opts->{$_}) if $class_opts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $Type::Guess::Role::Tiny::class_opts->{$_} 6 0 6
33 }
34 }
35 38 my $ret = $orig->($self, @_);
36 38 return $ret
37 3 };
38  
39 3 our $types = [ Int, Num, Str, ];
40  
41 20 ▶sub _type {
Subroutine _type: called CC=7 SCAR=19.9
42 20 my $class = shift;
43  
44 my @vals =
45 82 ▶ map { looks_like_number($_) && int($_) == $_ ? int($_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) && int $_ == $_ ? : 51 31 82
Condition100%3/3
&looks_like_number($_) and int $_ == $_
!ll&&!rl&&r
24751
MC/DC100%2/2
&looks_like_number($_) and int $_ == $_
&looks_like_number($_) int $_ == $_
Truth table100%3/3
A and B
A= &looks_like_number($_)B= int $_ == $_
ABresult
0X0
100
111
46 20 map { s/(\d\.*\d*)%$/$1/r }
47 @_;
48  
49 20 my $tot = scalar @vals;
50 20 for ($class->class_opts("types")->@*) {
51  
52 42 ▶ my $tiny_type = ref $_ ? $_ : eval $_ || eval '$' . $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 42 0 42
Condition0%0/3
eval $_ or eval '$' . $_
l!l&&r!l&&!r
000
MC/DC0%0/2
eval $_ or eval '$' . $_
eval $_ eval '$' . $_
Truth table0%0/3
A or B
A= eval $_B= eval '$' . $_
ABresult
1X1
011
000
53  
54 42 my $ok = scalar $tiny_type->grep(@vals);
55 42 ▶ if ($ok / $tot >= (1 - $class->tolerance)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok / $tot >= 1 - $class->tolerance) 20 22 42
56 20 return $tiny_type
57 }
58 }
59 }
60  
61 3 1;
62  
« Guess/Role/DateTime/Naive.pm ↑ Summary Guess/Role/DateTime.pm »