blib/lib/Data/Sah/Util/Type.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 67.5% 27 / 40 Conditioncond 64.8% 24 / 37 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 86.6% 13 / 15 Podpod 100% 6 / 6 totaltotal 76.3% 145 / 190 CC 36 SCAR 18.0
CC
36
Cov
79%
is_numeric
8.7
is_ref
8.7
is_simple
8.3
CRAP 6.1
↑ Summary
1 package Data::Sah::Util::Type;
2  
3 2 our $DATE = '2016-12-09'; # DATE
4 2 our $VERSION = '0.46'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 require Exporter;
11 2 our @ISA = qw(Exporter);
12 2 our @EXPORT_OK = qw(get_type is_type is_simple is_numeric is_collection is_ref);
13  
14 # XXX absorb and use metadata from Data::Sah::Type::*
15 2 our $type_metas = {
16 all => {scalar=>0, numeric=>0, ref=>0},
17 any => {scalar=>0, numeric=>0, ref=>0},
18 array => {scalar=>0, numeric=>0, ref=>1},
19 bool => {scalar=>1, numeric=>0, ref=>0},
20 buf => {scalar=>1, numeric=>0, ref=>0},
21 cistr => {scalar=>1, numeric=>0, ref=>0},
22 code => {scalar=>1, numeric=>0, ref=>1},
23 float => {scalar=>1, numeric=>1, ref=>0},
24 hash => {scalar=>0, numeric=>0, ref=>1},
25 int => {scalar=>1, numeric=>1, ref=>0},
26 num => {scalar=>1, numeric=>1, ref=>0},
27 obj => {scalar=>1, numeric=>0, ref=>1},
28 re => {scalar=>1, numeric=>0, ref=>1, simple=>1},
29 str => {scalar=>1, numeric=>0, ref=>0},
30 undef => {scalar=>1, numeric=>0, ref=>0},
31 date => {scalar=>1, numeric=>0, ref=>0},
32 duration => {scalar=>1, numeric=>0, ref=>0},
33 };
34  
35 82 ▶sub get_type {
Subroutine get_type: called CC=4 SCAR=14.8
Pod documented
36 82 my $sch = shift;
37  
38 82 ▶ if (ref($sch) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sch eq "ARRAY") 24 58 82
39 24 $sch = $sch->[0];
40 }
41  
42 82 ▶ if (defined($sch) && !ref($sch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sch and not ref $sch) { } 82 0 82
Condition33%1/3
defined $sch and not ref $sch
!ll&&!rl&&r
0082
MC/DC0%0/2
defined $sch and not ref $sch
defined $sch not ref $sch
Truth table33%1/3
A and not(B)
A= defined $schB= not ref $sch
ABresult
0X0
100
111
43 82 $sch =~ s/\*\z//;
44 82 return $sch;
45 } else {
46 0 return undef;
47 }
48 }
49  
50 24 ▶sub _normalize {
Subroutine _normalize: called CC=2 SCAR=7.0
51 24 require Data::Sah::Normalize;
52  
53 24 my ($sch, $opts) = @_;
54 24 ▶ return $sch if $opts->{schema_is_normalized};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"schema_is_normalized"} 0 24 24
55 24 return Data::Sah::Normalize::normalize_schema($sch);
56 }
57  
58 # for any|all to pass a criteria, we assume that all of the schemas in the 'of'
59 # clause must also pass (and there must not be '!of', 'of&', or that kind of
60 # thing.
61 24 ▶sub _handle_any_all {
Subroutine _handle_any_all: called CC=7 SCAR=19.5
62 24 my ($sch, $opts, $crit) = @_;
63 24 $sch = _normalize($sch, $opts);
64 24 ▶ return 0 if $sch->[1]{'of.op'};
Branch: 1 branch
BranchTrueFalseTotal
if $sch->[1]{"of.op"} 8 16 24
65 16 my $of = $sch->[1]{of};
66 16 ▶ return 0 unless $of && ref($of) eq 'ARRAY' && @$of;
Branch: 1 branch
BranchTrueFalseTotal
unless $of and ref $of eq "ARRAY" and @$of 8 8 16
Condition83%5/6
$of and ref $of eq "ARRAY" and @$of
!ll&&!rl&&r
448
$of and ref $of eq "ARRAY"
!ll&&!rl&&r
4012
MC/DC66%2/3
$of and ref $of eq "ARRAY" and @$of
$of ref $of eq "ARRAY" @$of
Truth table75%3/4
A and B and C
A= $ofB= ref $of eq "ARRAY"C= @$of
ABCresult
0XX0
10X0
1100
1111
67 8 for (@$of) {
68 14 ▶ return 0 unless $crit->($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$crit($_) 4 10 14
69 }
70 4 1;
71 }
72  
73 3 ▶sub is_type {
Subroutine is_type: called CC=4 SCAR=14.1
Pod documented
74 3 my ($sch, $opts) = @_;
75 3 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
03
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
76  
77 3 ▶ my $type = get_type($sch) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = get_type($sch) or return undef 3 0 3
78 3 ▶ my $tmeta = $type_metas->{$type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tmeta = $Data::Sah::Util::Type::type_metas->{$type} or return undef 2 1 3
79 2 $type;
80 }
81  
82 33 ▶sub is_simple {
Subroutine is_simple: called CC=8 SCAR=21.1
Pod documented
83 33 my ($sch, $opts) = @_;
84 33 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
033
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
85  
86 33 ▶ my $type = get_type($sch) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = get_type($sch) or return undef 33 0 33
87 33 ▶ my $tmeta = $type_metas->{$type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tmeta = $Data::Sah::Util::Type::type_metas->{$type} or return undef 33 0 33
88 33 ▶ if ($type eq 'any' || $type eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "any" or $type eq "all") 12 21 33
Condition100%3/3
$type eq "any" or $type eq "all"
l!l&&r!l&&!r
6621
MC/DC100%2/2
$type eq "any" or $type eq "all"
$type eq "any" $type eq "all"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "all"
ABresult
1X1
011
000
89 12 ▶ return _handle_any_all($sch, $opts, sub { is_simple(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91 21 ▶ return $tmeta->{simple} // ($tmeta->{scalar} && !$tmeta->{ref});
Condition83%5/6
$tmeta->{"simple"} // $tmeta->{"scalar"} && !$tmeta->{"ref"}
l!l&&r!l&&!r
1200
$tmeta->{"scalar"} and not $tmeta->{"ref"}
!ll&&!rl&&r
4214
MC/DC100%3/3
$tmeta->{"simple"} // $tmeta->{"scalar"} && !$tmeta->{"ref"}
$tmeta->{"simple"} $tmeta->{"scalar"} not $tmeta->{"ref"}
Truth table100%4/4
A // B and not(C)
A= $tmeta->{"simple"}B= $tmeta->{"scalar"}C= not $tmeta->{"ref"}
ABCresult
1XX1
0111
00X0
0100
92 }
93  
94 31 ▶sub is_collection {
Subroutine is_collection: called CC=6 SCAR=18.2
Pod documented
95 31 my ($sch, $opts) = @_;
96 31 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
031
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
97  
98 31 ▶ my $type = get_type($sch) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = get_type($sch) or return undef 31 0 31
99 31 ▶ my $tmeta = $type_metas->{$type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tmeta = $Data::Sah::Util::Type::type_metas->{$type} or return undef 31 0 31
100 31 ▶ if ($type eq 'any' || $type eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "any" or $type eq "all") 12 19 31
Condition100%3/3
$type eq "any" or $type eq "all"
l!l&&r!l&&!r
6619
MC/DC100%2/2
$type eq "any" or $type eq "all"
$type eq "any" $type eq "all"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "all"
ABresult
1X1
011
000
101 12 ▶ return _handle_any_all($sch, $opts, sub { is_collection(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
102 }
103 19 return !$tmeta->{scalar};
104 }
105  
106 9 ▶sub is_numeric {
Subroutine is_numeric: called CC=6 SCAR=21.6
Pod documented
107 9 my ($sch, $opts) = @_;
108 9 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
09
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
109  
110 9 ▶ my $type = get_type($sch) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = get_type($sch) or return undef 9 0 9
111 9 ▶ my $tmeta = $type_metas->{$type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tmeta = $Data::Sah::Util::Type::type_metas->{$type} or return undef 9 0 9
112 9 ▶ if ($type eq 'any' || $type eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "any" or $type eq "all") 0 9 9
Condition33%1/3
$type eq "any" or $type eq "all"
l!l&&r!l&&!r
009
MC/DC0%0/2
$type eq "any" or $type eq "all"
$type eq "any" $type eq "all"
Truth table33%1/3
A or B
A= $type eq "any"B= $type eq "all"
ABresult
1X1
011
000
113 0 ▶ return _handle_any_all($sch, $opts, sub { is_numeric(shift) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 }
115 9 return $tmeta->{numeric};
116 }
117  
118 6 ▶sub is_ref {
Subroutine is_ref: called CC=6 SCAR=21.6
Pod documented
119 6 my ($sch, $opts) = @_;
120 6 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
06
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
121  
122 6 ▶ my $type = get_type($sch) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = get_type($sch) or return undef 6 0 6
123 6 ▶ my $tmeta = $type_metas->{$type} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tmeta = $Data::Sah::Util::Type::type_metas->{$type} or return undef 6 0 6
124 6 ▶ if ($type eq 'any' || $type eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "any" or $type eq "all") 0 6 6
Condition33%1/3
$type eq "any" or $type eq "all"
l!l&&r!l&&!r
006
MC/DC0%0/2
$type eq "any" or $type eq "all"
$type eq "any" $type eq "all"
Truth table33%1/3
A or B
A= $type eq "any"B= $type eq "all"
ABresult
1X1
011
000
125 0 ▶ return _handle_any_all($sch, $opts, sub { is_ref(shift) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 }
127 6 return $tmeta->{ref};
128 }
129  
130 2 1;
131 # ABSTRACT: Utility functions related to types
132  
133 __END__
↑ Summary