Pod/Coverage/More/Types.pm

filter: Statementstmt 100% 77 / 77 Branchbran 94.4% 51 / 54 Conditioncond 93.7% 15 / 16 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 97.0% 166 / 171 CC 35 SCAR 26.4
CC
35
Cov
97%
type_of_value
22.0
type_of_usage
11.0
type_named
4.0
CRAP 14.0
« Pod/Coverage/More/Analyzer.pm ↑ Summary Pod/Coverage/More/Declared/MOP.pm »
1 # ABSTRACT: The Type::Tiny constraints that the evidence of an analysis amounts to
2 # PODNAME: Pod::Coverage::More::Types
3  
4 package Pod::Coverage::More::Types 0.001;
5  
6 21 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use Exporter qw{import};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Type::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Type::Tiny::Intersection;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use Type::Tiny::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use Types::Standard qw{Any ArrayRef Bool CodeRef GlobRef HasMethods HashRef InstanceOf Int Maybe Num Object Ref ScalarRef Str Undef};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 21 our @EXPORT_OK = qw{type_of_value type_of_usage type_named};
17  
18  
19 322 ▶sub type_of_value {
Subroutine type_of_value: called CC=22 SCAR=30.9
Pod documented
20 322 my ($evidence) = @_;
21 322 my $type = $evidence->{type};
22  
23 322 ▶ if ( $type eq 'number' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 87 235 322
24 87 my $value = $evidence->{value};
25 87 ▶ return Maybe [Int] if $evidence->{integer} && $evidence->{maybe_undef};
Branch: 1 branch
BranchTrueFalseTotal
if $evidence->{"integer"} and $evidence->{"maybe_undef"} 2 85 87
Condition100%3/3
$evidence->{"integer"} and $evidence->{"maybe_undef"}
!ll&&!rl&&r
7872
MC/DC100%2/2
$evidence->{"integer"} and $evidence->{"maybe_undef"}
$evidence->{"integer"} $evidence->{"maybe_undef"}
Truth table100%3/3
A and B
A= $evidence->{"integer"}B= $evidence->{"maybe_undef"}
ABresult
0X0
100
111
26 85 ▶ return Int if $evidence->{integer};
Branch: 1 branch
BranchTrueFalseTotal
if $evidence->{"integer"} 7 78 85
27 78 ▶ return ( defined $value && $value =~ m/\A-?\d+\z/ ) ? Int : Num;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && $value =~ /\A-?\d+\z/aasx ? : 47 31 78
Condition100%3/3
defined $value and $value =~ /\A-?\d+\z/aasx
!ll&&!rl&&r
30147
MC/DC100%2/2
defined $value and $value =~ /\A-?\d+\z/aasx
defined $value $value =~ /\A-?\d+\z/aasx
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\A-?\d+\z/aasx
ABresult
0X0
100
111
28 }
29 235 ▶ return Str if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 22 213 235
30 213 ▶ return Bool if $type eq 'boolean' || $type eq 'context';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" or $type eq "context" 1 212 213
Condition66%2/3
$type eq "boolean" or $type eq "context"
l!l&&r!l&&!r
10212
MC/DC50%1/2
$type eq "boolean" or $type eq "context"
$type eq "boolean" $type eq "context"
Truth table66%2/3
A or B
A= $type eq "boolean"B= $type eq "context"
ABresult
1X1
011
000
31  
32 # An empty return gives undef to a caller that wants one value.
33 212 ▶ return Undef if $type eq 'undef' || $type eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "undef" or $type eq "empty" 82 130 212
Condition100%3/3
$type eq "undef" or $type eq "empty"
l!l&&r!l&&!r
1171130
MC/DC100%2/2
$type eq "undef" or $type eq "empty"
$type eq "undef" $type eq "empty"
Truth table100%3/3
A or B
A= $type eq "undef"B= $type eq "empty"
ABresult
1X1
011
000
34  
35 130 ▶ return HashRef if $type eq 'hashref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hashref" 10 120 130
36 120 ▶ return ArrayRef if $type eq 'arrayref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "arrayref" 21 99 120
37 99 ▶ return CodeRef if $type eq 'coderef';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "coderef" 0 99 99
38 99 ▶ return Ref if $type eq 'ref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ref" 0 99 99
39 99 ▶ if ( $type eq 'object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "object") 27 72 99
40 27 ▶ my @classes = @{ $evidence->{classes} || [] };
Condition100%2/2
$evidence->{'classes'} || []
l!l
225
MC/DC100%1/1
$evidence->{'classes'} || []
$evidence->{'classes'}
Truth table100%2/2
A
A= $evidence->{'classes'}
Aresult
11
00
41 27 ▶ return InstanceOf [ $classes[0] ] if @classes == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @classes == 1 1 26 27
42 26 ▶ if ( @classes > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@classes > 1) 1 25 26
43 1 my @instances;
44 1 foreach my $class (@classes) {
45 2 push @instances, InstanceOf [$class];
46 }
47 1 return Type::Tiny::Union->new( type_constraints => \@instances );
48 }
49 25 ▶ return defined $evidence->{class} ? InstanceOf [ $evidence->{class} ] : Object;
Branch: 1 branch
BranchTrueFalseTotal
defined $evidence->{'class'} ? : 22 3 25
50 }
51 72 return;
52 }
53  
54  
55 276 ▶sub type_of_usage {
Subroutine type_of_usage: called CC=11 SCAR=24.0
Pod documented
56 276 my ($usage) = @_;
57 276 my @alternatives;
58 276 ▶ push @alternatives, HashRef if $usage->{hashref};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"hashref"} 12 264 276
59 276 ▶ push @alternatives, ArrayRef if $usage->{arrayref};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"arrayref"} 5 271 276
60 276 ▶ push @alternatives, CodeRef if $usage->{coderef};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"coderef"} 2 274 276
61  
62 # Perl makes a string of a number, so a variable used as both is a string.
63 276 ▶ if ( $usage->{string} ) { push @alternatives, Str }
Branch: 2 branches
BranchTrueFalseTotal
if ($usage->{'string'}) { } 25 251 276
elsif ($usage->{'numeric'}) { } 38 213 251
64 38 elsif ( $usage->{numeric} ) { push @alternatives, Num }
65  
66 276 my $type;
67 276 ▶ $type = $alternatives[0] if @alternatives == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @alternatives == 1 80 196 276
68 276 ▶ $type = Type::Tiny::Union->new( type_constraints => \@alternatives ) if @alternatives > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @alternatives > 1 1 275 276
69  
70 276 ▶ my @methods = @{ $usage->{methods} || [] };
Condition100%2/2
$usage->{'methods'} || []
l!l
7269
MC/DC100%1/1
$usage->{'methods'} || []
$usage->{'methods'}
Truth table100%2/2
A
A= $usage->{'methods'}
Aresult
11
00
71 276 ▶ return $type if !@methods;
Branch: 1 branch
BranchTrueFalseTotal
unless @methods 269 7 276
72 7 my $duck = HasMethods [@methods];
73 7 ▶ return $duck if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 6 1 7
74 1 return Type::Tiny::Intersection->new( type_constraints => [ $type, $duck ] );
75 }
76  
77 # The type words of the POD that Pod::Coverage::More has always read, in lower case.
78 21 our %POD_WORDS = (
79 scalar => Any,
80 mixed => Any,
81 string => Str,
82 integer => Int,
83 float => Num,
84 boolean => Bool,
85 object => Object,
86 scalarref => ScalarRef,
87 arrayref => ArrayRef,
88 hashref => HashRef,
89 coderef => CodeRef,
90 typeglobref => GlobRef,
91 array => ArrayRef,
92 hash => HashRef,
93 );
94  
95  
96 235 ▶sub type_named {
Subroutine type_named: called CC=4 SCAR=13.9
Pod documented
97 235 my ($name) = @_;
98 235 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 234 235
99 234 my $word = $POD_WORDS{ lc $name };
100 234 ▶ return $word if $word;
Branch: 1 branch
BranchTrueFalseTotal
if $word 86 148 234
101  
102 148 my $registry = Type::Registry->for_class(__PACKAGE__);
103 148 ▶ $registry->add_types('Types::Standard') if !$registry->simple_lookup('Str');
Branch: 1 branch
BranchTrueFalseTotal
unless $registry->simple_lookup("Str") 0 148 148
104 148 my $type = eval { $registry->lookup($name) };
105 148 return $type;
106 }
107  
108 21 1;
109  
110 __END__
« Pod/Coverage/More/Analyzer.pm ↑ Summary Pod/Coverage/More/Declared/MOP.pm »