blib/lib/Type/Tiny/ConstrainedObject.pm

filter: Statementstmt 87.6% 56 / 65 (1 uncoverable) Branchbran 78.5% 11 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.8% 13 / 14 Podpod 100% 6 / 6 totaltotal 86.2% 87 / 102 (1 uncoverable) CC 10 SCAR 13.2
CC
10
Cov
85%
__ANON__
8.2
new
4.1
BEGIN
1.0
CRAP 3.7
« blib/lib/Type/Tiny/Duck.pm ↑ Summary blib/lib/Type/Tiny/Role.pm »
1 package Type::Tiny::ConstrainedObject;
2  
3 45 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 $Type::Tiny::ConstrainedObject::AUTHORITY = 'cpan:TOBYINK';
9 45 $Type::Tiny::ConstrainedObject::VERSION = '2.010001';
10 }
11  
12 45 $Type::Tiny::ConstrainedObject::VERSION =~ tr/_//d;
13  
14 9 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
15  
16 45 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 our @ISA = 'Type::Tiny';
18  
19 45 my %errlabel = (
20 parent => 'a parent',
21 constraint => 'a constraint coderef',
22 inlined => 'an inlining coderef',
23 );
24  
25 282 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
26 282 my $proto = shift;
27 0 ▶ my %opts = ( @_ == 1 ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 282 282
28 282 for my $key ( qw/ parent constraint inlined / ) {
29 837 ▶ next unless exists $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{$key} 828 9 837
30 _croak(
31 '%s type constraints cannot have %s passed to the constructor',
32 $proto->_short_name,
33 9 $errlabel{$key},
34 );
35 }
36 273 $proto->SUPER::new( %opts );
37 } #/ sub new
38  
39 7648 ▶sub has_parent {
Subroutine has_parent: called CC=1 SCAR=0.0
Pod documented
40 7648 !!1;
41 }
42  
43 244 ▶sub parent {
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
44 244 require Types::Standard;
45 244 Types::Standard::Object();
46 }
47  
48 0 ▶sub _short_name {
Subroutine _short_name: not called CC=1 SCAR=0.0
49 0 die "subclasses must implement this"; # uncoverable statement
50 }
51  
52 45 my $i = 0;
53 55 ▶my $_where_expressions = sub {
Subroutine __ANON__: called CC=7 SCAR=21.1
54 55 my $self = shift;
55 55 my $name = shift;
56 55 ▶ $name ||= "where expression check";
Condition50%1/2
$name ||= "where expression check"
l!l
550
MC/DC0%0/1
$name ||= "where expression check"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
57 55 my ( %env, @codes );
58 55 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 79 55 134
59 79 my $expr = shift;
60 79 my $constraint = shift;
61 79 ▶ if ( !ref $constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $constraint) { } 22 57 79
62 22 push @codes, sprintf( 'do { local $_ = %s; %s }', $expr, $constraint );
63 }
64 else {
65 57 require Types::Standard;
66 57 ▶ my $type =
Branch: 1 branch
BranchTrueFalseTotal
&Types::Standard::is_RegexpRef($constraint) ? : 12 45 57
67 Types::Standard::is_RegexpRef( $constraint )
68 ? Types::Standard::StrMatch()->of( $constraint )
69 : Types::TypeTiny::to_TypeTiny( $constraint );
70 57 ▶ if ( $type->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can_be_inlined) { } 57 0 57
71 57 push @codes,
72 sprintf(
73 'do { my $tmp = %s; %s }', $expr,
74 $type->inline_check( '$tmp' )
75 );
76 }
77 else {
78 0 ++$i;
79 0 $env{ '$chk' . $i } = do { my $chk = $type->compiled_check; \$chk };
80 0 push @codes, sprintf( '$chk%d->(%s)', $i, $expr );
81 }
82 } #/ else [ if ( !ref $constraint )]
83 } #/ while ( @_ )
84
85 55 ▶ if ( keys %env ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %env) { } 0 55 55
86
87 # cannot inline
88 0 my $sub = Eval::TypeTiny::eval_closure(
89 source =>
90 sprintf( 'sub ($) { local $_ = shift; %s }', join( q( and ), @codes ) ),
91 description => sprintf( '%s for %s', $name, $self->name ),
92 environment => \%env,
93 );
94 0 return $self->where( $sub );
95 } #/ if ( keys %env )
96 else {
97 55 return $self->where( join( q( and ), @codes ) );
98 }
99 45 };
100  
101 16 ▶sub stringifies_to {
Subroutine stringifies_to: called CC=1 SCAR=0.0
Pod documented
102 16 my $self = shift;
103 16 my ( $constraint ) = @_;
104 16 $self->$_where_expressions( "stringification check", q{"$_"}, $constraint );
105 }
106  
107 15 ▶sub numifies_to {
Subroutine numifies_to: called CC=1 SCAR=0.0
Pod documented
108 15 my $self = shift;
109 15 my ( $constraint ) = @_;
110 15 $self->$_where_expressions( "numification check", q{0+$_}, $constraint );
111 }
112  
113 24 ▶sub with_attribute_values {
Subroutine with_attribute_values: called CC=1 SCAR=0.0
Pod documented
114 24 my $self = shift;
115 24 my %constraint = @_;
116 $self->$_where_expressions(
117 "attributes check",
118 24 map { my $attr = $_; qq{\$_->$attr} => $constraint{$attr} }
119 sort keys %constraint,
120 );
121 }
122  
123 45 1;
124  
125 __END__
« blib/lib/Type/Tiny/Duck.pm ↑ Summary blib/lib/Type/Tiny/Role.pm »