lib/Pod/Coverage/More/Types.pm

filter: Statementstmt 100% 78 / 78 Branchbran 94.6% 53 / 56 Conditioncond 93.7% 15 / 16 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 97.1% 169 / 174 CC 36 SCAR 26.3
CC
36
Cov
97%
type_of_value
22.0
type_of_usage
11.0
type_named
5.0
CRAP 13.9
« lib/Pod/Coverage/More/Analyzer.pm ↑ Summary lib/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.003;
5  
6 25 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use warnings FATAL => q{all};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use re q{/xsaa};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 ▶use Exporter qw{import};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Type::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Type::Tiny::Intersection;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Type::Tiny::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶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 25 our @EXPORT_OK = qw{type_of_value type_of_usage type_named};
17  
18  
19 405 ▶sub type_of_value {
Subroutine type_of_value: called CC=22 SCAR=30.9
Pod documented
20 405 my ($evidence) = @_;
21 405 my $type = $evidence->{type};
22  
23 405 ▶ if ( $type eq 'number' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 94 311 405
24 94 my $value = $evidence->{value};
25 94 ▶ return Maybe [Int] if $evidence->{integer} && $evidence->{maybe_undef};
Branch: 1 branch
BranchTrueFalseTotal
if $evidence->{"integer"} and $evidence->{"maybe_undef"} 2 92 94
Condition100%3/3
$evidence->{"integer"} and $evidence->{"maybe_undef"}
!ll&&!rl&&r
8572
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 92 ▶ return Int if $evidence->{integer};
Branch: 1 branch
BranchTrueFalseTotal
if $evidence->{"integer"} 7 85 92
27 85 ▶ return ( defined $value && $value =~ m/\A-?\d+\z/ ) ? Int : Num;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && $value =~ /\A-?\d+\z/aasx ? : 51 34 85
Condition100%3/3
defined $value and $value =~ /\A-?\d+\z/aasx
!ll&&!rl&&r
33151
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 311 ▶ return Str if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 27 284 311
30 284 ▶ return Bool if $type eq 'boolean' || $type eq 'context';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" or $type eq "context" 1 283 284
Condition66%2/3
$type eq "boolean" or $type eq "context"
l!l&&r!l&&!r
10283
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 283 ▶ return Undef if $type eq 'undef' || $type eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "undef" or $type eq "empty" 109 174 283
Condition100%3/3
$type eq "undef" or $type eq "empty"
l!l&&r!l&&!r
1198174
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 174 ▶ return HashRef if $type eq 'hashref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hashref" 10 164 174
36 164 ▶ return ArrayRef if $type eq 'arrayref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "arrayref" 31 133 164
37 133 ▶ return CodeRef if $type eq 'coderef';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "coderef" 0 133 133
38 133 ▶ return Ref if $type eq 'ref';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ref" 0 133 133
39 133 ▶ if ( $type eq 'object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "object") 27 106 133
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 106 return;
52 }
53  
54  
55 351 ▶sub type_of_usage {
Subroutine type_of_usage: called CC=11 SCAR=24.0
Pod documented
56 351 my ($usage) = @_;
57 351 my @alternatives;
58 351 ▶ push @alternatives, HashRef if $usage->{hashref};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"hashref"} 35 316 351
59 351 ▶ push @alternatives, ArrayRef if $usage->{arrayref};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"arrayref"} 13 338 351
60 351 ▶ push @alternatives, CodeRef if $usage->{coderef};
Branch: 1 branch
BranchTrueFalseTotal
if $usage->{"coderef"} 2 349 351
61  
62 # Perl makes a string of a number, so a variable used as both is a string.
63 351 ▶ if ( $usage->{string} ) { push @alternatives, Str }
Branch: 2 branches
BranchTrueFalseTotal
if ($usage->{'string'}) { } 30 321 351
elsif ($usage->{'numeric'}) { } 41 280 321
64 41 elsif ( $usage->{numeric} ) { push @alternatives, Num }
65  
66 351 my $type;
67 351 ▶ $type = $alternatives[0] if @alternatives == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @alternatives == 1 119 232 351
68 351 ▶ $type = Type::Tiny::Union->new( type_constraints => \@alternatives ) if @alternatives > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @alternatives > 1 1 350 351
69  
70 351 ▶ my @methods = @{ $usage->{methods} || [] };
Condition100%2/2
$usage->{'methods'} || []
l!l
15336
MC/DC100%1/1
$usage->{'methods'} || []
$usage->{'methods'}
Truth table100%2/2
A
A= $usage->{'methods'}
Aresult
11
00
71 351 ▶ return $type if !@methods;
Branch: 1 branch
BranchTrueFalseTotal
unless @methods 336 15 351
72 15 my $duck = HasMethods [@methods];
73 15 ▶ return $duck if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 14 1 15
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 25 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 343 ▶sub type_named {
Subroutine type_named: called CC=5 SCAR=16.1
Pod documented
97 343 my ($name) = @_;
98 343 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 342 343
99 342 my $word = $POD_WORDS{ lc $name };
100 342 ▶ return $word if $word;
Branch: 1 branch
BranchTrueFalseTotal
if $word 106 236 342
101  
102 # No type expression has a %, and the error of a failed lookup takes NAME as a printf format, which warns.
103 236 ▶ return if index( $name, q{%} ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($name, "%") != -1 1 235 236
104  
105 235 my $registry = Type::Registry->for_class(__PACKAGE__);
106 235 ▶ $registry->add_types('Types::Standard') if !$registry->simple_lookup('Str');
Branch: 1 branch
BranchTrueFalseTotal
unless $registry->simple_lookup("Str") 0 235 235
107 235 my $type = eval { $registry->lookup($name) };
108 235 return $type;
109 }
110  
111 25 1;
112  
113 __END__
« lib/Pod/Coverage/More/Analyzer.pm ↑ Summary lib/Pod/Coverage/More/Declared/MOP.pm »