TypeTiny.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 84.3% 27 / 32 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 90.7% 118 / 130 CC 21 SCAR 22.0
CC
21
Cov
92%
args
12.1
args_pos
10.1
BEGIN
1.0
CRAP 9.0
↑ Summary TypeTiny/Check.pm »
1 package Smart::Args::TypeTiny;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 our $VERSION = "0.14";
5 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use PadWalker qw/var_name/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 our @EXPORT = qw/args args_pos/;
10  
11 16 $Carp::CarpInternal{+__PACKAGE__}++;
12  
13 16 ▶use Smart::Args::TypeTiny::Check qw/check_rule/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 my %is_invocant = map { ($_ => 1) } qw($self $class);
16  
17 57 ▶sub args {
Subroutine args: called CC=12 SCAR=24.9
Pod documented
18 {
19 57 package DB;
20 # call of caller in DB package sets @DB::args,
21 # which requires list context, but we don't need return values
22 57 () = CORE::caller(1);
23 }
24  
25 57 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 57 0 57
26 57 ▶ my $name = var_name(1, \$_[0]) || '';
Condition50%1/2
var_name(1, \$_[0]) || ''
l!l
570
MC/DC0%0/1
var_name(1, \$_[0]) || ''
var_name(1, \$_[0])
Truth table50%1/2
A
A= var_name(1, \$_[0])
Aresult
11
00
27 57 ▶ if ($is_invocant{$name}) { # seems instance/class method call
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invocant{$name}) 10 47 57
28 10 $name =~ s/^\$//;
29 10 $_[0] = shift @DB::args;
30 10 ▶ if (defined $_[1]) { # has rule?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 3 7 10
31 3 $_[0] = check_rule($_[1], $_[0], 1, $name);
32 2 shift;
33 }
34 9 shift;
35 }
36 }
37  
38 my $args = (@DB::args == 1 && ref $DB::args[0] eq 'HASH')
39 56 ▶ ? +{ %{$DB::args[0]} } # must be hash
Branch: 1 branch
BranchTrueFalseTotal
@DB::args == 1 && ref $DB::args[0] eq 'HASH' ? : 1 55 56
Condition66%2/3
@DB::args == 1 and ref $DB::args[0] eq "HASH"
!ll&&!rl&&r
5501
MC/DC50%1/2
@DB::args == 1 and ref $DB::args[0] eq "HASH"
@DB::args == 1 ref $DB::args[0] eq "HASH"
Truth table66%2/3
A and B
A= @DB::args == 1B= ref $DB::args[0] eq "HASH"
ABresult
0X0
100
111
40 : +{ @DB::args }; # must be key-value list
41 56 my $kv = {};
42  
43 # args my $var => RULE
44 # ~~~~ ~~~~
45 # undef defined
46  
47 56 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 71 40 111
48 71 ▶ (my $name = var_name(1, \$_[$i]))
Branch: 1 branch
BranchTrueFalseTotal
my $name = var_name(1, \$_[$i]) or Carp::croak("Usage: args my \$var => RULE, ...") 71 0 71
49 or Carp::croak('Usage: args my $var => RULE, ...');
50 71 $name =~ s/^\$//;
51  
52 # with rule (my $foo => RULE, ...)
53 71 ▶ if (defined $_[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i + 1]) { } 64 7 71
54 64 $_[$i] = $kv->{$name} = check_rule($_[$i+1], $args->{$name}, exists $args->{$name}, $name);
55 49 delete $args->{$name};
56 49 $i++;
57 }
58 # without rule (my $foo, my $bar, ...)
59 else {
60 7 ▶ unless (exists $args->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$name}) 1 6 7
61 1 Carp::confess("Required parameter '$name' not passed");
62 }
63 6 $_[$i] = $kv->{$name} = delete $args->{$name};
64 }
65 }
66  
67 40 for my $name (sort keys %$args) {
68 3 Carp::confess("Unexpected parameter '$name' passed");
69 }
70  
71 37 return $kv;
72 }
73  
74 28 ▶sub args_pos {
Subroutine args_pos: called CC=10 SCAR=23.2
Pod documented
75 {
76 28 package DB;
77 # call of caller in DB package sets @DB::args,
78 # which requires list context, but we don't need return values
79 28 () = CORE::caller(1);
80 }
81  
82 28 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 28 0 28
83 28 ▶ my $name = var_name(1, \$_[0]) || '';
Condition50%1/2
var_name(1, \$_[0]) || ''
l!l
280
MC/DC0%0/1
var_name(1, \$_[0]) || ''
var_name(1, \$_[0])
Truth table50%1/2
A
A= var_name(1, \$_[0])
Aresult
11
00
84 28 ▶ if ($is_invocant{$name}) { # seems instance/class method call
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invocant{$name}) 7 21 28
85 7 $name =~ s/^\$//;
86 7 $_[0] = shift @DB::args;
87 7 ▶ if (defined $_[1]) { # has rule?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 2 5 7
88 2 $_[0] = check_rule($_[1], $_[0], 1, $name);
89 1 shift;
90 }
91 6 shift;
92 }
93 }
94  
95 27 my $args = [@DB::args];
96 27 my $kv = {};
97  
98 # args my $var => RULE
99 # ~~~~ ~~~~
100 # undef defined
101  
102 27 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 38 19 57
103 38 ▶ (my $name = var_name(1, \$_[$i]))
Branch: 1 branch
BranchTrueFalseTotal
my $name = var_name(1, \$_[$i]) or Carp::croak("Usage: args_pos my \$var => RULE, ...") 38 0 38
104 or Carp::croak('Usage: args_pos my $var => RULE, ...');
105 38 $name =~ s/^\$//;
106  
107 # with rule (my $foo => RULE, ...)
108 38 ▶ if (defined $_[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i + 1]) { } 33 5 38
109 33 $_[$i] = $kv->{$name} = check_rule($_[$i+1], $args->[0], @$args > 0, $name);
110 25 shift @$args;
111 25 $i++;
112 }
113 # without rule (my $foo, my $bar, ...)
114 else {
115 5 ▶ unless (@$args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args > 0) 0 5 5
116 0 Carp::confess("Required parameter '$name' not passed");
117 }
118 5 $_[$i] = $kv->{$name} = shift @$args;
119 }
120 }
121  
122 19 ▶ if (@$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 2 17 19
123 2 Carp::confess('Too many parameters passed');
124 }
125  
126 17 return $kv;
127 }
128  
129 16 1;
130 __END__
↑ Summary TypeTiny/Check.pm »