lib/Types/Standard/HashRef.pm

filter: Statementstmt 100% 110 / 111 (1 uncoverable) Branchbran 82.6% 38 / 46 Conditioncond 58.3% 7 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 22 / 22 Podpod n/a totaltotal 91.4% 180 / 198 (1 uncoverable) CC 31 SCAR 15.0
CC
31
Cov
92%
_exporter_fail
11.6
my_hashref_allows_value
4.0
__ANON__
4.0
CRAP 4.5
« lib/Types/Standard/StrMatch.pm ↑ Summary blib/lib/Error/TypeTiny/Assertion.pm »
1 # INTERNAL MODULE: guts for HashRef type from Types::Standard.
2  
3 package Types::Standard::HashRef;
4  
5 22 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 $Types::Standard::HashRef::AUTHORITY = 'cpan:TOBYINK';
11 22 $Types::Standard::HashRef::VERSION = '2.010001';
12 }
13  
14 22 $Types::Standard::HashRef::VERSION =~ tr/_//d;
15  
16 22 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use Types::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
21  
22 22 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 22 our @ISA = qw( Exporter::Tiny );
24  
25 2 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=10 SCAR=24.5
26 2 my ( $class, $type_name, $values, $globals ) = @_;
27 2 my $caller = $globals->{into};
28
29 2 ▶ my $of = exists( $values->{of} ) ? $values->{of} : $values->{type};
Branch: 1 branch
BranchTrueFalseTotal
exists $values->{'of'} ? : 1 1 2
30 2 ▶ defined $of or _croak( qq{Expected option "of" for type "$type_name"} );
Branch: 1 branch
BranchTrueFalseTotal
defined $of or &_croak(qq[Expected option "of" for type "$type_name"]) 2 0 2
31 2 ▶ if ( not Types::TypeTiny::is_TypeTiny($of) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Types::TypeTiny::is_TypeTiny($of)) 1 1 2
32 1 require Type::Utils;
33 1 $of = Type::Utils::dwim_type( $of, for => $caller );
34 }
35
36 2 my $type = Types::Standard::HashRef->of( $of );
37 $type = $type->create_child_type(
38 name => $type_name,
39 $type->has_coercion ? ( coercion => 1 ) : (),
40 2 ▶ exists( $values->{where} ) ? ( constraint => $values->{where} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$type->has_coercion ? : 0 2 2
exists $values->{'where'} ? : 0 2 2
41 );
42
43 $INC{'Type/Registry.pm'}
44 ? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
45 : ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
46 2 ▶ unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $caller or $caller eq "-lexical" or $globals->{"lexical"} 2 0 2
$INC{'Type/Registry.pm'} ? : 1 1 2
Condition33%2/6
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
l!l&&r!l&&!r
002
ref $caller or $caller eq "-lexical"
l!l&&r!l&&!r
002
MC/DC0%0/3
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
ref $caller $caller eq "-lexical" $globals->{"lexical"}
Truth table25%1/4
A or B or C
A= ref $callerB= $caller eq "-lexical"C= $globals->{"lexical"}
ABCresult
1XX1
01X1
0011
0000
47 2 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
48 }
49  
50 22 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 43 ▶sub __constraint_generator {
Subroutine __constraint_generator: called CC=4 SCAR=13.9
53 43 ▶ return Types::Standard::HashRef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 43 43
54
55 43 require Error::TypeTiny::WrongNumberOfParameters;
56 43 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Standard', 'HashRef', \@_, 1 );
57 43 my $param = shift;
58 43 ▶ Types::TypeTiny::is_TypeTiny( $param )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($param) or &_croak("Parameter to HashRef[`a] expected to be a type constraint; got $param") 40 3 43
59 or _croak(
60 "Parameter to HashRef[`a] expected to be a type constraint; got $param" );
61
62 40 my $param_compiled_check = $param->compiled_check;
63 40 my $xsub;
64 40 if ( Type::Tiny::_USE_XS ) {
65 40 my $paramname = Type::Tiny::XS::is_known( $param_compiled_check );
66 40 ▶ $xsub = Type::Tiny::XS::get_coderef_for( "HashRef[$paramname]" )
Branch: 1 branch
BranchTrueFalseTotal
if $paramname 29 11 40
67 if $paramname;
68 }
69 elsif ( Type::Tiny::_USE_MOUSE and $param->_has_xsub ) {
70 require Mouse::Util::TypeConstraints;
71 my $maker = "Mouse::Util::TypeConstraints"->can( "_parameterize_HashRef_for" );
72 $xsub = $maker->( $param ) if $maker;
73 }
74
75 return (
76 116 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
77 116 my $hash = shift;
78 116 ▶ $param->check( $_ ) || return for values %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$param->check($_) or return 65 17 82
79 99 return !!1;
80 },
81 40 $xsub,
82 );
83 } #/ sub __constraint_generator
84  
85 40 ▶sub __inline_generator {
Subroutine __inline_generator: called CC=3 SCAR=11.0
86 40 my $param = shift;
87
88 40 my $compiled = $param->compiled_check;
89 40 my $xsubname;
90 40 ▶ if ( Type::Tiny::_USE_XS and not $Type::Tiny::AvoidCallbacks ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Type::Tiny::AvoidCallbacks) 37 3 40
91 37 my $paramname = Type::Tiny::XS::is_known( $compiled );
92 37 $xsubname = Type::Tiny::XS::get_subname_for( "HashRef[$paramname]" );
93 }
94
95 40 ▶ return unless $param->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $param->can_be_inlined 2 38 40
96 340 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
97 340 my $v = $_[1];
98 340 ▶ return "$xsubname\($v\)" if $xsubname && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsubname and not $Type::Tiny::AvoidCallbacks 217 123 340
Condition100%3/3
$xsubname and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
7152217
MC/DC100%2/2
$xsubname and not $Type::Tiny::AvoidCallbacks
$xsubname not $Type::Tiny::AvoidCallbacks
Truth table100%3/3
A and not(B)
A= $xsubnameB= not $Type::Tiny::AvoidCallbacks
ABresult
0X0
100
111
99 123 my $p = Types::Standard::HashRef->inline_check( $v );
100 123 my $param_check = $param->inline_check( '$i' );
101
102 123 "$p and do { "
103 . "my \$ok = 1; "
104 . "for my \$i (values \%{$v}) { "
105 . "(\$ok = 0, last) unless $param_check " . "}; " . "\$ok " . "}";
106 38 };
107 } #/ sub __inline_generator
108  
109 2 ▶sub __deep_explanation {
Subroutine __deep_explanation: called CC=3 SCAR=11.0
110 2 require B;
111 2 my ( $type, $value, $varname ) = @_;
112 2 my $param = $type->parameters->[0];
113
114 2 for my $k ( sort keys %$value ) {
115 4 my $item = $value->{$k};
116 4 ▶ next if $param->check( $item );
Branch: 1 branch
BranchTrueFalseTotal
if $param->check($item) 2 2 4
117 return [
118 sprintf( '"%s" constrains each value in the hash with "%s"', $type, $param ),
119 @{
120 2 $param->validate_explain(
121 $item, sprintf( '%s->{%s}', $varname, B::perlstring( $k ) )
122 )
123 },
124 ];
125 } #/ for my $k ( sort keys %$value)
126
127 # This should never happen...
128 0 return; # uncoverable statement
129 } #/ sub __deep_explanation
130  
131 30 ▶sub __coercion_generator {
Subroutine __coercion_generator: called CC=4 SCAR=13.9
132 30 my ( $parent, $child, $param ) = @_;
133 30 ▶ return unless $param->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
unless $param->has_coercion 18 12 30
134
135 12 my $coercable_item = $param->coercion->_source_type_union;
136 12 my $C = "Type::Coercion"->new( type_constraint => $child );
137
138 12 ▶ if ( $param->coercion->can_be_inlined and $coercable_item->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->coercion->can_be_inlined and $coercable_item->can_be_inlined) { } 6 6 12
Condition66%2/3
$param->coercion->can_be_inlined and $coercable_item->can_be_inlined
!ll&&!rl&&r
606
MC/DC50%1/2
$param->coercion->can_be_inlined and $coercable_item->can_be_inlined
$param->coercion->can_be_inlined $coercable_item->can_be_inlined
Truth table66%2/3
A and B
A= $param->coercion->can_be_inlinedB= $coercable_item->can_be_inlined
ABresult
0X0
100
111
139 $C->add_type_coercions(
140 6 ▶ $parent => Types::Standard::Stringable {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 6 my @code;
142 6 push @code, 'do { my ($orig, $return_orig, %new) = ($_, 0);';
143 6 push @code, 'for (keys %$orig) {';
144 6 push @code,
145 sprintf(
146 '$return_orig++ && last unless (%s);',
147 $coercable_item->inline_check( '$orig->{$_}' )
148 );
149 6 push @code,
150 sprintf(
151 '$new{$_} = (%s);',
152 $param->coercion->inline_coercion( '$orig->{$_}' )
153 );
154 6 push @code, '}';
155 6 push @code, '$return_orig ? $orig : \\%new';
156 6 push @code, '}';
157 6 "@code";
158 }
159 6 );
160 } #/ if ( $param->coercion->...)
161 else {
162 $C->add_type_coercions(
163 12 ▶ $parent => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
164 12 ▶ my $value = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 0 12
165 12 my %new;
166 12 for my $k ( keys %$value ) {
167 33 ▶ return $value unless $coercable_item->check( $value->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless $coercable_item->check($value->{$k}) 1 32 33
168 32 $new{$k} = $param->coerce( $value->{$k} );
169 }
170 11 return \%new;
171 },
172 6 );
173 } #/ else [ if ( $param->coercion->...)]
174
175 12 return $C;
176 } #/ sub __coercion_generator
177  
178 19 ▶sub __hashref_allows_key {
Subroutine my_hashref_allows_key: called CC=1 SCAR=0.0
179 19 my $self = shift;
180 19 Types::Standard::is_Str( $_[0] );
181 }
182  
183 6 ▶sub __hashref_allows_value {
Subroutine my_hashref_allows_value: called CC=4 SCAR=13.9
184 6 my $self = shift;
185 6 my ( $key, $value ) = @_;
186
187 6 ▶ return !!0 unless $self->my_hashref_allows_key( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->my_hashref_allows_key($key) 1 5 6
188 5 ▶ return !!1 if $self == Types::Standard::HashRef();
Branch: 1 branch
BranchTrueFalseTotal
if $self == &Types::Standard::HashRef() 2 3 5
189
190 my $href = $self->find_parent(
191 3 ▶ sub { $_->has_parent && $_->parent == Types::Standard::HashRef() } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->has_parent and $_->parent == &Types::Standard::HashRef() 3 0 3
192 3 my $param = $href->type_parameter;
193
194 3 ▶ Types::Standard::is_Str( $key ) and $param->check( $value );
Branch: 1 branch
BranchTrueFalseTotal
Types::Standard::is_Str($key) and $param->check($value) 3 0 3
195 } #/ sub __hashref_allows_value
196  
197 22 1;
198  
199 __END__
« lib/Types/Standard/StrMatch.pm ↑ Summary blib/lib/Error/TypeTiny/Assertion.pm »