TypeTiny/Check.pm

filter: Statementstmt 100% 87 / 87 Branchbran 95.8% 46 / 48 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 5 totaltotal 95.5% 150 / 157 CC 28 SCAR 19.8
CC
28
Cov
99%
check_rule
13.0
no_check_rule
8.0
check_type
5.0
CRAP 7.3
« TypeTiny.pm ↑ Summary
1 package Smart::Args::TypeTiny::Check;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Type::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 our @EXPORT_OK = qw/check_rule no_check_rule check_type type type_role/;
11  
12 17 $Carp::CarpInternal{+__PACKAGE__}++;
13  
14 17 my $reg = Type::Registry->for_class(__PACKAGE__);
15  
16 103 ▶sub check_rule {
Subroutine check_rule: called CC=13 SCAR=25.7
Pod undocumented
17 103 my ($rule, $value, $exists, $name) = @_;
18  
19 103 ▶ if (ref $rule eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq 'HASH') { } 38 65 103
20 38 my %check = map { ($_ => undef) } keys %$rule;
21 38 delete $check{$_} for qw/isa does optional default/;
22 38 ▶ if (%check) {
Branch: 1 branch
BranchTrueFalseTotal
if (%check) 1 37 38
23 1 Carp::croak("Malformed rule for '$name' (isa, does, optional, default)");
24 }
25 } else {
26 65 $rule = {isa => $rule};
27 }
28  
29 102 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) { } 84 18 102
30 84 ▶ return $value if !defined $value && $rule->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $rule->{"optional"} 3 81 84
Condition100%3/3
not defined $value and $rule->{"optional"}
!ll&&!rl&&r
7923
MC/DC100%2/2
not defined $value and $rule->{"optional"}
not defined $value $rule->{"optional"}
Truth table100%3/3
not(A) and B
A= not defined $valueB= $rule->{"optional"}
ABresult
0X0
100
111
31 } else {
32 18 ▶ if (exists $rule->{default}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rule->{'default'}) { } 8 10 18
elsif (not $rule->{'optional'}) { } 6 4 10
33 8 my $default = $rule->{default};
34 8 ▶ $value = ref $default eq 'CODE' ? scalar $default->() : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 4 4 8
35 } elsif (!$rule->{optional}) {
36 6 Carp::confess("Required parameter '$name' not passed");
37 } else {
38 4 return $value;
39 }
40 }
41  
42 89 my $type;
43 89 ▶ if (exists $rule->{isa}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rule->{'isa'}) { } 87 2 89
elsif (exists $rule->{'does'}) { } 2 0 2
44 87 $type = type($rule->{isa});
45 } elsif (exists $rule->{does}) {
46 2 $type = type_role($rule->{does});
47 }
48  
49 89 ($value, my $ok) = check_type($type, $value, $name);
50 89 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 22 67 89
51 22 Carp::confess("Type check failed in binding to parameter '\$$name'; " . $type->get_message($value));
52 }
53  
54 67 return $value;
55 }
56  
57 # Functions without type checking for better performance
58 25 ▶sub no_check_rule {
Subroutine no_check_rule: called CC=8 SCAR=20.8
Pod undocumented
59 25 my ($rule, $value, $exists, $name) = @_;
60  
61 25 ▶ if (ref $rule eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq 'HASH') { } 15 10 25
62 15 my %check = map { ($_ => undef) } keys %$rule;
63 15 delete $check{$_} for qw/isa does optional default/;
64 15 ▶ if (%check) {
Branch: 1 branch
BranchTrueFalseTotal
if (%check) 1 14 15
65 1 Carp::croak("Malformed rule for '$name' (isa, does, optional, default)");
66 }
67 } else {
68 10 $rule = {isa => $rule};
69 }
70  
71 24 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) { } 17 7 24
72 17 return $value;
73 } else {
74 7 ▶ if (exists $rule->{default}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rule->{'default'}) { } 3 4 7
elsif (not $rule->{'optional'}) { } 2 2 4
75 3 my $default = $rule->{default};
76 3 ▶ return ref $default eq 'CODE' ? scalar $default->() : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 1 2 3
77 } elsif (!$rule->{optional}) {
78 2 Carp::confess("Required parameter '$name' not passed");
79 } else {
80 2 return $value;
81 }
82 }
83 }
84  
85 93 ▶sub check_type {
Subroutine check_type: called CC=5 SCAR=16.1
Pod undocumented
86 93 my ($type, $value) = @_;
87 93 ▶ return ($value, 1) unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 92 93
88 92 ▶ return ($value, 1) if $type->check($value);
Branch: 1 branch
BranchTrueFalseTotal
if $type->check($value) 66 26 92
89  
90 26 ▶ if ($type->has_coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 4 22 26
91 4 my $coerced_value = $type->coerce($value);
92 4 ▶ if ($type->check($coerced_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->check($coerced_value)) 3 1 4
93 3 return ($coerced_value, 1);
94 }
95 }
96  
97 23 return ($value, 0);
98 }
99  
100 92 ▶sub type {
Subroutine type: called CC=3 SCAR=11.0
Pod undocumented
101 92 my ($type_name) = @_;
102 92 ▶ return $type_name if blessed($type_name);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($type_name) 69 23 92
103 23 ▶ if (my $type = $reg->simple_lookup($type_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $reg->simple_lookup($type_name)) { } 10 13 23
104 10 return $type;
105 } else {
106 13 my $type = Type::Utils::dwim_type(
107 $type_name,
108 fallback => ['lookup_via_mouse', 'make_class_type'],
109 );
110 13 $type->{display_name} = $type_name;
111 13 $reg->add_type($type, $type_name);
112 13 return $type;
113 }
114 }
115  
116 3 ▶sub type_role {
Subroutine type_role: called CC=3 SCAR=11.0
Pod undocumented
117 3 my ($type_name) = @_;
118 3 ▶ return $type_name if blessed($type_name);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($type_name) 0 3 3
119 3 ▶ if (my $type = $reg->simple_lookup($type_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $reg->simple_lookup($type_name)) { } 1 2 3
120 1 return $type;
121 } else {
122 2 my $type = Type::Utils::dwim_type(
123 $type_name,
124 fallback => ['make_role_type'],
125 );
126 2 $type->{display_name} = $type_name;
127 2 $reg->add_type($type, $type_name);
128 2 return $type;
129 }
130 }
131  
132 17 1;
« TypeTiny.pm ↑ Summary