| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 30 | 84 | ▶ | return $value if !defined $value && $rule->{optional}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value and $rule->{"optional"}
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"}
| |||||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 18 | ▶ | if (exists $rule->{default}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 33 | 8 | my $default = $rule->{default}; | |||||||||||||||||||||||||||
| 34 | 8 | ▶ | $value = ref $default eq 'CODE' ? scalar $default->() : $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 17 | return $value; | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 7 | ▶ | if (exists $rule->{default}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 3 | my $default = $rule->{default}; | |||||||||||||||||||||||||||
| 76 | 3 | ▶ | return ref $default eq 'CODE' ? scalar $default->() : $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 92 | ▶ | return ($value, 1) if $type->check($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 26 | ▶ | if ($type->has_coercion) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 4 | my $coerced_value = $type->coerce($value); | |||||||||||||||||||||||||||
| 92 | 4 | ▶ | if ($type->check($coerced_value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 23 | ▶ | if (my $type = $reg->simple_lookup($type_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if (my $type = $reg->simple_lookup($type_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||