lib/Types/Standard/CycleTuple.pm

filter: Statementstmt 99.2% 134 / 136 (1 uncoverable) Branchbran 82.7% 48 / 58 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 92.1% 209 / 228 (1 uncoverable) CC 37 SCAR 15.8
CC
37
Cov
93%
_exporter_fail
8.8
__coercion_generator
8.0
__ANON__
6.4
CRAP 4.8
« blib/lib/Type/Tiny/Intersection.pm ↑ Summary lib/Types/Standard/ScalarRef.pm »
1 # INTERNAL MODULE: guts for CycleTuple type from Types::Standard.
2  
3 package Types::Standard::CycleTuple;
4  
5 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 $Types::Standard::CycleTuple::AUTHORITY = 'cpan:TOBYINK';
11 5 $Types::Standard::CycleTuple::VERSION = '2.010001';
12 }
13  
14 5 $Types::Standard::CycleTuple::VERSION =~ tr/_//d;
15  
16 5 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Types::Standard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
21  
22 5 my $_Optional = Types::Standard::Optional;
23 5 my $_arr = Types::Standard::ArrayRef;
24 5 my $_Slurpy = Types::Standard::Slurpy;
25  
26 5 ▶use Exporter::Tiny 1.004001 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 our @ISA = qw( Exporter::Tiny );
28  
29 2 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=8 SCAR=21.7
30 2 my ( $class, $type_name, $values, $globals ) = @_;
31 2 my $caller = $globals->{into};
32
33 2 my @final;
34 {
35 2 ▶ my $to_type = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
36 4 ▶ return $_[0] if Types::TypeTiny::is_TypeTiny($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Types::TypeTiny::is_TypeTiny($_[0]) 2 2 4
37 2 require Type::Utils;
38 2 Type::Utils::dwim_type( $_[0], for => 'caller' );
39 2 };
40 2 my $of = $values->{of};
41 2 ▶ Types::TypeTiny::is_ArrayLike($of)
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_ArrayLike($of) or &_croak(qq[Expected arrayref option "of" for type "$type_name"]) 2 0 2
42 or _croak( qq{Expected arrayref option "of" for type "$type_name"} );
43 2 @final = map { $to_type->($_) } @$of;
44 }
45
46 2 my $type = Types::Standard::CycleTuple->of( @final );
47 $type = $type->create_child_type(
48 name => $type_name,
49 $type->has_coercion ? ( coercion => 1 ) : (),
50 2 ▶ exists( $values->{where} ) ? ( constraint => $values->{where} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$type->has_coercion ? : 0 2 2
exists $values->{'where'} ? : 0 2 2
51 );
52
53 $INC{'Type/Registry.pm'}
54 ? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
55 : ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
56 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
57 2 return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
58 }
59  
60 5 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 5 my $cycleuniq = 0;
63  
64 14 ▶sub __constraint_generator {
Subroutine __constraint_generator: called CC=4 SCAR=13.9
65 my @params = map {
66 14 my $param = $_;
67 29 ▶ Types::TypeTiny::is_TypeTiny( $param )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($param) or &_croak("Parameters to CycleTuple[...] expected to be type constraints; got $param") 28 1 29
68 or _croak(
69 "Parameters to CycleTuple[...] expected to be type constraints; got $param" );
70 28 $param;
71 } @_;
72 13 my $count = @params;
73 13 my $tuple = Types::Standard::Tuple()->of( @params );
74
75 13 ▶ _croak( "Parameters to CycleTuple[...] cannot be optional" )
Branch: 1 branch
BranchTrueFalseTotal
if grep((!!$_->is_strictly_a_type_of($_Optional)), @params) 3 10 13
76 if grep !!$_->is_strictly_a_type_of( $_Optional ), @params;
77 10 ▶ _croak( "Parameters to CycleTuple[...] cannot be slurpy" )
Branch: 1 branch
BranchTrueFalseTotal
if grep((!!$_->is_strictly_a_type_of($_Slurpy)), @params) 2 8 10
78 if grep !!$_->is_strictly_a_type_of( $_Slurpy ), @params;
79
80 74 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
81 74 my $value = shift;
82 74 ▶ return unless $_arr->check( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $_arr->check($value) 0 74 74
83 74 ▶ return if @$value % $count;
Branch: 1 branch
BranchTrueFalseTotal
if @$value % $count 41 33 74
84 33 my $i = 0;
85 33 ▶ while ( $i < $#$value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $value->$#*) 40 30 70
86 40 my $tmp = [ @$value[ $i .. $i + $count - 1 ] ];
87 40 ▶ return unless $tuple->check( $tmp );
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple->check($tmp) 3 37 40
88 37 $i += $count;
89 }
90 30 !!1;
91 }
92 8 } #/ sub __constraint_generator
93  
94 8 ▶sub __inline_generator {
Subroutine __inline_generator: called CC=3 SCAR=11.0
95 my @params = map {
96 8 my $param = $_;
97 21 ▶ Types::TypeTiny::is_TypeTiny( $param )
Branch: 1 branch
BranchTrueFalseTotal
Types::TypeTiny::is_TypeTiny($param) or &_croak("Parameter to CycleTuple[`a] expected to be a type constraint; got $param") 21 0 21
98 or _croak(
99 "Parameter to CycleTuple[`a] expected to be a type constraint; got $param" );
100 21 $param;
101 } @_;
102 8 my $count = @params;
103 8 my $tuple = Types::Standard::Tuple()->of( @params );
104
105 8 ▶ return unless $tuple->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple->can_be_inlined 2 6 8
106
107 72 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
108 72 $cycleuniq++;
109
110 72 my $v = $_[1];
111 72 my @checks = $_arr->inline_check( $v );
112 72 ▶ push @checks, sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A\$[a-z0-9_]+\z/i ? : 58 14 72
113 'not(@%s %% %d)',
114 ( $v =~ /\A\$[a-z0-9_]+\z/i ? $v : "{$v}" ),
115 $count,
116 );
117 push @checks, sprintf(
118 'do { my $cyclecount%d = 0; my $cycleok%d = 1; while ($cyclecount%d < $#{%s}) { my $cycletmp%d = [@{%s}[$cyclecount%d .. $cyclecount%d+%d]]; unless (%s) { $cycleok%d = 0; last; }; $cyclecount%d += %d; }; $cycleok%d; }',
119 $cycleuniq,
120 $cycleuniq,
121 $cycleuniq,
122 $v,
123 $cycleuniq,
124 $v,
125 $cycleuniq,
126 $cycleuniq,
127 $count - 1,
128 $tuple->inline_check( "\$cycletmp$cycleuniq" ),
129 $cycleuniq,
130 $cycleuniq,
131 $count,
132 $cycleuniq,
133 72 ▶ ) if grep { $_->inline_check( '$xyz' ) ne '(!!1)' } @params;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->inline_check("\$xyz") ne "(!!1)";} @params 72 0 72
134 72 join( ' && ', @checks );
135 }
136 6 } #/ sub __inline_generator
137  
138 3 ▶sub __deep_explanation {
Subroutine __deep_explanation: called CC=4 SCAR=13.9
139 3 my ( $type, $value, $varname ) = @_;
140
141 my @constraints =
142 3 map Types::TypeTiny::to_TypeTiny( $_ ), @{ $type->parameters };
143
144 3 ▶ if ( @$value % @constraints ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$value % @constraints) 1 2 3
145 return [
146 1 sprintf(
147 '"%s" expects a multiple of %d values in the array', $type,
148 scalar( @constraints )
149 ),
150 sprintf( '%d values found', scalar( @$value ) ),
151 ];
152 }
153
154 2 for my $i ( 0 .. $#$value ) {
155 4 my $constraint = $constraints[ $i % @constraints ];
156 4 ▶ next if $constraint->check( $value->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->check($value->[$i]) 2 2 4
157
158 return [
159 sprintf(
160 '"%s" constrains value at index %d of array with "%s"', $type, $i, $constraint
161 ),
162 @{
163 2 $constraint->validate_explain(
164 $value->[$i], sprintf( '%s->[%s]', $varname, $i )
165 )
166 },
167 ];
168 } #/ for my $i ( 0 .. $#$value)
169
170 # This should never happen...
171 0 return; # uncoverable statement
172 } #/ sub __deep_explanation
173  
174 5 my $label_counter = 0;
175  
176 6 ▶sub __coercion_generator {
Subroutine __coercion_generator: called CC=8 SCAR=20.8
177 6 my ( $parent, $child, @tuple ) = @_;
178
179 6 my $child_coercions_exist = 0;
180 6 my $all_inlinable = 1;
181 6 for my $tc ( @tuple ) {
182 16 ▶ $all_inlinable = 0 if !$tc->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->can_be_inlined 2 14 16
183 16 ▶ $all_inlinable = 0 if $tc->has_coercion && !$tc->coercion->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
if $tc->has_coercion and not $tc->coercion->can_be_inlined 3 13 16
Condition100%3/3
$tc->has_coercion and not $tc->coercion->can_be_inlined
!ll&&!rl&&r
1123
MC/DC100%2/2
$tc->has_coercion and not $tc->coercion->can_be_inlined
$tc->has_coercion not $tc->coercion->can_be_inlined
Truth table100%3/3
A and not(B)
A= $tc->has_coercionB= not $tc->coercion->can_be_inlined
ABresult
0X0
100
111
184 16 ▶ $child_coercions_exist++ if $tc->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
if $tc->has_coercion 5 11 16
185 }
186
187 6 ▶ return unless $child_coercions_exist;
Branch: 1 branch
BranchTrueFalseTotal
unless $child_coercions_exist 1 5 6
188 5 my $C = "Type::Coercion"->new( type_constraint => $child );
189
190 5 ▶ if ( $all_inlinable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_inlinable) { } 2 3 5
191 $C->add_type_coercions(
192 2 ▶ $parent => Types::Standard::Stringable {
Subroutine __ANON__: called CC=3 SCAR=11.0
193 2 my $label = sprintf( "CTUPLELABEL%d", ++$label_counter );
194 2 my $label2 = sprintf( "CTUPLEINNER%d", $label_counter );
195 2 my @code;
196 2 push @code, 'do { my ($orig, $return_orig, $tmp, @new) = ($_, 0);';
197 2 push @code, "$label: {";
198 2 push @code,
199 sprintf(
200 '(($return_orig = 1), last %s) if scalar(@$orig) %% %d != 0;', $label,
201 scalar @tuple
202 );
203 2 push @code, sprintf( 'my $%s = 0; while ($%s < @$orig) {', $label2, $label2 );
204 2 for my $i ( 0 .. $#tuple ) {
205 5 my $ct = $tuple[$i];
206 5 my $ct_coerce = $ct->has_coercion;
207
208 5 ▶ push @code, sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$ct_coerce ? : 2 3 5
209 'do { $tmp = %s; (%s) ? ($new[$%s + %d]=$tmp) : (($return_orig=1), last %s) };',
210 $ct_coerce
211 ? $ct->coercion->inline_coercion( "\$orig->[\$$label2 + $i]" )
212 : "\$orig->[\$$label2 + $i]",
213 $ct->inline_check( '$tmp' ),
214 $label2,
215 $i,
216 $label,
217 );
218 } #/ for my $i ( 0 .. $#tuple)
219 2 push @code, sprintf( '$%s += %d;', $label2, scalar( @tuple ) );
220 2 push @code, '}';
221 2 push @code, '}';
222 2 push @code, '$return_orig ? $orig : \\@new';
223 2 push @code, '}';
224 2 "@code";
225 }
226 2 );
227 } #/ if ( $all_inlinable )
228
229 else {
230 $C->add_type_coercions(
231 3 ▶ $parent => sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
232 3 ▶ my $value = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
233
234 3 ▶ if ( scalar( @$value ) % scalar( @tuple ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@$value) % scalar(@tuple) != 0) 0 3 3
235 0 return $value;
236 }
237
238 3 my @new;
239 3 for my $i ( 0 .. $#$value ) {
240 18 my $ct = $tuple[ $i % @tuple ];
241 18 ▶ my $x = $ct->has_coercion ? $ct->coerce( $value->[$i] ) : $value->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$ct->has_coercion ? : 6 12 18
242
243 18 ▶ return $value unless $ct->check( $x );
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->check($x) 0 18 18
244
245 18 $new[$i] = $x;
246 }
247
248 3 return \@new;
249 },
250 3 );
251 } #/ else [ if ( $all_inlinable ) ]
252
253 5 return $C;
254 } #/ sub __coercion_generator
255  
256 5 1;
257  
258 __END__
« blib/lib/Type/Tiny/Intersection.pm ↑ Summary lib/Types/Standard/ScalarRef.pm »