blib/lib/Type/Tiny/XS.pm

filter: Statementstmt 80.6% 108 / 134 Branchbran 57.7% 52 / 90 Conditioncond 18.7% 9 / 48 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 70.0% 14 / 20 Podpod 100% 3 / 3 totaltotal 59.0% 190 / 322 CC 60 SCAR 37.0
CC
60
Cov
62%
get_coderef_for
69.5
__ANON__
20.0
__ANON__
20.0
CRAP 40.3
↑ Summary blib/lib/Type/Tiny/XS/Util.pm »
1 18 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use XSLoader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Type::Tiny::XS;
7  
8 18 our $AUTHORITY = 'cpan:TOBYINK';
9 18 our $VERSION = '0.025';
10  
11 18 __PACKAGE__->XSLoader::load( $VERSION );
12  
13 18 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 my %names = (
16 map +( $_ => __PACKAGE__ . "::$_" ), qw/
17 Any ArrayRef Bool ClassName CodeRef Defined
18 FileHandle GlobRef HashRef Int Num Object
19 Ref RegexpRef ScalarRef Str Undef Value
20 PositiveInt PositiveOrZeroInt NonEmptyStr
21 ArrayLike HashLike CodeLike StringLike
22 Map Tuple Enum AnyOf AllOf
23 /
24 );
25 18 $names{Item} = $names{Any};
26  
27 18 ▶if ( $] lt '5.010000' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] lt "5.010000") 0 18 18
28 0 require MRO::Compat;
29 0 *Type::Tiny::XS::Util::get_linear_isa = \&mro::get_linear_isa;
30
31 0 ▶ my $overloaded = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
32 0 require overload;
33 0 ▶ overload::Overloaded( ref $_[0] or $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
overload::Overloaded(ref $_[0] || $_[0]) and overload::Method(ref $_[0] || $_[0], $_[1]) 0 0 0
Condition0%0/6
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
34 and overload::Method( ( ref $_[0] or $_[0] ), $_[1] );
35 0 };
36
37 18 ▶ no warnings qw( uninitialized redefine once );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 0 ▶ *StringLike = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
39 0 ▶ defined( $_[0] ) && !ref( $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] or builtin::blessed($_[0]) and &$overloaded($_[0], "\"\"") 0 0 0
Condition0%0/9
defined $_[0] && !ref($_[0]) || builtin::blessed($_[0]) && &$overloaded($_[0], '""')
l!l&&r!l&&!r
000
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
000
builtin::blessed($_[0]) and &$overloaded($_[0], "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/4
defined $_[0] && !ref($_[0]) || builtin::blessed($_[0]) && &$overloaded($_[0], '""')
defined $_[0] not ref $_[0] builtin::blessed($_[0]) &$overloaded($_[0], "\"\"")
Truth table0%0/7
A and not(B) || C and D
A= defined $_[0]B= not ref $_[0]C= builtin::blessed($_[0])D= &$overloaded($_[0], "\"\"")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
40 or Scalar::Util::blessed( $_[0] ) && $overloaded->( $_[0], q[""] );
41 0 };
42 0 ▶ *CodeLike = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
43 0 ▶ ref( $_[0] ) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" or builtin::blessed($_[0]) and &$overloaded($_[0], "&{}") 0 0 0
Condition0%0/6
ref $_[0] eq 'CODE' || builtin::blessed($_[0]) && &$overloaded($_[0], '&{}')
l!l&&r!l&&!r
000
builtin::blessed($_[0]) and &$overloaded($_[0], "&{}")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_[0] eq 'CODE' || builtin::blessed($_[0]) && &$overloaded($_[0], '&{}')
ref $_[0] eq 'CODE' builtin::blessed($_[0]) &$overloaded($_[0], "&{}")
Truth table0%0/4
A || B and C
A= ref $_[0] eq 'CODE'B= builtin::blessed($_[0])C= &$overloaded($_[0], "&{}")
ABCresult
1XX1
0111
00X0
0100
44 or Scalar::Util::blessed( $_[0] ) && $overloaded->( $_[0], q[&{}] );
45 0 };
46 0 ▶ *HashLike = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
47 0 ▶ ref( $_[0] ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "HASH" or builtin::blessed($_[0]) and &$overloaded($_[0], "%{}") 0 0 0
Condition0%0/6
ref $_[0] eq 'HASH' || builtin::blessed($_[0]) && &$overloaded($_[0], '%{}')
l!l&&r!l&&!r
000
builtin::blessed($_[0]) and &$overloaded($_[0], "%{}")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_[0] eq 'HASH' || builtin::blessed($_[0]) && &$overloaded($_[0], '%{}')
ref $_[0] eq 'HASH' builtin::blessed($_[0]) &$overloaded($_[0], "%{}")
Truth table0%0/4
A || B and C
A= ref $_[0] eq 'HASH'B= builtin::blessed($_[0])C= &$overloaded($_[0], "%{}")
ABCresult
1XX1
0111
00X0
0100
48 or Scalar::Util::blessed( $_[0] ) && $overloaded->( $_[0], q[%{}] );
49 0 };
50 0 ▶ *ArrayLike = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
51 0 ▶ ref( $_[0] ) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or builtin::blessed($_[0]) and &$overloaded($_[0], "\@{}") 0 0 0
Condition0%0/6
ref $_[0] eq 'ARRAY' || builtin::blessed($_[0]) && &$overloaded($_[0], '@{}')
l!l&&r!l&&!r
000
builtin::blessed($_[0]) and &$overloaded($_[0], "\@{}")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_[0] eq 'ARRAY' || builtin::blessed($_[0]) && &$overloaded($_[0], '@{}')
ref $_[0] eq 'ARRAY' builtin::blessed($_[0]) &$overloaded($_[0], "\@{}")
Truth table0%0/4
A || B and C
A= ref $_[0] eq 'ARRAY'B= builtin::blessed($_[0])C= &$overloaded($_[0], "\@{}")
ABCresult
1XX1
0111
00X0
0100
52 or Scalar::Util::blessed( $_[0] ) && $overloaded->( $_[0], q[@{}] );
53 0 };
54 } #/ if ( $] < '5.010000' [)
55  
56 18 my %coderefs;
57  
58 579 ▶sub _know {
Subroutine _know: called CC=1 SCAR=0.0
59 579 my ( $coderef, $type ) = @_;
60 579 $coderefs{ refaddr( $coderef ) } = $type;
61 }
62  
63 3 ▶sub is_known {
Subroutine is_known: called CC=1 SCAR=0.0
Pod documented
64 3 my $coderef = shift;
65 3 $coderefs{ refaddr( $coderef ) };
66 }
67  
68 18 for ( reverse sort keys %names ) {
69 18 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 558 _know \&{ $names{$_} }, $_;
71 }
72  
73 18 my $id = 0;
74  
75 44 ▶sub get_coderef_for {
Subroutine get_coderef_for: called CC=41 SCAR=42.4
Pod documented
76 44 my $type = $_[0];
77
78 return do {
79 18 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 23 \&{ $names{$type} };
81 44 ▶ } if exists $names{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $names{$type} 23 21 44
82
83 21 my $made;
84
85 21 ▶ if ( $type =~ /^ArrayRef\[(.+)\]$/ ) {
Branch: 11 branches
BranchTrueFalseTotal
if ($type =~ /^ArrayRef\[(.+)\]$/) { } 7 14 21
elsif ($] ge '5.010000' and $type =~ /^ArrayLike\[(.+)\]$/) { } 1 13 14
elsif ($type =~ /^HashRef\[(.+)\]$/) { } 2 11 13
elsif ($] ge '5.010000' and $type =~ /^HashLike\[(.+)\]$/) { } 1 10 11
elsif ($type =~ /^Map\[(.+),(.+)\]$/) { } 2 8 10
elsif ($type =~ /^(AnyOf|AllOf|Tuple)\[(.+)\]$/) { } 4 4 8
elsif ($type =~ /^Maybe\[(.+)\]$/) { } 0 4 4
elsif ($type =~ /^InstanceOf\[(.+)\]$/) { } 1 3 4
elsif ($type =~ /^HasMethods\[(.+)\]$/) { } 0 3 3
elsif ($type =~ /^Enum\[".*"\]$/) { } 1 2 3
elsif ($type =~ /^Enum\[(.+)\]$/) { } 2 0 2
86 7 ▶ my $child = get_coderef_for( $1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $child = get_coderef_for($1) or return 7 0 7
87 7 $made = _parameterize_ArrayRef_for( $child );
88 }
89
90 ▶ elsif ( $] ge '5.010000' and $type =~ /^ArrayLike\[(.+)\]$/ ) {
Condition66%2/3
$] ge "5.010000" and $type =~ /^ArrayLike\[(.+)\]$/
!ll&&!rl&&r
0131
MC/DC50%1/2
$] ge "5.010000" and $type =~ /^ArrayLike\[(.+)\]$/
$] ge "5.010000" $type =~ /^ArrayLike\[(.+)\]$/
Truth table66%2/3
A and B
A= $] ge "5.010000"B= $type =~ /^ArrayLike\[(.+)\]$/
ABresult
0X0
100
111
91 1 ▶ my $child = get_coderef_for( $1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $child = get_coderef_for($1) or return 1 0 1
92 1 $made = _parameterize_ArrayLike_for( $child );
93 }
94
95 elsif ( $type =~ /^HashRef\[(.+)\]$/ ) {
96 2 ▶ my $child = get_coderef_for( $1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $child = get_coderef_for($1) or return 2 0 2
97 2 $made = _parameterize_HashRef_for( $child );
98 }
99
100 ▶ elsif ( $] ge '5.010000' and $type =~ /^HashLike\[(.+)\]$/ ) {
Condition66%2/3
$] ge "5.010000" and $type =~ /^HashLike\[(.+)\]$/
!ll&&!rl&&r
0101
MC/DC50%1/2
$] ge "5.010000" and $type =~ /^HashLike\[(.+)\]$/
$] ge "5.010000" $type =~ /^HashLike\[(.+)\]$/
Truth table66%2/3
A and B
A= $] ge "5.010000"B= $type =~ /^HashLike\[(.+)\]$/
ABresult
0X0
100
111
101 1 ▶ my $child = get_coderef_for( $1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $child = get_coderef_for($1) or return 1 0 1
102 1 $made = _parameterize_HashLike_for( $child );
103 }
104
105 elsif ( $type =~ /^Map\[(.+),(.+)\]$/ ) {
106 2 my @children;
107 2 ▶ if ( eval { require Type::Parser } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Type::Parser } }) { } 2 0 2
108 2 @children = map scalar( get_coderef_for( $_ ) ), _parse_parameters( $type );
109 }
110 else {
111 0 push @children, get_coderef_for( $1 );
112 0 push @children, get_coderef_for( $2 );
113 }
114 2 ▶ @children == 2 or return;
Branch: 1 branch
BranchTrueFalseTotal
@children == 2 or return 2 0 2
115 2 ▶ defined or return for @children;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or return 4 0 4
116 2 $made = _parameterize_Map_for( \@children );
117 } #/ elsif ( $type =~ /^Map\[(.+),(.+)\]$/)
118
119 elsif ( $type =~ /^(AnyOf|AllOf|Tuple)\[(.+)\]$/ ) {
120 4 my $base = $1;
121 my @children =
122 map scalar( get_coderef_for( $_ ) ),
123 4 ▶ ( eval { require Type::Parser } )
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Type::Parser } } ? : 4 0 4
124 ? _parse_parameters( $type )
125 : split( /,/, $2 );
126 4 ▶ defined or return for @children;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or return 10 0 10
127 4 my $maker = __PACKAGE__->can( "_parameterize_${base}_for" );
128 4 ▶ $made = $maker->( \@children ) if $maker;
Branch: 1 branch
BranchTrueFalseTotal
if $maker 4 0 4
129 } #/ elsif ( $type =~ /^(AnyOf|AllOf|Tuple)\[(.+)\]$/)
130
131 elsif ( $type =~ /^Maybe\[(.+)\]$/ ) {
132 0 ▶ my $child = get_coderef_for( $1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $child = get_coderef_for($1) or return 0 0 0
133 0 $made = _parameterize_Maybe_for( $child );
134 }
135
136 elsif ( $type =~ /^InstanceOf\[(.+)\]$/ ) {
137 1 my $class = $1;
138 1 ▶ return unless Type::Tiny::XS::Util::is_valid_class_name( $class );
Branch: 1 branch
BranchTrueFalseTotal
unless Type::Tiny::XS::Util::is_valid_class_name($class) 0 1 1
139 1 $made = Type::Tiny::XS::Util::generate_isa_predicate_for( $class );
140 }
141
142 elsif ( $type =~ /^HasMethods\[(.+)\]$/ ) {
143 0 my $methods = [ sort( split /,/, $1 ) ];
144 0 ▶ /^[^\W0-9]\w*$/ or return for @$methods;
Branch: 1 branch
BranchTrueFalseTotal
/^[^\W0-9]\w*$/ or return 0 0 0
145 0 $made = Type::Tiny::XS::Util::generate_can_predicate_for( $methods );
146 }
147
148 # Type::Tiny::Enum > 1.010003 double-quotes its enums
149 elsif ( $type =~ /^Enum\[".*"\]$/ ) {
150 1 ▶ if ( eval { require Type::Parser } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Type::Parser } }) 1 0 1
151 1 my $parsed = Type::Parser::parse( $type );
152 1 ▶ if ( $parsed->{type} eq "parameterized" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"type"} eq "parameterized") 1 0 1
153 1 my @todo = $parsed->{params};
154 1 my @strings;
155 my $bad;
156 1 ▶ while ( my $todo = shift @todo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $todo = shift @todo) 6 1 7
157 6 ▶ if ( $todo->{type} eq 'list' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($todo->{'type'} eq 'list') { } 1 5 6
elsif ($todo->{'type'} eq 'expression' and $todo->{'op'}->type eq Type::Parser::COMMA()) { } 2 3 5
elsif ($todo->{'type'} eq 'primary' and $todo->{'token'}->type eq 'QUOTELIKE') { } 3 0 3
158 1 push @todo, @{ $todo->{list} };
159 }
160 ▶ elsif ( $todo->{type} eq "expression"
Condition66%2/3
$todo->{"type"} eq "expression" and $todo->{"op"}->type eq Type::Parser::COMMA()
!ll&&!rl&&r
302
MC/DC50%1/2
$todo->{"type"} eq "expression" and $todo->{"op"}->type eq Type::Parser::COMMA()
$todo->{"type"} eq "expression" $todo->{"op"}->type eq Type::Parser::COMMA()
Truth table66%2/3
A and B
A= $todo->{"type"} eq "expression"B= $todo->{"op"}->type eq Type::Parser::COMMA()
ABresult
0X0
100
111
161 && $todo->{op}->type eq Type::Parser::COMMA() )
162 {
163 2 push @todo, $todo->{lhs}, $todo->{rhs};
164 }
165 ▶ elsif ( $todo->{type} eq "primary" && $todo->{token}->type eq "QUOTELIKE" ) {
Condition33%1/3
$todo->{"type"} eq "primary" and $todo->{"token"}->type eq "QUOTELIKE"
!ll&&!rl&&r
003
MC/DC0%0/2
$todo->{"type"} eq "primary" and $todo->{"token"}->type eq "QUOTELIKE"
$todo->{"type"} eq "primary" $todo->{"token"}->type eq "QUOTELIKE"
Truth table33%1/3
A and B
A= $todo->{"type"} eq "primary"B= $todo->{"token"}->type eq "QUOTELIKE"
ABresult
0X0
100
111
166 3 push @strings, eval( $todo->{token}->spelling );
167 }
168 else {
169 # Unexpected entry in the parse-tree, bail out
170 0 $bad = 1;
171 }
172 } #/ while ( my $todo = shift ...)
173 1 ▶ $made = _parameterize_Enum_for( \@strings ) unless $bad;
Branch: 1 branch
BranchTrueFalseTotal
unless $bad 1 0 1
174 } #/ if ( $parsed->{type} eq...)
175 } #/ if ( eval { require Type::Parser...})
176 } #/ elsif ( $type =~ /^Enum\[".*"\]$/)
177
178 elsif ( $type =~ /^Enum\[(.+)\]$/ ) {
179 2 my $strings = [ sort( split /,/, $1 ) ];
180 2 $made = _parameterize_Enum_for( $strings );
181 }
182
183 21 ▶ if ( $made ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($made) 21 0 21
184 18 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
185 21 my $slot = sprintf( '%s::AUTO::TC%d', __PACKAGE__, ++$id );
186 21 $names{$type} = $slot;
187 21 _know( $made, $type );
188 21 *$slot = $made;
189 21 return $made;
190 }
191
192 0 return;
193 } #/ sub get_coderef_for
194  
195 0 ▶sub get_subname_for {
Subroutine get_subname_for: not called CC=2 SCAR=17.9
Pod documented
196 0 my $type = $_[0];
197 0 ▶ get_coderef_for( $type ) unless exists $names{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $names{$type} 0 0 0
198 0 $names{$type};
199 }
200  
201 6 ▶sub _parse_parameters {
Subroutine _parse_parameters: called CC=2 SCAR=7.1
202 6 my $got = Type::Parser::parse( @_ );
203 6 ▶ $got->{params} or return;
Branch: 1 branch
BranchTrueFalseTotal
$got->{"params"} or return 6 0 6
204 6 _handle_expr( $got->{params} );
205 }
206  
207 51 ▶sub _handle_expr {
Subroutine _handle_expr: called CC=6 SCAR=18.0
208 51 my $e = shift;
209
210 51 ▶ if ( $e->{type} eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "list") 11 40 51
211 11 return map _handle_expr( $_ ), @{ $e->{list} };
212 }
213 40 ▶ if ( $e->{type} eq 'parameterized' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "parameterized") 5 35 40
214 5 my ( $base ) = _handle_expr( $e->{base} );
215 5 my @params = _handle_expr( $e->{params} );
216 5 return sprintf( '%s[%s]', $base, join( q[,], @params ) );
217 }
218 35 ▶ if ( $e->{type} eq 'expression' and $e->{op}->type eq Type::Parser::COMMA() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "expression" and $e->{"op"}->type eq Type::Parser::COMMA()) 12 23 35
Condition66%2/3
$e->{"type"} eq "expression" and $e->{"op"}->type eq Type::Parser::COMMA()
!ll&&!rl&&r
23012
MC/DC50%1/2
$e->{"type"} eq "expression" and $e->{"op"}->type eq Type::Parser::COMMA()
$e->{"type"} eq "expression" $e->{"op"}->type eq Type::Parser::COMMA()
Truth table66%2/3
A and B
A= $e->{"type"} eq "expression"B= $e->{"op"}->type eq Type::Parser::COMMA()
ABresult
0X0
100
111
219 12 return _handle_expr( $e->{lhs} ), _handle_expr( $e->{rhs} );
220 }
221 23 ▶ if ( $e->{type} eq 'primary' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "primary") 23 0 23
222 23 return $e->{token}->spelling;
223 }
224
225 0 '****';
226 } #/ sub _handle_expr
227  
228 18 1;
229  
230 __END__
↑ Summary blib/lib/Type/Tiny/XS/Util.pm »