blib/lib/Contract/Declare.pm

filter: Statementstmt 100% 81 / 81 Branchbran 84.6% 22 / 26 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 95.5% 130 / 136 CC 21 SCAR 16.8
CC
21
Cov
96%
method
11.1
_validate
6.0
__ANON__
3.0
CRAP 5.4
↑ Summary
1 package Contract::Declare;
2  
3 8 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 8 our @EXPORT = qw(contract interface method returns);
11  
12 8 our $CURRENT_PKG;
13 8 my %REGISTRY;
14  
15 8 our $VERSION = '1.0.0';
16  
17  
18 11 ▶sub contract {
Subroutine contract: called CC=2 SCAR=7.0
Pod documented
19 11 local $CURRENT_PKG;
20 11 my $block;
21 11 ($CURRENT_PKG, $block) = @_;
22 11 $block->();
23 6 _build_contract($CURRENT_PKG, $REGISTRY{$CURRENT_PKG});
24 6 ▶ delete $REGISTRY{$CURRENT_PKG} unless $ENV{CONTRACT_DECLARE_KEEP_CONTRACT};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CONTRACT_DECLARE_KEEP_CONTRACT"} 6 0 6
25 }
26  
27 11 ▶sub interface (&) { shift }
Subroutine interface: called CC=1 SCAR=0.0
Pod documented
28 12 ▶sub returns { [ @_ ] }
Subroutine returns: called CC=1 SCAR=0.0
Pod documented
29  
30 12 ▶sub method {
Subroutine method: called CC=11 SCAR=24.1
Pod documented
31 12 my ($name, @parts) = @_;
32 12 my (@in, $out);
33  
34 12 for my $p (@parts) {
35 21 ▶ if (ref($p) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "ARRAY") 12 9 21
36 12 $out = $p;
37 12 last;
38 }
39 9 push @in, $p;
40 }
41  
42 12 for my $arg (@in) {
43 9 ▶ croak "Contract violation: input argument for method '$name' must be an object with 'compiled_check'"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($arg) and $arg->can("compiled_check") 2 7 9
Condition66%2/3
builtin::blessed($arg) and $arg->can("compiled_check")
!ll&&!rl&&r
207
MC/DC50%1/2
builtin::blessed($arg) and $arg->can("compiled_check")
builtin::blessed($arg) $arg->can("compiled_check")
Truth table66%2/3
A and B
A= builtin::blessed($arg)B= $arg->can("compiled_check")
ABresult
0X0
100
111
44 unless blessed($arg) && $arg->can('compiled_check');
45 }
46  
47 10 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 10 0 10
48 10 ▶ croak "Contract violation: return type for method '$name' must be an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out eq "ARRAY" 0 10 10
49 unless ref($out) eq 'ARRAY';
50  
51 10 for my $ret (@$out) {
52 11 ▶ croak "Contract violation: each return type for method '$name' must be an object with 'compiled_check'"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ret) and $ret->can("compiled_check") 3 8 11
Condition100%3/3
builtin::blessed($ret) and $ret->can("compiled_check")
!ll&&!rl&&r
218
MC/DC100%2/2
builtin::blessed($ret) and $ret->can("compiled_check")
builtin::blessed($ret) $ret->can("compiled_check")
Truth table100%3/3
A and B
A= builtin::blessed($ret)B= $ret->can("compiled_check")
ABresult
0X0
100
111
53 unless blessed($ret) && $ret->can('compiled_check');
54 }
55 }
56  
57 7 $REGISTRY{$CURRENT_PKG}{$name} = [ \@in, $out ];
58 }
59  
60 6 ▶sub _build_contract {
Subroutine _build_contract: called CC=2 SCAR=6.9
61 6 my ($pkg, $contract) = @_;
62  
63 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 6 ▶ *{"${pkg}::new"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
66 5 my ($class, $impl) = @_;
67 5 my %cache;
68  
69 5 for my $method (keys %$contract) {
70 6 my $code = $impl->can($method);
71 6 ▶ croak "Contract violation: Implementation does not provide method '$method' for interface '$pkg'" unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 1 5 6
72 5 $cache{$method} = $code;
73 }
74  
75 bless {
76 4 _impl => $impl,
77 _cache => \%cache,
78 }, $pkg;
79 6 };
80  
81 6 for my $method (keys %$contract) {
82 7 my ($in_rules, $out_rules) = @{$contract->{$method}};
83 7 my @in_checks = map { $_->compiled_check } @$in_rules;
84 7 my @out_checks = map { $_->compiled_check } @$out_rules;
85  
86 7 ▶ *{"${pkg}::$method"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
87 7 my ($self, @args) = @_;
88  
89 7 _validate(\@args, \@in_checks, "$pkg\::$method args");
90  
91 5 my @res = $self->{_cache}{$method}->($self->{_impl}, @args);
92  
93 5 _validate(\@res, \@out_checks, "$pkg\::$method return");
94  
95 4 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
96 7 };
97 }
98  
99 6 Role::Tiny->make_role($pkg);
100 6 $Role::Tiny::INFO{$pkg}{requires} = [ sort keys %$contract ];
101  
102 8 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 }
104  
105 12 ▶sub _validate {
Subroutine _validate: called CC=6 SCAR=17.9
106 12 my ($values, $checkers, $label) = @_;
107  
108 12 ▶ return if @$checkers == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$checkers == 0 3 9 12
109  
110 9 ▶ if (@$values != @$checkers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values != @$checkers) 1 8 9
111 1 croak "Contract violation in $label: expected " . scalar(@$checkers) . " argument(s), got " . scalar(@$values);
112 }
113  
114 8 ▶ for (my $i = 0; $i < @$checkers; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$checkers) 10 6 16
115 10 ▶ next if $checkers->[$i]->($values->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $checkers->[$i]->($values->[$i]) 8 2 10
116 2 ▶ my $val = defined($values->[$i]) ? "'$values->[$i]'" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $values->[$i] ? : 2 0 2
117 2 croak "Contract violation in $label: argument #$i ($val) failed type check";
118 }
119 }
120  
121 8 1;
122  
123 __END__
↑ Summary