lib/Types/Standard/ArrayRef.pm

filter: Statementstmt 93.4% 128 / 138 (1 uncoverable) Branchbran 81.4% 70 / 86 Conditioncond 50.0% 18 / 36 MC/DCmcdc 28.5% 6 / 21 Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 80.5% 243 / 303 (1 uncoverable) CC 62 SCAR 23.1
CC
62
Cov
83%
__constraint_generator
22.2
__ANON__
20.0
__deep_explanation
11.3
CRAP 10.0
« lib/Type/Parser.pm ↑ Summary blib/lib/Type/Library.pm »
1 # INTERNAL MODULE: guts for ArrayRef type from Types::Standard.
2  
3 package Types::Standard::ArrayRef;
4  
5 95 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 95 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 95 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 95 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 95 $Types::Standard::ArrayRef::AUTHORITY = 'cpan:TOBYINK';
11 95 $Types::Standard::ArrayRef::VERSION = '2.010001';
12 }
13  
14 95 $Types::Standard::ArrayRef::VERSION =~ tr/_//d;
15  
16 95 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 95 ▶use Types::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 95 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
21  
22 95 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 95 our @ISA = qw( Exporter::Tiny );
24  
25 3 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=10 SCAR=24.0
26 3 my ( $class, $type_name, $values, $globals ) = @_;
27 3 my $caller = $globals->{into};
28
29 3 ▶ my $of = exists( $values->{of} ) ? $values->{of} : $values->{type};
Branch: 1 branch
BranchTrueFalseTotal
exists $values->{'of'} ? : 2 1 3
30 3 ▶ 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"]) 3 0 3
31 3 ▶ if ( not Types::TypeTiny::is_TypeTiny($of) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Types::TypeTiny::is_TypeTiny($of)) 1 2 3
32 1 require Type::Utils;
33 1 $of = Type::Utils::dwim_type( $of, for => $caller );
34 }
35
36 3 my $type = Types::Standard::ArrayRef->of( $of );
37 $type = $type->create_child_type(
38 name => $type_name,
39 $type->has_coercion ? ( coercion => 1 ) : (),
40 3 ▶ exists( $values->{where} ) ? ( constraint => $values->{where} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$type->has_coercion ? : 0 3 3
exists $values->{'where'} ? : 1 2 3
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 3 ▶ unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $caller or $caller eq "-lexical" or $globals->{"lexical"} 3 0 3
$INC{'Type/Registry.pm'} ? : 2 1 3
Condition33%2/6
ref $caller or $caller eq "-lexical" or $globals->{"lexical"}
l!l&&r!l&&!r
003
ref $caller or $caller eq "-lexical"
l!l&&r!l&&!r
003
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 3 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
48 }
49  
50 95 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 177 ▶sub __constraint_generator {
Subroutine __constraint_generator: called CC=17 SCAR=31.0
53 177 ▶ return Types::Standard::ArrayRef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 175 177
54
55 175 Type::Tiny::check_parameter_count_for_parameterized_type( 'Types::Standard', 'ArrayRef', \@_, 3 );
56 175 my $param = shift;
57 175 ▶ Types::TypeTiny::is_TypeTiny( $param )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($param) or &_croak("Parameter to ArrayRef[`a] expected to be a type constraint; got $param") 173 2 175
58 or _croak(
59 "Parameter to ArrayRef[`a] expected to be a type constraint; got $param" );
60
61 173 my ( $min, $max ) = ( 0, -1 );
62 173 ▶ $min = Types::Standard::assert_Int( shift ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 164 173
63 173 ▶ $max = Types::Standard::assert_Int( shift ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 169 173
64
65 173 my $param_compiled_check = $param->compiled_check;
66 173 my $xsub;
67 173 ▶ if ( Type::Tiny::_USE_XS and $min == 0 and $max == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($min == 0 and $max == -1) { } 164 9 173
elsif (&Type::Tiny::_USE_MOUSE() and $param->_has_xsub and $min == 0 and $max == -1) { } 0 18 18
Condition66%2/3
$min == 0 and $max == -1
!ll&&!rl&&r
90164
MC/DC50%1/2
$min == 0 and $max == -1
$min == 0 $max == -1
Truth table66%2/3
A and B
A= $min == 0B= $max == -1
ABresult
0X0
100
111
68 164 my $paramname = Type::Tiny::XS::is_known( $param_compiled_check );
69 164 ▶ $xsub = Type::Tiny::XS::get_coderef_for( "ArrayRef[$paramname]" )
Branch: 1 branch
BranchTrueFalseTotal
if $paramname 134 30 164
70 if $paramname;
71 }
72 ▶ elsif ( Type::Tiny::_USE_MOUSE
Condition33%3/9
&Type::Tiny::_USE_MOUSE() and $param->_has_xsub and $min == 0 and $max == -1
!ll&&!rl&&r
900
&Type::Tiny::_USE_MOUSE() and $param->_has_xsub and $min == 0
!ll&&!rl&&r
900
&Type::Tiny::_USE_MOUSE() and $param->_has_xsub
!ll&&!rl&&r
900
MC/DC0%0/4
&Type::Tiny::_USE_MOUSE() and $param->_has_xsub and $min == 0 and $max == -1
&Type::Tiny::_USE_MOUSE() $param->_has_xsub $min == 0 $max == -1
Truth table20%1/5
A and B and C and D
A= &Type::Tiny::_USE_MOUSE()B= $param->_has_xsubC= $min == 0D= $max == -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
73 and $param->_has_xsub
74 and $min == 0
75 and $max == -1 )
76 {
77 0 require Mouse::Util::TypeConstraints;
78 0 my $maker = "Mouse::Util::TypeConstraints"->can( "_parameterize_ArrayRef_for" );
79 0 ▶ $xsub = $maker->( $param ) if $maker;
Branch: 1 branch
BranchTrueFalseTotal
if $maker 0 0 0
80 }
81
82 return (
83 209 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
84 209 my $array = shift;
85 209 ▶ $param->check( $_ ) || return for @$array;
Branch: 1 branch
BranchTrueFalseTotal
$param->check($_) or return 545 63 608
86 146 return !!1;
87 },
88 173 ▶ $xsub,
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 and $max == -1 164 9 173
Condition66%2/3
$min == 0 and $max == -1
!ll&&!rl&&r
90164
MC/DC50%1/2
$min == 0 and $max == -1
$min == 0 $max == -1
Truth table66%2/3
A and B
A= $min == 0B= $max == -1
ABresult
0X0
100
111
89 ) if $min == 0 and $max == -1;
90
91 22 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
92 22 my $array = shift;
93 22 ▶ return if @$array < $min;
Branch: 1 branch
BranchTrueFalseTotal
if @$array < $min 10 12 22
94 12 ▶ $param->check( $_ ) || return for @$array;
Branch: 1 branch
BranchTrueFalseTotal
$param->check($_) or return 43 3 46
95 9 return !!1;
96 }
97 9 ▶ if $max == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $max == -1 5 4 9
98
99 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
100 0 my $array = shift;
101 0 ▶ return if @$array > $max;
Branch: 1 branch
BranchTrueFalseTotal
if @$array > $max 0 0 0
102 0 ▶ $param->check( $_ ) || return for @$array;
Branch: 1 branch
BranchTrueFalseTotal
$param->check($_) or return 0 0 0
103 0 return !!1;
104 }
105 4 ▶ if $min == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 0 4 4
106
107 10 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
108 10 my $array = shift;
109 10 ▶ return if @$array < $min;
Branch: 1 branch
BranchTrueFalseTotal
if @$array < $min 2 8 10
110 8 ▶ return if @$array > $max;
Branch: 1 branch
BranchTrueFalseTotal
if @$array > $max 3 5 8
111 5 ▶ $param->check( $_ ) || return for @$array;
Branch: 1 branch
BranchTrueFalseTotal
$param->check($_) or return 11 2 13
112 3 return !!1;
113 4 };
114 } #/ sub __constraint_generator
115  
116 173 ▶sub __inline_generator {
Subroutine __inline_generator: called CC=6 SCAR=17.9
117 173 my $param = shift;
118 173 my ( $min, $max ) = ( 0, -1 );
119 173 ▶ $min = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 164 173
120 173 ▶ $max = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 169 173
121
122 173 my $param_compiled_check = $param->compiled_check;
123 173 my $xsubname;
124 173 ▶ if ( Type::Tiny::_USE_XS and $min == 0 and $max == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == 0 and $max == -1) 164 9 173
Condition66%2/3
$min == 0 and $max == -1
!ll&&!rl&&r
90164
MC/DC50%1/2
$min == 0 and $max == -1
$min == 0 $max == -1
Truth table66%2/3
A and B
A= $min == 0B= $max == -1
ABresult
0X0
100
111
125 164 my $paramname = Type::Tiny::XS::is_known( $param_compiled_check );
126 164 $xsubname = Type::Tiny::XS::get_subname_for( "ArrayRef[$paramname]" );
127 }
128
129 173 ▶ return unless $param->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $param->can_be_inlined 10 163 173
130
131 1346 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
132 1346 my $v = $_[1];
133 1346 ▶ return "$xsubname\($v\)" if $xsubname && !$Type::Tiny::AvoidCallbacks;
Branch: 1 branch
BranchTrueFalseTotal
if $xsubname and not $Type::Tiny::AvoidCallbacks 952 394 1346
Condition100%3/3
$xsubname and not $Type::Tiny::AvoidCallbacks
!ll&&!rl&&r
289105952
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
134 394 my $p = Types::Standard::ArrayRef->inline_check( $v );
135
136 394 ▶ if ( $min != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min != 0) 106 288 394
137 106 $p .= sprintf( ' and @{%s} >= %d', $v, $min );
138 }
139 394 ▶ if ( $max > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > 0) 36 358 394
140 36 $p .= sprintf( ' and @{%s} <= %d', $v, $max );
141 }
142
143 394 my $param_check = $param->inline_check( '$i' );
144 394 ▶ return $p if $param->{uniq} eq Types::Standard::Any->{uniq};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"uniq"} eq &Types::Standard::Any()->{"uniq"} 4 390 394
145
146 390 "$p and do { "
147 . "my \$ok = 1; "
148 . "for my \$i (\@{$v}) { "
149 . "(\$ok = 0, last) unless $param_check " . "}; " . "\$ok " . "}";
150 163 };
151 } #/ sub __inline_generator
152  
153 3 ▶sub __deep_explanation {
Subroutine __deep_explanation: called CC=9 SCAR=24.2
154 3 my ( $type, $value, $varname ) = @_;
155 3 my $param = $type->parameters->[0];
156 3 my ( $min, $max ) = ( 0, -1 );
157 3 ▶ $min = $type->parameters->[1] if @{ $type->parameters } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$type->parameters;} > 1 0 3 3
158 3 ▶ $max = $type->parameters->[2] if @{ $type->parameters } > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$type->parameters;} > 2 0 3 3
159
160 3 ▶ if ( $min != 0 and @$value < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min != 0 and @$value < $min) 0 3 3
Condition33%1/3
$min != 0 and @$value < $min
!ll&&!rl&&r
300
MC/DC0%0/2
$min != 0 and @$value < $min
$min != 0 @$value < $min
Truth table33%1/3
A and B
A= $min != 0B= @$value < $min
ABresult
0X0
100
111
161 return [
162 0 sprintf( '"%s" constrains array length at least %s', $type, $min ),
163 sprintf( '@{%s} is %d', $varname, scalar @$value ),
164 ];
165 }
166
167 3 ▶ if ( $max > 0 and @$value > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > 0 and @$value > $max) 0 3 3
Condition33%1/3
$max > 0 and @$value > $max
!ll&&!rl&&r
300
MC/DC0%0/2
$max > 0 and @$value > $max
$max > 0 @$value > $max
Truth table33%1/3
A and B
A= $max > 0B= @$value > $max
ABresult
0X0
100
111
168 return [
169 0 sprintf( '"%s" constrains array length at most %d', $type, $max ),
170 sprintf( '@{%s} is %d', $varname, scalar @$value ),
171 ];
172 }
173
174 3 for my $i ( 0 .. $#$value ) {
175 7 my $item = $value->[$i];
176 7 ▶ next if $param->check( $item );
Branch: 1 branch
BranchTrueFalseTotal
if $param->check($item) 4 3 7
177 return [
178 sprintf( '"%s" constrains each value in the array with "%s"', $type, $param ),
179 3 @{ $param->validate_explain( $item, sprintf( '%s->[%d]', $varname, $i ) ) },
180 ];
181 }
182
183 # This should never happen...
184 0 return; # uncoverable statement
185 } #/ sub __deep_explanation
186  
187 # XXX: min and max need to be handled by coercion?
188 55 ▶sub __coercion_generator {
Subroutine __coercion_generator: called CC=4 SCAR=13.9
189 55 my ( $parent, $child, $param ) = @_;
190 55 ▶ return unless $param->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
unless $param->has_coercion 30 25 55
191
192 25 my $coercable_item = $param->coercion->_source_type_union;
193 25 my $C = "Type::Coercion"->new( type_constraint => $child );
194
195 25 ▶ 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) { } 11 14 25
Condition66%2/3
$param->coercion->can_be_inlined and $coercable_item->can_be_inlined
!ll&&!rl&&r
14011
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
196 $C->add_type_coercions(
197 11 ▶ $parent => Types::Standard::Stringable {
Subroutine __ANON__: called CC=1 SCAR=0.0
198 11 my @code;
199 11 push @code, 'do { my ($orig, $return_orig, @new) = ($_, 0);';
200 11 push @code, 'for (@$orig) {';
201 11 push @code,
202 sprintf(
203 '++$return_orig && last unless (%s);',
204 $coercable_item->inline_check( '$_' )
205 );
206 11 push @code,
207 sprintf(
208 'push @new, (%s);',
209 $param->coercion->inline_coercion( '$_' )
210 );
211 11 push @code, '}';
212 11 push @code, '$return_orig ? $orig : \\@new';
213 11 push @code, '}';
214 11 "@code";
215 }
216 11 );
217 } #/ if ( $param->coercion->...)
218 else {
219 $C->add_type_coercions(
220 72 ▶ $parent => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
221 72 ▶ my $value = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 72 0 72
222 72 my @new;
223 72 for my $item ( @$value ) {
224 280 ▶ return $value unless $coercable_item->check( $item );
Branch: 1 branch
BranchTrueFalseTotal
unless $coercable_item->check($item) 1 279 280
225 279 push @new, $param->coerce( $item );
226 }
227 71 return \@new;
228 },
229 14 );
230 } #/ else [ if ( $param->coercion->...)]
231
232 25 return $C;
233 } #/ sub __coercion_generator
234  
235 95 1;
236  
237 __END__
« lib/Type/Parser.pm ↑ Summary blib/lib/Type/Library.pm »